diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 diff --git a/README.md b/README.md new file mode 100755 index 0000000..85378d0 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# kottans_homework diff --git a/check.sh b/check.sh old mode 100644 new mode 100755 index 450f6e4..ce6c4ea --- a/check.sh +++ b/check.sh @@ -1 +1 @@ -ruby ./hw1/fibonacci_test.rb +ruby ./hw1/fibonacci_test.rb && (cd hw2 && bundle install && bundle exec rspec) diff --git a/hw1/fibonacci.rb b/hw1/fibonacci.rb old mode 100644 new mode 100755 index e69de29..3f5461a --- a/hw1/fibonacci.rb +++ b/hw1/fibonacci.rb @@ -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 diff --git a/hw1/fibonacci_test.rb b/hw1/fibonacci_test.rb old mode 100644 new mode 100755 index 0782e7d..c8941cb --- a/hw1/fibonacci_test.rb +++ b/hw1/fibonacci_test.rb @@ -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' diff --git a/hw2/.rspec b/hw2/.rspec new file mode 100755 index 0000000..83e16f8 --- /dev/null +++ b/hw2/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/hw2/Gemfile b/hw2/Gemfile new file mode 100755 index 0000000..f8bf844 --- /dev/null +++ b/hw2/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true +source "https://rubygems.org" + +gem "rack" +gem "rspec" diff --git a/hw2/Gemfile.lock b/hw2/Gemfile.lock new file mode 100755 index 0000000..ec73d13 --- /dev/null +++ b/hw2/Gemfile.lock @@ -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 diff --git a/hw2/app/app.rb b/hw2/app/app.rb new file mode 100755 index 0000000..6b2d9e0 --- /dev/null +++ b/hw2/app/app.rb @@ -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 diff --git a/hw2/config.ru b/hw2/config.ru new file mode 100755 index 0000000..c2e95d5 --- /dev/null +++ b/hw2/config.ru @@ -0,0 +1,3 @@ +require './main' + +run Application diff --git a/hw2/lib/router.rb b/hw2/lib/router.rb new file mode 100755 index 0000000..00924ef --- /dev/null +++ b/hw2/lib/router.rb @@ -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) + 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 diff --git a/hw2/main.rb b/hw2/main.rb new file mode 100755 index 0000000..acfd4b3 --- /dev/null +++ b/hw2/main.rb @@ -0,0 +1,2 @@ +require './lib/router' +require './app/app' diff --git a/hw2/spec/lib/router_spec.rb b/hw2/spec/lib/router_spec.rb new file mode 100755 index 0000000..a0ee879 --- /dev/null +++ b/hw2/spec/lib/router_spec.rb @@ -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 diff --git a/hw2/spec/spec_helper.rb b/hw2/spec/spec_helper.rb new file mode 100755 index 0000000..ef0fca6 --- /dev/null +++ b/hw2/spec/spec_helper.rb @@ -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