From eda2b90965bbf7415d7eabcb4f2267b9ea051767 Mon Sep 17 00:00:00 2001 From: yelagins Date: Mon, 7 Nov 2016 17:28:20 +0200 Subject: [PATCH 1/5] Update fibonacci.rb --- hw1/fibonacci.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/hw1/fibonacci.rb b/hw1/fibonacci.rb index e69de29..ef851d7 100644 --- a/hw1/fibonacci.rb +++ b/hw1/fibonacci.rb @@ -0,0 +1,11 @@ +class Fibonacci + include Enumerable + def initialize(times) + @times,@a,@b,@c = times,1,1,1; + end + def each + @times.times do + yield @a;@a=@b;@b=@a+@c;@c=@a; + end + end +end From 25e588283d9c919656182664cda54b6f1e747dc9 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 14 Nov 2016 01:14:58 +0200 Subject: [PATCH 2/5] router --- hw2/app/app.rb | 7 +++++-- hw2/lib/router.rb | 13 ++++++++++++- hw2/spec/lib/router_spec.rb | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/hw2/app/app.rb b/hw2/app/app.rb index 2be87a8..e15700c 100644 --- a/hw2/app/app.rb +++ b/hw2/app/app.rb @@ -1,4 +1,7 @@ Application = Router.new do - get '/test', ->(env) { [200, {}, ['get test']] } - post '/test', ->(env) { [200, {}, ['post test']] } + get '/test1', ->(env) {[200, {}, ['Page 1']]} + get '/test2/test:id', ->(env) {[200, {}, ['Page 2']]} + get '/test3/:id', ->(env) {[200, {}, ['Page 3']]} + get '/test4/:id/test5', ->(env) {[200, {}, ['Page 4']]} + get '/test4/:id/test5/:id', ->(env) {[200, {}, ['Page 5']]} end diff --git a/hw2/lib/router.rb b/hw2/lib/router.rb index eb6e53d..811116a 100644 --- a/hw2/lib/router.rb +++ b/hw2/lib/router.rb @@ -1,6 +1,17 @@ class Router def call(env) - @routes[env['REQUEST_METHOD']][env['REQUEST_PATH']].call(env) + + unless env['REQUEST_PATH'] == "/favicon.ico" + current_env = ->(env) {[404, {}, ['404']]} + + @routes[env['REQUEST_METHOD']].each { |(key,value)| + key_to_reg_exp= key.gsub(/(?<=:)[^\/]+(?=($|\/))/,"[a-zA-Z0-9_]+").gsub(/\/:/,"/") + regular_value = Regexp.new /\A#{key_to_reg_exp}\Z/ + current_env = value if regular_value=~env['REQUEST_PATH'] + } + current_env.call(env) + end + end private diff --git a/hw2/spec/lib/router_spec.rb b/hw2/spec/lib/router_spec.rb index e53a275..0a0daa0 100644 --- a/hw2/spec/lib/router_spec.rb +++ b/hw2/spec/lib/router_spec.rb @@ -31,4 +31,28 @@ expect(subject.call(env)).to eq [200, {}, ['post test']] end end + + context 'when request is GET & PATH is /post/about_ruby' do + let(:env) {{ 'REQUEST_PATH' => '/post/about_ruby', 'REQUEST_METHOD' => 'GET'}} + + it 'matches request' do + expect(subject.call(env)).to eq [200, {}, ['post show page']] + end + end + + context 'when request is GET & PATH is /post/43' do + let(:env) {{ 'REQUEST_PATH' => '/post/43', 'REQUEST_METHOD' => 'GET'}} + + it 'matches request' do + expect(subject.call(env)).to eq [200, {}, ['post show page']] + end + end + + context 'when page not found' do + let(:env) {{ 'REQUEST_PATH' => '/post/about_ruby/43', 'REQUEST_METHOD' => 'GET'}} + + it 'matches request' do + expect(subject.call(env)).to eq [404, {}, ['404']] + end + end end From 8e689a0b05b745a553199a2016d07492040d5cb0 Mon Sep 17 00:00:00 2001 From: Serg Date: Sun, 27 Nov 2016 23:20:10 +0200 Subject: [PATCH 3/5] add --- controuter/.gitignore | 9 +++ controuter/.rspec | 2 + controuter/Gemfile | 4 ++ controuter/LICENSE.txt | 21 +++++++ controuter/README.md | 41 +++++++++++++ controuter/Rakefile | 2 + controuter/bin/console | 14 +++++ controuter/bin/setup | 8 +++ controuter/controuter-0.2.4.gem | Bin 0 -> 7680 bytes controuter/controuter.gemspec | 35 +++++++++++ controuter/lib/controuter.rb | 87 +++++++++++++++++++++++++++ controuter/lib/controuter/version.rb | 3 + controuter/spec/controuter_spec.rb | 41 +++++++++++++ controuter/spec/spec_helper.rb | 2 + hw3 | 1 + 15 files changed, 270 insertions(+) create mode 100644 controuter/.gitignore create mode 100644 controuter/.rspec create mode 100644 controuter/Gemfile create mode 100644 controuter/LICENSE.txt create mode 100644 controuter/README.md create mode 100644 controuter/Rakefile create mode 100755 controuter/bin/console create mode 100755 controuter/bin/setup create mode 100644 controuter/controuter-0.2.4.gem create mode 100644 controuter/controuter.gemspec create mode 100644 controuter/lib/controuter.rb create mode 100644 controuter/lib/controuter/version.rb create mode 100644 controuter/spec/controuter_spec.rb create mode 100644 controuter/spec/spec_helper.rb create mode 160000 hw3 diff --git a/controuter/.gitignore b/controuter/.gitignore new file mode 100644 index 0000000..0cb6eeb --- /dev/null +++ b/controuter/.gitignore @@ -0,0 +1,9 @@ +/.bundle/ +/.yardoc +/Gemfile.lock +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ diff --git a/controuter/.rspec b/controuter/.rspec new file mode 100644 index 0000000..8c18f1a --- /dev/null +++ b/controuter/.rspec @@ -0,0 +1,2 @@ +--format documentation +--color diff --git a/controuter/Gemfile b/controuter/Gemfile new file mode 100644 index 0000000..a4fe9a1 --- /dev/null +++ b/controuter/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in controuter.gemspec +gemspec diff --git a/controuter/LICENSE.txt b/controuter/LICENSE.txt new file mode 100644 index 0000000..6ef8aaf --- /dev/null +++ b/controuter/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Serg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/controuter/README.md b/controuter/README.md new file mode 100644 index 0000000..b225d1c --- /dev/null +++ b/controuter/README.md @@ -0,0 +1,41 @@ +# Controuter + +Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/controuter`. To experiment with that code, run `bin/console` for an interactive prompt. + +TODO: Delete this and the text above, and describe your gem + +## Installation + +Add this line to your application's Gemfile: + +```ruby +gem 'controuter' +``` + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install controuter + +## Usage + +TODO: Write usage instructions here + +## Development + +After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/controuter. + + +## License + +The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). + diff --git a/controuter/Rakefile b/controuter/Rakefile new file mode 100644 index 0000000..43022f7 --- /dev/null +++ b/controuter/Rakefile @@ -0,0 +1,2 @@ +require "bundler/gem_tasks" +task :default => :spec diff --git a/controuter/bin/console b/controuter/bin/console new file mode 100755 index 0000000..1e5f1f7 --- /dev/null +++ b/controuter/bin/console @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "controuter" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require "irb" +IRB.start diff --git a/controuter/bin/setup b/controuter/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/controuter/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/controuter/controuter-0.2.4.gem b/controuter/controuter-0.2.4.gem new file mode 100644 index 0000000000000000000000000000000000000000..0483e8bd0f3b28a5a020aa9e49466ee86fb22a31 GIT binary patch literal 7680 zcmeHLWmFX0)<#;o5u{r{7+_#%5ETZbJ0u2%knWfvlu&Sxk`YM>K|;C(q=xRHgb^ep zr9+yz-tYVI-u2$KzVFw2@B733XP;PSpS_=R*0a{L-RylKP>3%?#1V$`R~g`!0f9iA zfBIkcXD=ou4#E+W0EkIRiGw60Bya#?5;AC&_`mY;* z8vlR$|6AK1#r^5{-*w84AjZLn$eH5eO&Wdk7za>=o>)W9t3L3!fo@cx@NMbSP=Th@ zlpJ(2eUg%hZ5!(X9(zonY;x$X0L5%3;@iQVqIy%A{f* zIfkT08irE~LsNPe!aQfQS1SCD4U>*+pdlIFL)eN&Vus|Wja zrqO74QCl*}659*q@;Ww`j#)=j-vVa=g<%jFcq>@257VwW4Bx4-809&NV-r!+BVO{+ zJRXTFuU40pQQ}r8vryp`YANTe=M7uQCCp zHc>Vzg_-Nn9YKp#(%Rz<6FQINDH0<$C#pCw548P3$;dRjqxEVvoe3Ddz12B6Bs^(k zI(}zXdTemeNfqAoTHa81Bx|Y)&V{Kh678*w z(Jw>`<_eS0Th$GX*Zn|_89kEgHBpz+o(w0I8DJyft{n@5Sw-SR)(fqb1LGxeRW4}@ zd*q9>m66Bt7a@CLj3~B-V32K(Uz=`N@ikkd@`5_Vl|p3!5n6D&majYG6{oaZBsjq1 z{7sOvkp1Q*#nkr|cI(}U9|VV)Q|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^YOxd{`z0SS#?Z=j|Tk2-m3&NUun8r-(x`N*~#>;qfIb zXEP+`$++uHO$Y=qCV@4!wQlz&;8&(BvOLqPz*KR4VbV&Q;G60t$KCK{3p%?2%OfSr Wz44y_`}ZBc5%`V3Zv_5E2>b(->z+>l literal 0 HcmV?d00001 diff --git a/controuter/controuter.gemspec b/controuter/controuter.gemspec new file mode 100644 index 0000000..895cb38 --- /dev/null +++ b/controuter/controuter.gemspec @@ -0,0 +1,35 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'controuter/version' + +Gem::Specification.new do |spec| + spec.name = "controuter" + spec.version = Controuter::VERSION + spec.authors = ["Serg"] + spec.email = ["yelagins@gmail.com"] + spec.files = ["lib/controuter.rb"] + spec.summary = %q{gem.} + spec.homepage = "http://sadasdasd.com" + spec.license = "MIT" + + # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' + # to allow pushing to a single host or delete this section to allow pushing to any host. + # if spec.respond_to?(:metadata) + # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserdsfver.com'" + # else + # raise "RubyGems 2.0 or newer is required to protect against " \ + # "public gem pushes." + # end + + spec.files = `git ls-files -z`.split("\x0").reject do |f| + f.match(%r{^(test|spec|features)/}) + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + spec.add_development_dependency "bundler", "~> 1.13" + spec.add_development_dependency "rake", "~> 10.0" + spec.add_development_dependency "rspec", "~> 3.0" +end diff --git a/controuter/lib/controuter.rb b/controuter/lib/controuter.rb new file mode 100644 index 0000000..a45101c --- /dev/null +++ b/controuter/lib/controuter.rb @@ -0,0 +1,87 @@ +require "controuter/version" + +module Controuter + + class Controller + RESPONSE_TYPES = { + text: ['text/html', ->(c) { c.to_s }], + json: ['application/json', ->(c) { Oj.dump(c) }] + }.freeze + + def call(env) + @env = env + @request = Rack::Request.new(env) + #@request.params.merge!(env['router.params'] || {}) + send(@action_name) + [200, @response_headers, [@response_body]] + end + + def self.action(action_name) + proc { |env| new(action_name).call(env) } + end + + private + attr_reader :request + + def initialize(action_name) + @action_name = action_name + end + + def params + request.params + end + + def response(type, content) + @response_headers ||= {} + @response_headers.merge!('Content-Type' => RESPONSE_TYPES[type][0]) + @response_body = RESPONSE_TYPES[type][1].call(content) + end + end + + class Router + def call(env) + find(env) + end + + private + + def initialize(&block) + @routes = {} + instance_exec(&block) + end + + def find(env) + current_env = ->(env) {[404, {}, ['404']]} + @routes[env['REQUEST_METHOD']].each { |(key,value)| + regular_value = Regexp.new('\A' + key.gsub(/:[\w-]+/, '[\w-]+') + '\Z') + current_env = get_controller_action(value) if regular_value=~env['REQUEST_PATH'] + } + current_env.call(env) + end + def get_controller_action(str) + controller_name, action_name = str.split('#') + controller_name = to_upper_camel_case(controller_name) + Kernel.const_get(controller_name).send(:action, action_name) + end + + def to_upper_camel_case(str) + str + .split('/') + .map { |part| part.split('_').map(&:capitalize).join } + .join('::') + 'Controller' + 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 +end diff --git a/controuter/lib/controuter/version.rb b/controuter/lib/controuter/version.rb new file mode 100644 index 0000000..be5448c --- /dev/null +++ b/controuter/lib/controuter/version.rb @@ -0,0 +1,3 @@ +module Controuter + VERSION = "0.2.4" +end diff --git a/controuter/spec/controuter_spec.rb b/controuter/spec/controuter_spec.rb new file mode 100644 index 0000000..fe273bf --- /dev/null +++ b/controuter/spec/controuter_spec.rb @@ -0,0 +1,41 @@ +require "spec_helper" +require "./lib/controuter" + +describe Controuter do + it "has a version number" do + expect(Controuter::VERSION).not_to be nil + end +end + +describe Controuter::Router do + subject do + Controuter::Router.new do + get '/test', 'tests#show' + end + end + + context "when page not found" do + let(:env) { { 'REQUEST_PATH' => '/wrong-page', 'REQUEST_METHOD' => 'GET'} } + it 'matches request' do + expect(subject.call(env)).to eq [404, {}, ['404']] + end + end +end + +describe Controuter::Router do + subject do + Controuter::Router.new do + get '/page', 'tests#show' + end + + TestsController = ->(_var){"show"} + end + + context "simulating controller action" do + let(:env) { { 'REQUEST_PATH' => '/page', 'REQUEST_METHOD' => 'GET'} } + + it do + expect(subject.call(env)).to eq "show" + end + end +end diff --git a/controuter/spec/spec_helper.rb b/controuter/spec/spec_helper.rb new file mode 100644 index 0000000..ab6277f --- /dev/null +++ b/controuter/spec/spec_helper.rb @@ -0,0 +1,2 @@ +$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) +require "controuter" diff --git a/hw3 b/hw3 new file mode 160000 index 0000000..8b11c91 --- /dev/null +++ b/hw3 @@ -0,0 +1 @@ +Subproject commit 8b11c9131290409ef90864eb535abdde930cfc47 From 8ac31401e7a1ab47c370788272143e3c2ab7b8e1 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 28 Nov 2016 09:43:51 +0200 Subject: [PATCH 4/5] add --- hw3 | 1 - hw3/.travis.yml | 10 +++++ hw3/Gemfile | 7 +++ hw3/Gemfile.lock | 32 ++++++++++++++ hw3/app/app.rb | 22 ++++++++++ hw3/before-deploy.sh | 1 + hw3/config.ru | 3 ++ hw3/lib/controller.rb | 35 +++++++++++++++ hw3/lib/router.rb | 47 ++++++++++++++++++++ hw3/main.rb | 4 ++ hw3/spec/lib/router_spec.rb | 28 ++++++++++++ hw3/spec/spec_helper.rb | 28 ++++++++++++ hw3/travis.log | 86 +++++++++++++++++++++++++++++++++++++ 13 files changed, 303 insertions(+), 1 deletion(-) delete mode 160000 hw3 create mode 100644 hw3/.travis.yml create mode 100644 hw3/Gemfile create mode 100644 hw3/Gemfile.lock create mode 100644 hw3/app/app.rb create mode 100644 hw3/before-deploy.sh create mode 100644 hw3/config.ru create mode 100644 hw3/lib/controller.rb create mode 100644 hw3/lib/router.rb create mode 100644 hw3/main.rb create mode 100644 hw3/spec/lib/router_spec.rb create mode 100644 hw3/spec/spec_helper.rb create mode 100644 hw3/travis.log diff --git a/hw3 b/hw3 deleted file mode 160000 index 8b11c91..0000000 --- a/hw3 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8b11c9131290409ef90864eb535abdde930cfc47 diff --git a/hw3/.travis.yml b/hw3/.travis.yml new file mode 100644 index 0000000..8de0cce --- /dev/null +++ b/hw3/.travis.yml @@ -0,0 +1,10 @@ +language: ruby +rvm: +- 2.3.1 +script: +- bash before-deploy.sh +deploy: + provider: heroku + api_key: + secure: NNdlTVbKxcM0pdHxn79+1zRyDx3umKAa9d3Ka09TLUQkjfNQyINi6dtbwZtfmduOIYlWMTy0ZSfkOzZnAvcwGO9b4unmdnE8FIvxDiKzTH15S9I4BePfB+5Utpa4mbN0UnB8v0GcW1b9+fwnHNQsdwNdr08AvPENEoSMbIjyhAK2IvlQHlqwKR72JvDRfRxwwG7S1s6+pSpAHh9Hl6BBPzCwn5Uf4Lt+Vwwcb9MPQPwpWyXzQk+LF2QIAO23oqb0l6/jcCaEcSw8J2rDPh1gCZnBSDj2JuGtA8URxgemFMPrzaPNDkyo0NZUDmdzHjGFyeD89C/fZ+N5704pg/Osuxl8DP0J/+xavDO/eY9gdskQhfjq91kN2FiBu7/P35pm963YzCwDqlZysxA8MYniR3ST1C9D5q3eluNY0vFEWaHUhi90URuVy3tuLl1b+cOHQ+TKRwYuisyaOPhQF76ERnjHm+jr5k6NIqD+sbBJ5D+uk0dpc4/y4FOYyxnuZ5JGK/TrmMXfCPs1yKgk6KwBa0/RNolIAQ1dmSrjBZxnZNxS0kLXGPrtYAZCrP0xvcy+KY1Wi9KfCtQ1Xao/ASanPxW24/IXiTczXxdsxqxQowkLZRBNBIDLDsev8MKMD1Mg9yeH3wDa22ahTR0i7SR/PYpiURf13g54OsmUrElh998= + app: home-work-project diff --git a/hw3/Gemfile b/hw3/Gemfile new file mode 100644 index 0000000..b10cdf7 --- /dev/null +++ b/hw3/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true +source "https://rubygems.org" + +gem "rack" +gem "rspec" +gem "oj" +gem "controuter" #custom gem diff --git a/hw3/Gemfile.lock b/hw3/Gemfile.lock new file mode 100644 index 0000000..59fdc7c --- /dev/null +++ b/hw3/Gemfile.lock @@ -0,0 +1,32 @@ +GEM + remote: https://rubygems.org/ + specs: + controuter (0.2.4) + diff-lcs (1.2.5) + oj (2.17.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 + controuter + oj + rack + rspec + +BUNDLED WITH + 1.13.6 diff --git a/hw3/app/app.rb b/hw3/app/app.rb new file mode 100644 index 0000000..f670299 --- /dev/null +++ b/hw3/app/app.rb @@ -0,0 +1,22 @@ +Controller = Controuter::Controller +Router = Controuter::Router + +class TestsController < Controller + def home + response(:text, "

Home Page


Page 1
Page 2") + end + + def show + response(:text, request.path_info) + end + + def test + response(:text, "Action 'test'") + end +end + +Application = Router.new do + get '/', 'tests#home' + get '/pages/1', 'tests#show' + get '/pages/2', 'tests#test' +end diff --git a/hw3/before-deploy.sh b/hw3/before-deploy.sh new file mode 100644 index 0000000..1bc4f5e --- /dev/null +++ b/hw3/before-deploy.sh @@ -0,0 +1 @@ +bundle install && bundle exec rspec diff --git a/hw3/config.ru b/hw3/config.ru new file mode 100644 index 0000000..c2e95d5 --- /dev/null +++ b/hw3/config.ru @@ -0,0 +1,3 @@ +require './main' + +run Application diff --git a/hw3/lib/controller.rb b/hw3/lib/controller.rb new file mode 100644 index 0000000..597e518 --- /dev/null +++ b/hw3/lib/controller.rb @@ -0,0 +1,35 @@ +class Controller + RESPONSE_TYPES = { + text: ['text/html', ->(c) { c.to_s }], + json: ['application/json', ->(c) { Oj.dump(c) }] + }.freeze + + def call(env) + @env = env + @request = Rack::Request.new(env) + #@request.params.merge!(env['router.params'] || {}) + send(@action_name) + [200, @response_headers, [@response_body]] + end + + def self.action(action_name) + proc { |env| new(action_name).call(env) } + end + +private + attr_reader :request + + def initialize(action_name) + @action_name = action_name + end + + def params + request.params + end + + def response(type, content) + @response_headers ||= {} + @response_headers.merge!('Content-Type' => RESPONSE_TYPES[type][0]) + @response_body = RESPONSE_TYPES[type][1].call(content) + end +end diff --git a/hw3/lib/router.rb b/hw3/lib/router.rb new file mode 100644 index 0000000..5496f20 --- /dev/null +++ b/hw3/lib/router.rb @@ -0,0 +1,47 @@ +class Router + def call(env) + find(env) + end + +private + + def initialize(&block) + @routes = {} + instance_exec(&block) + end + + def find(env) + current_env = ->(env) {[404, {}, ['404']]} + @routes[env['REQUEST_METHOD']].each { |(key,value)| + regular_value = Regexp.new('\A' + key.gsub(/:[\w-]+/, '[\w-]+') + '\Z') + current_env = get_controller_action(value) if regular_value=~env['REQUEST_PATH'] + } + current_env.call(env) + end + + def get_controller_action(str) + controller_name, action_name = str.split('#') + controller_name = to_upper_camel_case(controller_name) + Kernel.const_get(controller_name).send(:action, action_name) + end + + def to_upper_camel_case(str) + str + .split('/') + .map { |part| part.split('_').map(&:capitalize).join } + .join('::') + 'Controller' + 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/hw3/main.rb b/hw3/main.rb new file mode 100644 index 0000000..aee994b --- /dev/null +++ b/hw3/main.rb @@ -0,0 +1,4 @@ +require 'rack' +require 'controuter' +require './app/app' +require 'oj' diff --git a/hw3/spec/lib/router_spec.rb b/hw3/spec/lib/router_spec.rb new file mode 100644 index 0000000..ffd8604 --- /dev/null +++ b/hw3/spec/lib/router_spec.rb @@ -0,0 +1,28 @@ +require './main' + +RSpec.describe Router do + + context 'when page not found' do + let(:env) { { 'REQUEST_PATH' => '/wrong-page', 'REQUEST_METHOD' => 'GET'} } + + it 'matches request' do + expect(Application.call(env)).to eq [404, {}, ['404']] + end + end + + context 'when request is GET & PATH is home page' do + let(:env) {{ 'REQUEST_PATH' => '/', 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/'}} + + it 'matches request' do + expect(Application.call(env)).to eq [200, {"Content-Type"=>"text/html"}, ["

Home Page


Page 1
Page 2"]] + end + end + + context 'when request is GET & PATH is some page' do + let(:env) {{ 'REQUEST_PATH' => '/pages/1', 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => '/pages/1'}} + + it 'matches request' do + expect(Application.call(env)).to eq [200, {"Content-Type"=>"text/html"}, ["/pages/1"]] + end + end +end diff --git a/hw3/spec/spec_helper.rb b/hw3/spec/spec_helper.rb new file mode 100644 index 0000000..ef0fca6 --- /dev/null +++ b/hw3/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 diff --git a/hw3/travis.log b/hw3/travis.log new file mode 100644 index 0000000..b5d1889 --- /dev/null +++ b/hw3/travis.log @@ -0,0 +1,86 @@ +Command line: +"travis setup releases" + + + ________ __ + / | / | + ########/ ______ ______ __ __ ##/ _______ + ## | / \ / \ / \ / | / | / | + ## | /###### | ###### | ## \ /##/ ## | /#######/ + ## | ## | ##/ / ## | ## /##/ ## | ## \ + ## | ## | /####### | ## ##/ ## | ###### | + ## | ## | ## ## | ###/ ## | / ##/ + ##/ ##/ #######/ #/ ##/ #######/ + + TRajectory Analyzer and VISualizer - Open-source freeware under GNU GPL v3 + + Copyright (c) Martin Brehm (2009-2015) + Martin Thomas (2012-2015) + Barbara Kirchner (2009-2015) + University of Leipzig / University of Bonn. + + http://www.travis-analyzer.de + + Please cite: + M. Brehm and B. Kirchner, J. Chem. Inf. Model. 2011, 51 (8), pp 2007-2023. + + There is absolutely no warranty on any results obtained from TRAVIS. + + # Running on yelaginspc at Sun Nov 27 22:52:30 2016 (PID 26116). + # Running in /home/yelagins/rails-projects/homework/kottans_2016_homeworks/hw3 + # Source code version: Nov 07 2015. + # Compiled at Nov 17 2015 23:12:21. + # Compiler version: 5.2.1 20151028 + # Target platform: Linux + # Compile flags: DEBUG_ARRAYS + # Machine: int=4b, long=8b, addr=8b, 0xA0B0C0D0=D0,C0,B0,A0. + # User home: /home/yelagins + # Exe path: /usr/bin/travis + # Input from terminal, Output to terminal + + >>> Please use a color scheme with dark background or specify "-nocolor"! <<< + + Loading configuration from /home/yelagins/.travis.conf ... + +Unknown parameter: "setup". + + List of supported command line options: + + -p Loads position data from the specified trajectory file. + The file format may be *.xyz, *.pdb, *.lmp (Lammps), HISTORY (DLPOLY), or *.prmtop/*.mdcrd (Amber). + -i Reads input from the specified text file. + + -config Load the specified configuration file. + -stream Treats input trajectory as a stream (e.g. named pipe): No fseek, etc. + -showconf Shows a tree structure of the configuration file. + -writeconf Writes the default configuration file, including all defines values. + + -verbose Show detailed information about what's going on. + -nocolor Executes TRAVIS in monochrome mode. + -dimcolor Uses dim instead of bright colors. + + -credits Display a list of persons who contributed to TRAVIS. + -help, -? Shows this help. + + If only one argument is specified, it is assumed to be the name of a trajectory file. + If argument is specified at all, TRAVIS asks for the trajectory file to open. + + + Note: To show a list of all persons who contributed to TRAVIS, + please add "-credits" to your command line arguments, or set the + variable "SHOWCREDITS" to "TRUE" in your travis.conf file. + + Source code from other projects used in TRAVIS: + - lmfit from Joachim Wuttke + - kiss_fft from Mark Borgerding + - voro++ from Chris Rycroft + + http://www.travis-analyzer.de + + Please cite: + + * "TRAVIS - A Free Analyzer and Visualizer for Monte Carlo and Molecular Dynamics Trajectories", + M. Brehm, B. Kirchner; J. Chem. Inf. Model. 2011, 51 (8), pp 2007-2023. + +*** The End *** + From b7ee0ab5a8b5919a9c3d34496f24e0fd3ce96cec Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 28 Nov 2016 09:49:18 +0200 Subject: [PATCH 5/5] remove travis log file --- hw3/travis.log | 86 -------------------------------------------------- 1 file changed, 86 deletions(-) delete mode 100644 hw3/travis.log diff --git a/hw3/travis.log b/hw3/travis.log deleted file mode 100644 index b5d1889..0000000 --- a/hw3/travis.log +++ /dev/null @@ -1,86 +0,0 @@ -Command line: -"travis setup releases" - - - ________ __ - / | / | - ########/ ______ ______ __ __ ##/ _______ - ## | / \ / \ / \ / | / | / | - ## | /###### | ###### | ## \ /##/ ## | /#######/ - ## | ## | ##/ / ## | ## /##/ ## | ## \ - ## | ## | /####### | ## ##/ ## | ###### | - ## | ## | ## ## | ###/ ## | / ##/ - ##/ ##/ #######/ #/ ##/ #######/ - - TRajectory Analyzer and VISualizer - Open-source freeware under GNU GPL v3 - - Copyright (c) Martin Brehm (2009-2015) - Martin Thomas (2012-2015) - Barbara Kirchner (2009-2015) - University of Leipzig / University of Bonn. - - http://www.travis-analyzer.de - - Please cite: - M. Brehm and B. Kirchner, J. Chem. Inf. Model. 2011, 51 (8), pp 2007-2023. - - There is absolutely no warranty on any results obtained from TRAVIS. - - # Running on yelaginspc at Sun Nov 27 22:52:30 2016 (PID 26116). - # Running in /home/yelagins/rails-projects/homework/kottans_2016_homeworks/hw3 - # Source code version: Nov 07 2015. - # Compiled at Nov 17 2015 23:12:21. - # Compiler version: 5.2.1 20151028 - # Target platform: Linux - # Compile flags: DEBUG_ARRAYS - # Machine: int=4b, long=8b, addr=8b, 0xA0B0C0D0=D0,C0,B0,A0. - # User home: /home/yelagins - # Exe path: /usr/bin/travis - # Input from terminal, Output to terminal - - >>> Please use a color scheme with dark background or specify "-nocolor"! <<< - - Loading configuration from /home/yelagins/.travis.conf ... - -Unknown parameter: "setup". - - List of supported command line options: - - -p Loads position data from the specified trajectory file. - The file format may be *.xyz, *.pdb, *.lmp (Lammps), HISTORY (DLPOLY), or *.prmtop/*.mdcrd (Amber). - -i Reads input from the specified text file. - - -config Load the specified configuration file. - -stream Treats input trajectory as a stream (e.g. named pipe): No fseek, etc. - -showconf Shows a tree structure of the configuration file. - -writeconf Writes the default configuration file, including all defines values. - - -verbose Show detailed information about what's going on. - -nocolor Executes TRAVIS in monochrome mode. - -dimcolor Uses dim instead of bright colors. - - -credits Display a list of persons who contributed to TRAVIS. - -help, -? Shows this help. - - If only one argument is specified, it is assumed to be the name of a trajectory file. - If argument is specified at all, TRAVIS asks for the trajectory file to open. - - - Note: To show a list of all persons who contributed to TRAVIS, - please add "-credits" to your command line arguments, or set the - variable "SHOWCREDITS" to "TRUE" in your travis.conf file. - - Source code from other projects used in TRAVIS: - - lmfit from Joachim Wuttke - - kiss_fft from Mark Borgerding - - voro++ from Chris Rycroft - - http://www.travis-analyzer.de - - Please cite: - - * "TRAVIS - A Free Analyzer and Visualizer for Monte Carlo and Molecular Dynamics Trajectories", - M. Brehm, B. Kirchner; J. Chem. Inf. Model. 2011, 51 (8), pp 2007-2023. - -*** The End *** -