Skip to content
This repository was archived by the owner on May 1, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions hw1/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions hw2/app/app.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 28 additions & 3 deletions hw2/lib/router.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +20,31 @@ 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)
pattern = Regexp.new(path.gsub(/:[a-zA-Z0-9_]+/, '[a-zA-Z0-9_]+'))
return pattern
end
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
env['params'] = get_params(route[:pattern], env['REQUEST_PATH'])
return route[:app]
end
end
return ->(env) {[404, {}, ['Page not found']]}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return here is not needed

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
35 changes: 26 additions & 9 deletions hw2/spec/lib/router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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