This repository was archived by the owner on May 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Homework #21
Open
yelagins
wants to merge
6
commits into
denysxftr:master
Choose a base branch
from
yelagins:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Homework #21
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /.bundle/ | ||
| /.yardoc | ||
| /Gemfile.lock | ||
| /_yardoc/ | ||
| /coverage/ | ||
| /doc/ | ||
| /pkg/ | ||
| /spec/reports/ | ||
| /tmp/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --format documentation | ||
| --color |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| # Specify your gem's dependencies in controuter.gemspec | ||
| gemspec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2016 Serg | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Controuter | ||
|
|
||
| Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/controuter`. To experiment with that code, run `bin/console` for an interactive prompt. | ||
|
|
||
| TODO: Delete this and the text above, and describe your gem | ||
|
|
||
| ## Installation | ||
|
|
||
| Add this line to your application's Gemfile: | ||
|
|
||
| ```ruby | ||
| gem 'controuter' | ||
| ``` | ||
|
|
||
| And then execute: | ||
|
|
||
| $ bundle | ||
|
|
||
| Or install it yourself as: | ||
|
|
||
| $ gem install controuter | ||
|
|
||
| ## Usage | ||
|
|
||
| TODO: Write usage instructions here | ||
|
|
||
| ## Development | ||
|
|
||
| After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. | ||
|
|
||
| To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). | ||
|
|
||
| ## Contributing | ||
|
|
||
| Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/controuter. | ||
|
|
||
|
|
||
| ## License | ||
|
|
||
| The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| require "bundler/gem_tasks" | ||
| task :default => :spec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #!/usr/bin/env ruby | ||
|
|
||
| require "bundler/setup" | ||
| require "controuter" | ||
|
|
||
| # You can add fixtures and/or initialization code here to make experimenting | ||
| # with your gem easier. You can also use a different console, if you like. | ||
|
|
||
| # (If you use this, don't forget to add pry to your Gemfile!) | ||
| # require "pry" | ||
| # Pry.start | ||
|
|
||
| require "irb" | ||
| IRB.start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| IFS=$'\n\t' | ||
| set -vx | ||
|
|
||
| bundle install | ||
|
|
||
| # Do any other automated setup that you need to do here |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # coding: utf-8 | ||
| lib = File.expand_path('../lib', __FILE__) | ||
| $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
| require 'controuter/version' | ||
|
|
||
| Gem::Specification.new do |spec| | ||
| spec.name = "controuter" | ||
| spec.version = Controuter::VERSION | ||
| spec.authors = ["Serg"] | ||
| spec.email = ["yelagins@gmail.com"] | ||
| spec.files = ["lib/controuter.rb"] | ||
| spec.summary = %q{gem.} | ||
| spec.homepage = "http://sadasdasd.com" | ||
| spec.license = "MIT" | ||
|
|
||
| # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' | ||
| # to allow pushing to a single host or delete this section to allow pushing to any host. | ||
| # if spec.respond_to?(:metadata) | ||
| # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserdsfver.com'" | ||
| # else | ||
| # raise "RubyGems 2.0 or newer is required to protect against " \ | ||
| # "public gem pushes." | ||
| # end | ||
|
|
||
| spec.files = `git ls-files -z`.split("\x0").reject do |f| | ||
| f.match(%r{^(test|spec|features)/}) | ||
| end | ||
| spec.bindir = "exe" | ||
| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | ||
| spec.require_paths = ["lib"] | ||
|
|
||
| spec.add_development_dependency "bundler", "~> 1.13" | ||
| spec.add_development_dependency "rake", "~> 10.0" | ||
| spec.add_development_dependency "rspec", "~> 3.0" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| require "controuter/version" | ||
|
|
||
| module Controuter | ||
|
|
||
| class Controller | ||
| RESPONSE_TYPES = { | ||
| text: ['text/html', ->(c) { c.to_s }], | ||
| json: ['application/json', ->(c) { Oj.dump(c) }] | ||
| }.freeze | ||
|
|
||
| def call(env) | ||
| @env = env | ||
| @request = Rack::Request.new(env) | ||
| #@request.params.merge!(env['router.params'] || {}) | ||
| send(@action_name) | ||
| [200, @response_headers, [@response_body]] | ||
| end | ||
|
|
||
| def self.action(action_name) | ||
| proc { |env| new(action_name).call(env) } | ||
| end | ||
|
|
||
| private | ||
| attr_reader :request | ||
|
|
||
| def initialize(action_name) | ||
| @action_name = action_name | ||
| end | ||
|
|
||
| def params | ||
| request.params | ||
| end | ||
|
|
||
| def response(type, content) | ||
| @response_headers ||= {} | ||
| @response_headers.merge!('Content-Type' => RESPONSE_TYPES[type][0]) | ||
| @response_body = RESPONSE_TYPES[type][1].call(content) | ||
| end | ||
| end | ||
|
|
||
| class Router | ||
| def call(env) | ||
| find(env) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def initialize(&block) | ||
| @routes = {} | ||
| instance_exec(&block) | ||
| end | ||
|
|
||
| def find(env) | ||
| current_env = ->(env) {[404, {}, ['404']]} | ||
| @routes[env['REQUEST_METHOD']].each { |(key,value)| | ||
| regular_value = Regexp.new('\A' + key.gsub(/:[\w-]+/, '[\w-]+') + '\Z') | ||
| current_env = get_controller_action(value) if regular_value=~env['REQUEST_PATH'] | ||
| } | ||
| current_env.call(env) | ||
| end | ||
| def get_controller_action(str) | ||
| controller_name, action_name = str.split('#') | ||
| controller_name = to_upper_camel_case(controller_name) | ||
| Kernel.const_get(controller_name).send(:action, action_name) | ||
| end | ||
|
|
||
| def to_upper_camel_case(str) | ||
| str | ||
| .split('/') | ||
| .map { |part| part.split('_').map(&:capitalize).join } | ||
| .join('::') + 'Controller' | ||
| end | ||
|
|
||
| def get(path, rack_app) | ||
| match('GET', path, rack_app) | ||
| end | ||
|
|
||
| def post(path, rack_app) | ||
| match('POST', path, rack_app) | ||
| end | ||
|
|
||
| def match(http_method, path, rack_app) | ||
| @routes[http_method] ||= {} | ||
| @routes[http_method][path] = rack_app | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module Controuter | ||
| VERSION = "0.2.4" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| require "spec_helper" | ||
| require "./lib/controuter" | ||
|
|
||
| describe Controuter do | ||
| it "has a version number" do | ||
| expect(Controuter::VERSION).not_to be nil | ||
| end | ||
| end | ||
|
|
||
| describe Controuter::Router do | ||
| subject do | ||
| Controuter::Router.new do | ||
| get '/test', 'tests#show' | ||
| end | ||
| end | ||
|
|
||
| context "when page not found" do | ||
| let(:env) { { 'REQUEST_PATH' => '/wrong-page', 'REQUEST_METHOD' => 'GET'} } | ||
| it 'matches request' do | ||
| expect(subject.call(env)).to eq [404, {}, ['404']] | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe Controuter::Router do | ||
| subject do | ||
| Controuter::Router.new do | ||
| get '/page', 'tests#show' | ||
| end | ||
|
|
||
| TestsController = ->(_var){"show"} | ||
| end | ||
|
|
||
| context "simulating controller action" do | ||
| let(:env) { { 'REQUEST_PATH' => '/page', 'REQUEST_METHOD' => 'GET'} } | ||
|
|
||
| it do | ||
| expect(subject.call(env)).to eq "show" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) | ||
| require "controuter" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Fibonacci | ||
| include Enumerable | ||
| def initialize(times) | ||
| @times,@a,@b,@c = times,1,1,1; | ||
| end | ||
| def each | ||
| @times.times do | ||
| yield @a;@a=@b;@b=@a+@c;@c=@a; | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| Application = Router.new do | ||
| get '/test', ->(env) { [200, {}, ['get test']] } | ||
| post '/test', ->(env) { [200, {}, ['post test']] } | ||
| get '/test1', ->(env) {[200, {}, ['Page 1']]} | ||
| get '/test2/test:id', ->(env) {[200, {}, ['Page 2']]} | ||
| get '/test3/:id', ->(env) {[200, {}, ['Page 3']]} | ||
| get '/test4/:id/test5', ->(env) {[200, {}, ['Page 4']]} | ||
| get '/test4/:id/test5/:id', ->(env) {[200, {}, ['Page 5']]} | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| language: ruby | ||
| rvm: | ||
| - 2.3.1 | ||
| script: | ||
| - bash before-deploy.sh | ||
| deploy: | ||
| provider: heroku | ||
| api_key: | ||
| secure: NNdlTVbKxcM0pdHxn79+1zRyDx3umKAa9d3Ka09TLUQkjfNQyINi6dtbwZtfmduOIYlWMTy0ZSfkOzZnAvcwGO9b4unmdnE8FIvxDiKzTH15S9I4BePfB+5Utpa4mbN0UnB8v0GcW1b9+fwnHNQsdwNdr08AvPENEoSMbIjyhAK2IvlQHlqwKR72JvDRfRxwwG7S1s6+pSpAHh9Hl6BBPzCwn5Uf4Lt+Vwwcb9MPQPwpWyXzQk+LF2QIAO23oqb0l6/jcCaEcSw8J2rDPh1gCZnBSDj2JuGtA8URxgemFMPrzaPNDkyo0NZUDmdzHjGFyeD89C/fZ+N5704pg/Osuxl8DP0J/+xavDO/eY9gdskQhfjq91kN2FiBu7/P35pm963YzCwDqlZysxA8MYniR3ST1C9D5q3eluNY0vFEWaHUhi90URuVy3tuLl1b+cOHQ+TKRwYuisyaOPhQF76ERnjHm+jr5k6NIqD+sbBJ5D+uk0dpc4/y4FOYyxnuZ5JGK/TrmMXfCPs1yKgk6KwBa0/RNolIAQ1dmSrjBZxnZNxS0kLXGPrtYAZCrP0xvcy+KY1Wi9KfCtQ1Xao/ASanPxW24/IXiTczXxdsxqxQowkLZRBNBIDLDsev8MKMD1Mg9yeH3wDa22ahTR0i7SR/PYpiURf13g54OsmUrElh998= | ||
| app: home-work-project |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
| source "https://rubygems.org" | ||
|
|
||
| gem "rack" | ||
| gem "rspec" | ||
| gem "oj" | ||
| gem "controuter" #custom gem |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
every class/module tests should be separated in files
like
/spec/lib/router_spec.rbIt's a good practice