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
Empty file modified .travis.yml
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# kottans_homework
2 changes: 1 addition & 1 deletion check.sh
100644 → 100755
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)
60 changes: 60 additions & 0 deletions hw1/fibonacci.rb
100644 → 100755
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
3 changes: 3 additions & 0 deletions hw1/fibonacci_test.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def assert(expect:, to_equal: true, text:)
)

sequence = Fibonacci.new(1)

puts sequence.map(&:to_s)

assert(
expect: sequence.methods.include?(:each),
text: 'has method each'
Expand Down
2 changes: 2 additions & 0 deletions hw2/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
5 changes: 5 additions & 0 deletions hw2/Gemfile
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"
28 changes: 28 additions & 0 deletions hw2/Gemfile.lock
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
7 changes: 7 additions & 0 deletions hw2/app/app.rb
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
3 changes: 3 additions & 0 deletions hw2/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require './main'

run Application
51 changes: 51 additions & 0 deletions hw2/lib/router.rb
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']]
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.

you don't need return here

else
return @routes[http_method][get_routes[0]].call(env)
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.

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
2 changes: 2 additions & 0 deletions hw2/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './lib/router'
require './app/app'
51 changes: 51 additions & 0 deletions hw2/spec/lib/router_spec.rb
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
28 changes: 28 additions & 0 deletions hw2/spec/spec_helper.rb
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