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
Fastman kottans homework #13
Open
fastmanV
wants to merge
11
commits into
denysxftr:master
Choose a base branch
from
fastmanV: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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3457769
Update fibonacci.rb
fastmanV 95b2d6e
my router
fastmanV 93b9192
Update class.rb
fastmanV af57813
last upd
fastmanV 5ef90d1
Remove duplicated directory
fastmanV b2346e2
ololo
fastmanV d26f4d7
ololo
fastmanV 1d47397
Delete class.rb
fastmanV 80c637e
change upd
fastmanV 0a993aa
Update check.sh
fastmanV 27619ff
fix
fastmanV 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
Empty file.
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 @@ | ||
| # kottans_homework |
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 +1 @@ | ||
| ruby ./hw1/fibonacci_test.rb | ||
| ruby ./hw1/fibonacci_test.rb && (cd hw2 && bundle install && bundle exec rspec) |
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,60 @@ | ||
|
|
||
| class Fibonacci | ||
| include Enumerable | ||
|
|
||
| def fibonacci(n) | ||
| a = 1 | ||
| b = 1 | ||
| n.times do | ||
| temp = a | ||
| a = b | ||
| b = temp + b | ||
| end | ||
|
|
||
| return a | ||
| end | ||
|
|
||
|
|
||
| def each(&block) | ||
| @num.times do |n| | ||
| block.call(fibonacci(n)) | ||
| end | ||
|
|
||
|
|
||
| end | ||
|
|
||
| def initialize(num) | ||
| @num = num | ||
|
|
||
| end | ||
| end | ||
|
|
||
| class Fibonacci | ||
| include Enumerable | ||
|
|
||
| def fibonacci(n) | ||
| a = 1 | ||
| b = 1 | ||
| n.times do | ||
| temp = a | ||
| a = b | ||
| b = temp + b | ||
| end | ||
|
|
||
| return a | ||
| end | ||
|
|
||
|
|
||
| def each(&block) | ||
| @num.times do |n| | ||
| block.call(fibonacci(n)) | ||
| end | ||
|
|
||
|
|
||
| end | ||
|
|
||
| def initialize(num) | ||
| @num = num | ||
|
|
||
| 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
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 @@ | ||
| --color | ||
| --require spec_helper |
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,5 @@ | ||
| # frozen_string_literal: true | ||
| source "https://rubygems.org" | ||
|
|
||
| gem "rack" | ||
| gem "rspec" |
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,28 @@ | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| diff-lcs (1.2.5) | ||
| rack (2.0.1) | ||
| rspec (3.5.0) | ||
| rspec-core (~> 3.5.0) | ||
| rspec-expectations (~> 3.5.0) | ||
| rspec-mocks (~> 3.5.0) | ||
| rspec-core (3.5.4) | ||
| rspec-support (~> 3.5.0) | ||
| rspec-expectations (3.5.0) | ||
| diff-lcs (>= 1.2.0, < 2.0) | ||
| rspec-support (~> 3.5.0) | ||
| rspec-mocks (3.5.0) | ||
| diff-lcs (>= 1.2.0, < 2.0) | ||
| rspec-support (~> 3.5.0) | ||
| rspec-support (3.5.0) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| rack | ||
| rspec | ||
|
|
||
| BUNDLED WITH | ||
| 1.13.5 |
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 @@ | ||
| Application = Router.new do | ||
| get '/test', ->(env) { [200, {}, ['get test']] } | ||
| post '/test', ->(env) { [200, {}, ['post test']] } | ||
| get '/post/:id', ->(env) { [200, {}, ['post show page']] } | ||
| get '/post', ->(env) { [200, {}, ['post index page']] } | ||
| get '/post/:id/any/:id', ->(env) { [200, {}, ['post any index page']] } | ||
| 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 @@ | ||
| require './main' | ||
|
|
||
| run Application |
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,51 @@ | ||
| class Router | ||
| def call(env) | ||
| route_for(env) | ||
| end | ||
|
|
||
|
|
||
| def reg_path(path) | ||
| Regexp.new("^#{path.gsub(/(:\w+){1,}/, '\w+')}\/?$") | ||
| end | ||
|
|
||
|
|
||
| def route_for(env) | ||
| http_method = env['REQUEST_METHOD'] | ||
| url_path = env['REQUEST_PATH'] | ||
| get_routes = @routes[http_method] | ||
| .map {|key, val| key if reg_path(key) | ||
| .match(url_path)} | ||
| .reject {|val| val.nil?} | ||
|
|
||
| if get_routes.empty? | ||
| return [404, {}, ['404 error']] | ||
| else | ||
| return @routes[http_method][get_routes[0]].call(env) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
| end | ||
|
|
||
| end | ||
|
|
||
|
|
||
| private | ||
|
|
||
| def initialize(&block) | ||
| @routes = {} | ||
| instance_exec(&block) | ||
| 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 | ||
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 './lib/router' | ||
| require './app/app' |
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,51 @@ | ||
| RSpec.describe Router do | ||
| subject do | ||
| Router.new do | ||
| get '/test', ->(env) { [200, {}, ['get test']] } | ||
| post '/test', ->(env) { [200, {}, ['post test']] } | ||
|
|
||
| ## | ||
| # TODO: router should match path by pattern like | ||
| # Pattern: /posts/:name | ||
| # Paths: | ||
| # /post/about_ruby | ||
| # /post/43 | ||
| # Cover this with tests. | ||
| # | ||
| get '/post/:name', ->(env) { [200, {}, ['post show page']] } | ||
| end | ||
| end | ||
|
|
||
| context 'when request is GET' do | ||
| let(:env) { { 'REQUEST_PATH' => '/test', 'REQUEST_METHOD' => 'GET'} } | ||
|
|
||
| it 'matches request' do | ||
| expect(subject.call(env)).to eq [200, {}, ['get test']] | ||
| end | ||
| end | ||
|
|
||
| context 'when request is GET' do | ||
| let(:env) { { 'REQUEST_PATH' => '/post/1', 'REQUEST_METHOD' => 'GET'} } | ||
|
|
||
| it 'matches request' do | ||
| expect(subject.call(env)).to eq [200, {}, ['post show page']] | ||
| end | ||
| end | ||
|
|
||
|
|
||
| context 'when request is GET' do | ||
| let(:env) { { 'REQUEST_PATH' => '/', 'REQUEST_METHOD' => 'GET'} } | ||
|
|
||
| it 'matches request' do | ||
| expect(subject.call(env)).to eq [404, {}, ['404 error']] | ||
| end | ||
| end | ||
|
|
||
| context 'when request is POST' do | ||
| let(:env) { { 'REQUEST_PATH' => '/test', 'REQUEST_METHOD' => 'POST'} } | ||
|
|
||
| it 'matches request' do | ||
| expect(subject.call(env)).to eq [200, {}, ['post test']] | ||
| 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,28 @@ | ||
| require './lib/router' | ||
|
|
||
| RSpec.configure do |config| | ||
| config.expect_with :rspec do |expectations| | ||
| expectations.include_chain_clauses_in_custom_matcher_descriptions = true | ||
| end | ||
|
|
||
| config.mock_with :rspec do |mocks| | ||
| mocks.verify_partial_doubles = true | ||
| end | ||
|
|
||
| config.shared_context_metadata_behavior = :apply_to_host_groups | ||
|
|
||
| config.filter_run_when_matching :focus | ||
|
|
||
| config.disable_monkey_patching! | ||
|
|
||
| config.warnings = true | ||
|
|
||
| if config.files_to_run.one? | ||
| config.default_formatter = 'doc' | ||
| end | ||
|
|
||
| config.profile_examples = 10 | ||
|
|
||
| config.order = :random | ||
| Kernel.srand config.seed | ||
| end |
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.
you don't need return here