From 3457769a7546bce350889c324d5bda53f627b908 Mon Sep 17 00:00:00 2001 From: fastmanV Date: Sun, 6 Nov 2016 09:55:03 +0200 Subject: [PATCH 1/9] Update fibonacci.rb --- hw1/fibonacci.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/hw1/fibonacci.rb b/hw1/fibonacci.rb index e69de29..4d7101c 100644 --- a/hw1/fibonacci.rb +++ b/hw1/fibonacci.rb @@ -0,0 +1,29 @@ +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 From 95b2d6e3ca0c495036bb970a3d724523bcc00372 Mon Sep 17 00:00:00 2001 From: fastmanV Date: Sun, 13 Nov 2016 22:06:37 +0200 Subject: [PATCH 2/9] my router --- hw2/Gemfile | 5 ++++ hw2/Gemfile.lock | 28 ++++++++++++++++++++ hw2/app/app.rb | 7 +++++ hw2/class.rb | 15 +++++++++++ hw2/config.ru | 3 +++ hw2/lib/router.rb | 51 +++++++++++++++++++++++++++++++++++++ hw2/main.rb | 2 ++ hw2/spec/lib/router_spec.rb | 51 +++++++++++++++++++++++++++++++++++++ hw2/spec/spec_helper.rb | 28 ++++++++++++++++++++ 9 files changed, 190 insertions(+) create mode 100644 hw2/Gemfile create mode 100644 hw2/Gemfile.lock create mode 100644 hw2/app/app.rb create mode 100644 hw2/class.rb create mode 100644 hw2/config.ru create mode 100644 hw2/lib/router.rb create mode 100644 hw2/main.rb create mode 100644 hw2/spec/lib/router_spec.rb create mode 100644 hw2/spec/spec_helper.rb diff --git a/hw2/Gemfile b/hw2/Gemfile new file mode 100644 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 100644 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 100644 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/class.rb b/hw2/class.rb new file mode 100644 index 0000000..092920f --- /dev/null +++ b/hw2/class.rb @@ -0,0 +1,15 @@ +class HelloWorld + def call(env) + req = Rack::Request.new(env) + case req.path_info + when /hello/ + [200, {"Content-Type" => "text/html"}, ["Hello World!"]] + when /goodbye/ + [500, {"Content-Type" => "text/html"}, ["Goodbye Cruel World!"]] + else + [404, {"Content-Type" => "text/html"}, ["I'm Lost!"]] + end + end +end + +run HelloWorld.new diff --git a/hw2/config.ru b/hw2/config.ru new file mode 100644 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 100644 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 100644 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 100644 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 100644 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 From 93b919298a8e8ba2a08277658c7114971e0e0e8d Mon Sep 17 00:00:00 2001 From: fastmanV Date: Sun, 13 Nov 2016 22:33:09 +0200 Subject: [PATCH 3/9] Update class.rb --- hw2/class.rb | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/hw2/class.rb b/hw2/class.rb index 092920f..8b13789 100644 --- a/hw2/class.rb +++ b/hw2/class.rb @@ -1,15 +1 @@ -class HelloWorld - def call(env) - req = Rack::Request.new(env) - case req.path_info - when /hello/ - [200, {"Content-Type" => "text/html"}, ["Hello World!"]] - when /goodbye/ - [500, {"Content-Type" => "text/html"}, ["Goodbye Cruel World!"]] - else - [404, {"Content-Type" => "text/html"}, ["I'm Lost!"]] - end - end -end -run HelloWorld.new From af578136f18ea411b8c586ebe6627d528952fa48 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 14 Nov 2016 11:45:40 +0200 Subject: [PATCH 4/9] last upd --- kottans_2016_homeworks/.travis.yml | 5 ++ kottans_2016_homeworks/README.md | 1 + kottans_2016_homeworks/check.sh | 1 + kottans_2016_homeworks/hw1/fibonacci.rb | 29 +++++++++++ kottans_2016_homeworks/hw1/fibonacci_test.rb | 43 ++++++++++++++++ kottans_2016_homeworks/hw2/.rspec | 2 + kottans_2016_homeworks/hw2/Gemfile | 5 ++ kottans_2016_homeworks/hw2/Gemfile.lock | 28 ++++++++++ kottans_2016_homeworks/hw2/app/app.rb | 7 +++ kottans_2016_homeworks/hw2/config.ru | 3 ++ kottans_2016_homeworks/hw2/lib/router.rb | 51 +++++++++++++++++++ kottans_2016_homeworks/hw2/main.rb | 2 + .../hw2/spec/lib/router_spec.rb | 51 +++++++++++++++++++ .../hw2/spec/spec_helper.rb | 28 ++++++++++ 14 files changed, 256 insertions(+) create mode 100755 kottans_2016_homeworks/.travis.yml create mode 100755 kottans_2016_homeworks/README.md create mode 100755 kottans_2016_homeworks/check.sh create mode 100755 kottans_2016_homeworks/hw1/fibonacci.rb create mode 100755 kottans_2016_homeworks/hw1/fibonacci_test.rb create mode 100755 kottans_2016_homeworks/hw2/.rspec create mode 100755 kottans_2016_homeworks/hw2/Gemfile create mode 100755 kottans_2016_homeworks/hw2/Gemfile.lock create mode 100755 kottans_2016_homeworks/hw2/app/app.rb create mode 100755 kottans_2016_homeworks/hw2/config.ru create mode 100755 kottans_2016_homeworks/hw2/lib/router.rb create mode 100755 kottans_2016_homeworks/hw2/main.rb create mode 100755 kottans_2016_homeworks/hw2/spec/lib/router_spec.rb create mode 100755 kottans_2016_homeworks/hw2/spec/spec_helper.rb diff --git a/kottans_2016_homeworks/.travis.yml b/kottans_2016_homeworks/.travis.yml new file mode 100755 index 0000000..2d528b0 --- /dev/null +++ b/kottans_2016_homeworks/.travis.yml @@ -0,0 +1,5 @@ +language: ruby +rvm: + - 2.3.1 +script: + - bash check.sh diff --git a/kottans_2016_homeworks/README.md b/kottans_2016_homeworks/README.md new file mode 100755 index 0000000..85378d0 --- /dev/null +++ b/kottans_2016_homeworks/README.md @@ -0,0 +1 @@ +# kottans_homework diff --git a/kottans_2016_homeworks/check.sh b/kottans_2016_homeworks/check.sh new file mode 100755 index 0000000..450f6e4 --- /dev/null +++ b/kottans_2016_homeworks/check.sh @@ -0,0 +1 @@ +ruby ./hw1/fibonacci_test.rb diff --git a/kottans_2016_homeworks/hw1/fibonacci.rb b/kottans_2016_homeworks/hw1/fibonacci.rb new file mode 100755 index 0000000..f4f9a95 --- /dev/null +++ b/kottans_2016_homeworks/hw1/fibonacci.rb @@ -0,0 +1,29 @@ +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/kottans_2016_homeworks/hw1/fibonacci_test.rb b/kottans_2016_homeworks/hw1/fibonacci_test.rb new file mode 100755 index 0000000..f7cb20f --- /dev/null +++ b/kottans_2016_homeworks/hw1/fibonacci_test.rb @@ -0,0 +1,43 @@ +require './hw1/fibonacci' + +def assert(expect:, to_equal: true, text:) + if expect == to_equal + puts "\e[32m#{text}: OK!\e[0m" + else + puts "\e[31m#{text}: FAILED!\e[0m" + puts "Expected: #{expect.inspect}" + puts "To equal: #{to_equal.inspect}" + exit(1) + end +end + +assert( + expect: defined?(Fibonacci), + to_equal: 'constant', + text: 'class defined' +) + +sequence = Fibonacci.new(1) +puts sequence.map(&:to_s) +assert( + expect: sequence.methods.include?(:each), + text: 'has method each' +) + +assert( + expect: Fibonacci.ancestors.include?(Enumerable), + text: 'has enumerable included' +) + +assert( + expect: sequence.map(&:to_s), + to_equal: ["1"], + text: 'map works for one element' +) + +sequence = Fibonacci.new(10) +assert( + expect: sequence.map(&:to_s), + to_equal: ["1", "1", "2", "3", "5", "8", "13", "21", "34", "55"], + text: 'map works for many elements' +) diff --git a/kottans_2016_homeworks/hw2/.rspec b/kottans_2016_homeworks/hw2/.rspec new file mode 100755 index 0000000..83e16f8 --- /dev/null +++ b/kottans_2016_homeworks/hw2/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/kottans_2016_homeworks/hw2/Gemfile b/kottans_2016_homeworks/hw2/Gemfile new file mode 100755 index 0000000..f8bf844 --- /dev/null +++ b/kottans_2016_homeworks/hw2/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true +source "https://rubygems.org" + +gem "rack" +gem "rspec" diff --git a/kottans_2016_homeworks/hw2/Gemfile.lock b/kottans_2016_homeworks/hw2/Gemfile.lock new file mode 100755 index 0000000..ec73d13 --- /dev/null +++ b/kottans_2016_homeworks/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/kottans_2016_homeworks/hw2/app/app.rb b/kottans_2016_homeworks/hw2/app/app.rb new file mode 100755 index 0000000..6b2d9e0 --- /dev/null +++ b/kottans_2016_homeworks/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/kottans_2016_homeworks/hw2/config.ru b/kottans_2016_homeworks/hw2/config.ru new file mode 100755 index 0000000..c2e95d5 --- /dev/null +++ b/kottans_2016_homeworks/hw2/config.ru @@ -0,0 +1,3 @@ +require './main' + +run Application diff --git a/kottans_2016_homeworks/hw2/lib/router.rb b/kottans_2016_homeworks/hw2/lib/router.rb new file mode 100755 index 0000000..00924ef --- /dev/null +++ b/kottans_2016_homeworks/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/kottans_2016_homeworks/hw2/main.rb b/kottans_2016_homeworks/hw2/main.rb new file mode 100755 index 0000000..acfd4b3 --- /dev/null +++ b/kottans_2016_homeworks/hw2/main.rb @@ -0,0 +1,2 @@ +require './lib/router' +require './app/app' diff --git a/kottans_2016_homeworks/hw2/spec/lib/router_spec.rb b/kottans_2016_homeworks/hw2/spec/lib/router_spec.rb new file mode 100755 index 0000000..a0ee879 --- /dev/null +++ b/kottans_2016_homeworks/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/kottans_2016_homeworks/hw2/spec/spec_helper.rb b/kottans_2016_homeworks/hw2/spec/spec_helper.rb new file mode 100755 index 0000000..ef0fca6 --- /dev/null +++ b/kottans_2016_homeworks/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 From 5ef90d125bbd1360d881d086a4de0a179ffaf83f Mon Sep 17 00:00:00 2001 From: root Date: Mon, 14 Nov 2016 11:49:54 +0200 Subject: [PATCH 5/9] Remove duplicated directory --- kottans_2016_homeworks/.travis.yml | 5 -- kottans_2016_homeworks/README.md | 1 - kottans_2016_homeworks/check.sh | 1 - kottans_2016_homeworks/hw1/fibonacci.rb | 29 ----------- kottans_2016_homeworks/hw1/fibonacci_test.rb | 43 ---------------- kottans_2016_homeworks/hw2/.rspec | 2 - kottans_2016_homeworks/hw2/Gemfile | 5 -- kottans_2016_homeworks/hw2/Gemfile.lock | 28 ---------- kottans_2016_homeworks/hw2/app/app.rb | 7 --- kottans_2016_homeworks/hw2/config.ru | 3 -- kottans_2016_homeworks/hw2/lib/router.rb | 51 ------------------- kottans_2016_homeworks/hw2/main.rb | 2 - .../hw2/spec/lib/router_spec.rb | 51 ------------------- .../hw2/spec/spec_helper.rb | 28 ---------- 14 files changed, 256 deletions(-) delete mode 100755 kottans_2016_homeworks/.travis.yml delete mode 100755 kottans_2016_homeworks/README.md delete mode 100755 kottans_2016_homeworks/check.sh delete mode 100755 kottans_2016_homeworks/hw1/fibonacci.rb delete mode 100755 kottans_2016_homeworks/hw1/fibonacci_test.rb delete mode 100755 kottans_2016_homeworks/hw2/.rspec delete mode 100755 kottans_2016_homeworks/hw2/Gemfile delete mode 100755 kottans_2016_homeworks/hw2/Gemfile.lock delete mode 100755 kottans_2016_homeworks/hw2/app/app.rb delete mode 100755 kottans_2016_homeworks/hw2/config.ru delete mode 100755 kottans_2016_homeworks/hw2/lib/router.rb delete mode 100755 kottans_2016_homeworks/hw2/main.rb delete mode 100755 kottans_2016_homeworks/hw2/spec/lib/router_spec.rb delete mode 100755 kottans_2016_homeworks/hw2/spec/spec_helper.rb diff --git a/kottans_2016_homeworks/.travis.yml b/kottans_2016_homeworks/.travis.yml deleted file mode 100755 index 2d528b0..0000000 --- a/kottans_2016_homeworks/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -language: ruby -rvm: - - 2.3.1 -script: - - bash check.sh diff --git a/kottans_2016_homeworks/README.md b/kottans_2016_homeworks/README.md deleted file mode 100755 index 85378d0..0000000 --- a/kottans_2016_homeworks/README.md +++ /dev/null @@ -1 +0,0 @@ -# kottans_homework diff --git a/kottans_2016_homeworks/check.sh b/kottans_2016_homeworks/check.sh deleted file mode 100755 index 450f6e4..0000000 --- a/kottans_2016_homeworks/check.sh +++ /dev/null @@ -1 +0,0 @@ -ruby ./hw1/fibonacci_test.rb diff --git a/kottans_2016_homeworks/hw1/fibonacci.rb b/kottans_2016_homeworks/hw1/fibonacci.rb deleted file mode 100755 index f4f9a95..0000000 --- a/kottans_2016_homeworks/hw1/fibonacci.rb +++ /dev/null @@ -1,29 +0,0 @@ -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/kottans_2016_homeworks/hw1/fibonacci_test.rb b/kottans_2016_homeworks/hw1/fibonacci_test.rb deleted file mode 100755 index f7cb20f..0000000 --- a/kottans_2016_homeworks/hw1/fibonacci_test.rb +++ /dev/null @@ -1,43 +0,0 @@ -require './hw1/fibonacci' - -def assert(expect:, to_equal: true, text:) - if expect == to_equal - puts "\e[32m#{text}: OK!\e[0m" - else - puts "\e[31m#{text}: FAILED!\e[0m" - puts "Expected: #{expect.inspect}" - puts "To equal: #{to_equal.inspect}" - exit(1) - end -end - -assert( - expect: defined?(Fibonacci), - to_equal: 'constant', - text: 'class defined' -) - -sequence = Fibonacci.new(1) -puts sequence.map(&:to_s) -assert( - expect: sequence.methods.include?(:each), - text: 'has method each' -) - -assert( - expect: Fibonacci.ancestors.include?(Enumerable), - text: 'has enumerable included' -) - -assert( - expect: sequence.map(&:to_s), - to_equal: ["1"], - text: 'map works for one element' -) - -sequence = Fibonacci.new(10) -assert( - expect: sequence.map(&:to_s), - to_equal: ["1", "1", "2", "3", "5", "8", "13", "21", "34", "55"], - text: 'map works for many elements' -) diff --git a/kottans_2016_homeworks/hw2/.rspec b/kottans_2016_homeworks/hw2/.rspec deleted file mode 100755 index 83e16f8..0000000 --- a/kottans_2016_homeworks/hw2/.rspec +++ /dev/null @@ -1,2 +0,0 @@ ---color ---require spec_helper diff --git a/kottans_2016_homeworks/hw2/Gemfile b/kottans_2016_homeworks/hw2/Gemfile deleted file mode 100755 index f8bf844..0000000 --- a/kottans_2016_homeworks/hw2/Gemfile +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true -source "https://rubygems.org" - -gem "rack" -gem "rspec" diff --git a/kottans_2016_homeworks/hw2/Gemfile.lock b/kottans_2016_homeworks/hw2/Gemfile.lock deleted file mode 100755 index ec73d13..0000000 --- a/kottans_2016_homeworks/hw2/Gemfile.lock +++ /dev/null @@ -1,28 +0,0 @@ -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/kottans_2016_homeworks/hw2/app/app.rb b/kottans_2016_homeworks/hw2/app/app.rb deleted file mode 100755 index 6b2d9e0..0000000 --- a/kottans_2016_homeworks/hw2/app/app.rb +++ /dev/null @@ -1,7 +0,0 @@ -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/kottans_2016_homeworks/hw2/config.ru b/kottans_2016_homeworks/hw2/config.ru deleted file mode 100755 index c2e95d5..0000000 --- a/kottans_2016_homeworks/hw2/config.ru +++ /dev/null @@ -1,3 +0,0 @@ -require './main' - -run Application diff --git a/kottans_2016_homeworks/hw2/lib/router.rb b/kottans_2016_homeworks/hw2/lib/router.rb deleted file mode 100755 index 00924ef..0000000 --- a/kottans_2016_homeworks/hw2/lib/router.rb +++ /dev/null @@ -1,51 +0,0 @@ -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/kottans_2016_homeworks/hw2/main.rb b/kottans_2016_homeworks/hw2/main.rb deleted file mode 100755 index acfd4b3..0000000 --- a/kottans_2016_homeworks/hw2/main.rb +++ /dev/null @@ -1,2 +0,0 @@ -require './lib/router' -require './app/app' diff --git a/kottans_2016_homeworks/hw2/spec/lib/router_spec.rb b/kottans_2016_homeworks/hw2/spec/lib/router_spec.rb deleted file mode 100755 index a0ee879..0000000 --- a/kottans_2016_homeworks/hw2/spec/lib/router_spec.rb +++ /dev/null @@ -1,51 +0,0 @@ -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/kottans_2016_homeworks/hw2/spec/spec_helper.rb b/kottans_2016_homeworks/hw2/spec/spec_helper.rb deleted file mode 100755 index ef0fca6..0000000 --- a/kottans_2016_homeworks/hw2/spec/spec_helper.rb +++ /dev/null @@ -1,28 +0,0 @@ -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 From b2346e222a7c3379e00549143ef8fef5443c4661 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 14 Nov 2016 11:56:19 +0200 Subject: [PATCH 6/9] ololo --- .travis.yml | 5 ++++ README.md | 1 + check.sh | 1 + hw1/fibonacci.rb | 29 +++++++++++++++++++++ hw1/fibonacci_test.rb | 43 +++++++++++++++++++++++++++++++ hw2/.rspec | 2 ++ hw2/Gemfile | 5 ++++ hw2/Gemfile.lock | 28 ++++++++++++++++++++ hw2/app/app.rb | 7 +++++ hw2/config.ru | 3 +++ hw2/lib/router.rb | 51 +++++++++++++++++++++++++++++++++++++ hw2/main.rb | 2 ++ hw2/spec/lib/router_spec.rb | 51 +++++++++++++++++++++++++++++++++++++ hw2/spec/spec_helper.rb | 28 ++++++++++++++++++++ 14 files changed, 256 insertions(+) create mode 100755 .travis.yml create mode 100755 README.md create mode 100755 check.sh create mode 100755 hw1/fibonacci.rb create mode 100755 hw1/fibonacci_test.rb create mode 100755 hw2/.rspec create mode 100755 hw2/Gemfile create mode 100755 hw2/Gemfile.lock create mode 100755 hw2/app/app.rb create mode 100755 hw2/config.ru create mode 100755 hw2/lib/router.rb create mode 100755 hw2/main.rb create mode 100755 hw2/spec/lib/router_spec.rb create mode 100755 hw2/spec/spec_helper.rb diff --git a/.travis.yml b/.travis.yml new file mode 100755 index 0000000..2d528b0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: ruby +rvm: + - 2.3.1 +script: + - bash check.sh 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 new file mode 100755 index 0000000..450f6e4 --- /dev/null +++ b/check.sh @@ -0,0 +1 @@ +ruby ./hw1/fibonacci_test.rb diff --git a/hw1/fibonacci.rb b/hw1/fibonacci.rb new file mode 100755 index 0000000..f4f9a95 --- /dev/null +++ b/hw1/fibonacci.rb @@ -0,0 +1,29 @@ +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 new file mode 100755 index 0000000..f7cb20f --- /dev/null +++ b/hw1/fibonacci_test.rb @@ -0,0 +1,43 @@ +require './hw1/fibonacci' + +def assert(expect:, to_equal: true, text:) + if expect == to_equal + puts "\e[32m#{text}: OK!\e[0m" + else + puts "\e[31m#{text}: FAILED!\e[0m" + puts "Expected: #{expect.inspect}" + puts "To equal: #{to_equal.inspect}" + exit(1) + end +end + +assert( + expect: defined?(Fibonacci), + to_equal: 'constant', + text: 'class defined' +) + +sequence = Fibonacci.new(1) +puts sequence.map(&:to_s) +assert( + expect: sequence.methods.include?(:each), + text: 'has method each' +) + +assert( + expect: Fibonacci.ancestors.include?(Enumerable), + text: 'has enumerable included' +) + +assert( + expect: sequence.map(&:to_s), + to_equal: ["1"], + text: 'map works for one element' +) + +sequence = Fibonacci.new(10) +assert( + expect: sequence.map(&:to_s), + to_equal: ["1", "1", "2", "3", "5", "8", "13", "21", "34", "55"], + text: 'map works for many elements' +) 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 From 1d47397da3db2178b5597be8656ad0d9916a528a Mon Sep 17 00:00:00 2001 From: fastmanV Date: Mon, 14 Nov 2016 12:47:13 +0200 Subject: [PATCH 7/9] Delete class.rb --- hw2/class.rb | 1 - 1 file changed, 1 deletion(-) delete mode 100644 hw2/class.rb diff --git a/hw2/class.rb b/hw2/class.rb deleted file mode 100644 index 8b13789..0000000 --- a/hw2/class.rb +++ /dev/null @@ -1 +0,0 @@ - From 80c637e63b1c5a1a60309811375decc935dc2e80 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 14 Nov 2016 13:12:35 +0200 Subject: [PATCH 8/9] change upd --- check.sh | 2 +- hw1/fibonacci.rb | 63 +++++++++++++++++++++---------------------- hw1/fibonacci_test.rb | 5 ++-- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/check.sh b/check.sh index 450f6e4..ce6c4ea 100755 --- 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 index 4fe45d5..3f5461a 100755 --- a/hw1/fibonacci.rb +++ b/hw1/fibonacci.rb @@ -1,4 +1,4 @@ -<<<<<<< HEAD + class Fibonacci include Enumerable @@ -28,34 +28,33 @@ def initialize(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 ->>>>>>> origin/master + +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 index 8c0314b..c8941cb 100755 --- a/hw1/fibonacci_test.rb +++ b/hw1/fibonacci_test.rb @@ -18,10 +18,9 @@ def assert(expect:, to_equal: true, text:) ) sequence = Fibonacci.new(1) -<<<<<<< HEAD + puts sequence.map(&:to_s) -======= ->>>>>>> origin/master + assert( expect: sequence.methods.include?(:each), text: 'has method each' From 0a993aa58fb1aa1bf4f10c117ea9a44eb99b1ff8 Mon Sep 17 00:00:00 2001 From: fastmanV Date: Mon, 14 Nov 2016 13:51:51 +0200 Subject: [PATCH 9/9] Update check.sh --- check.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check.sh b/check.sh index 450f6e4..ce6c4ea 100755 --- 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)