From 177d4f0956f74518b1d69b766a935542e3100ae7 Mon Sep 17 00:00:00 2001 From: Pavelgyravel Date: Mon, 7 Nov 2016 21:52:34 +0200 Subject: [PATCH 1/4] hw1 - Demchenko --- hw1/fibonacci.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/hw1/fibonacci.rb b/hw1/fibonacci.rb index e69de29..aa0c7bd 100644 --- a/hw1/fibonacci.rb +++ b/hw1/fibonacci.rb @@ -0,0 +1,18 @@ +class Fibonacci + include Enumerable + + def initialize (n) + @n = n + end + + def each + a, b = 0, 1 + @n.times do + temp = a + a = b + b = temp + b + yield a + end + end + +end \ No newline at end of file From eebc0b767a5a5744b27760bad3306191aa3690da Mon Sep 17 00:00:00 2001 From: Pavelgyravel Date: Mon, 14 Nov 2016 23:04:51 +0200 Subject: [PATCH 2/4] Router completed --- hw2/lib/router.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/hw2/lib/router.rb b/hw2/lib/router.rb index eb6e53d..b434d19 100644 --- a/hw2/lib/router.rb +++ b/hw2/lib/router.rb @@ -1,6 +1,6 @@ class Router def call(env) - @routes[env['REQUEST_METHOD']][env['REQUEST_PATH']].call(env) + select_route(env).call(env) end private @@ -20,6 +20,22 @@ def post(path, rack_app) def match(http_method, path, rack_app) @routes[http_method] ||= {} - @routes[http_method][path] = rack_app + @routes[http_method][regexp_method(path)] = { + :app => rack_app, + :pattern => path + } end -end + + def regexp_method(path) + Regexp.new(path.gsub(/:[a-zA-Z0-9_]+/, '[a-zA-Z0-9_]+')) + end + + def select_route(env) + @routes[env['REQUEST_METHOD']].each do |path_regexp, route| + if env['REQUEST_PATH'] =~ path_regexp + return route[:app] + end + end + return ->(env) {[404, {}, ['Page not found']]} + end +end \ No newline at end of file From d2d389c7212ace37bd236c60d5288ab4339c44e5 Mon Sep 17 00:00:00 2001 From: Pavelgyravel Date: Tue, 15 Nov 2016 00:00:11 +0200 Subject: [PATCH 3/4] Add params parser --- hw2/app/app.rb | 5 +++++ hw2/lib/router.rb | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/hw2/app/app.rb b/hw2/app/app.rb index 2be87a8..500d08a 100644 --- a/hw2/app/app.rb +++ b/hw2/app/app.rb @@ -1,4 +1,9 @@ Application = Router.new do get '/test', ->(env) { [200, {}, ['get test']] } post '/test', ->(env) { [200, {}, ['post test']] } + + + get '/post/:post_id/comment/:comment_id', ->(env) { [200, {}, ["Post id: #{env['params']['post_id']}, comment id: #{env['params']['comment_id']}"]] } + get '/post/:post_id', ->(env) { [200, {}, ["Post id: #{env['params']['post_id']}"]] } + end diff --git a/hw2/lib/router.rb b/hw2/lib/router.rb index b434d19..3f66bce 100644 --- a/hw2/lib/router.rb +++ b/hw2/lib/router.rb @@ -27,15 +27,24 @@ def match(http_method, path, rack_app) end def regexp_method(path) - Regexp.new(path.gsub(/:[a-zA-Z0-9_]+/, '[a-zA-Z0-9_]+')) + pattern = Regexp.new(path.gsub(/:[a-zA-Z0-9_]+/, '[a-zA-Z0-9_]+')) + return pattern end def select_route(env) @routes[env['REQUEST_METHOD']].each do |path_regexp, route| if env['REQUEST_PATH'] =~ path_regexp + env['params'] = get_params(route[:pattern], env['REQUEST_PATH']) return route[:app] end end return ->(env) {[404, {}, ['Page not found']]} end + + def get_params(pattern, path) + pattern.split('/') + .zip(path.split('/')) + .reject{|e| e.first==e.last} + .map{|e| [e.first[1..-1], e.last]}.to_h + end end \ No newline at end of file From a9836d9c22f8efb7e2604a41755cd9af6ea91404 Mon Sep 17 00:00:00 2001 From: Pavelgyravel Date: Tue, 15 Nov 2016 00:08:54 +0200 Subject: [PATCH 4/4] Add tests for routing with params and 'Page not found' error --- hw2/spec/lib/router_spec.rb | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/hw2/spec/lib/router_spec.rb b/hw2/spec/lib/router_spec.rb index e53a275..e42bd04 100644 --- a/hw2/spec/lib/router_spec.rb +++ b/hw2/spec/lib/router_spec.rb @@ -4,15 +4,8 @@ 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']] } + get '/post/:post_id/comment/:comment_id', ->(env) { [200, {}, ["Post id: #{env['params']['post_id']}, comment id: #{env['params']['comment_id']}"]] } + get '/post/:post_id', ->(env) { [200, {}, ["Post id: #{env['params']['post_id']}"]] } end end @@ -31,4 +24,28 @@ expect(subject.call(env)).to eq [200, {}, ['post test']] end end + + context 'when request is GET with param post_id' do + let(:env) {{ 'REQUEST_PATH' => '/post/43', 'REQUEST_METHOD' => 'GET'}} + + it 'matches request' do + expect(subject.call(env)).to eq [200, {}, ['Post id: 43']] + end + end + + context 'when request is GET with params post_id and comment_id' do + let(:env) {{ 'REQUEST_PATH' => '/post/43/comment/77', 'REQUEST_METHOD' => 'GET'}} + + it 'matches request' do + expect(subject.call(env)).to eq [200, {}, ['Post id: 43, comment id: 77']] + end + end + + context 'when page is not exist' do + let(:env) {{ 'REQUEST_PATH' => '/winter', 'REQUEST_METHOD' => 'GET'}} + + it 'matches request' do + expect(subject.call(env)).to eq [404, {}, ['Page not found']] + end + end end