This repository was archived by the owner on May 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
hw1/hw2/hw3-gem-travis #16
Open
lysenko-sergey-developer
wants to merge
15
commits into
denysxftr:master
Choose a base branch
from
lysenko-sergey-developer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e3337f6
write Fibonacci class
invalid-email-address 8c40f04
implement Fibonacci class with yield
invalid-email-address bbac300
Merge remote-tracking branch 'denis_branch/master'
invalid-email-address bbe4499
add router tests; re-write router to custom path
lysenko-sergey-developer 4f9964b
copied controller
lysenko-sergey-developer f1f0ced
add rspec test to controller
lysenko-sergey-developer 0c9872a
add accompanying comments to router and controller
lysenko-sergey-developer 7b59624
add hw3/hw4
lysenko-sergey-developer b09568f
Create README.md
lysenko-sergey-developer d1d758a
add Rakefile
lysenko-sergey-developer 78dc8d8
Merge branch 'master' of github.com:lysenko-sergei-developer/kottans_…
lysenko-sergey-developer 1ea8784
add check.sh file
lysenko-sergey-developer 11631d6
some fix in rakefile
lysenko-sergey-developer 65de5f0
some fix
lysenko-sergey-developer 0b7439d
some fix
lysenko-sergey-developer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| sudo: false | ||
| language: ruby | ||
| rvm: | ||
| - 2.3.1 | ||
| - 2.3.0 | ||
| script: | ||
| - bash check.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # kottans_2016_homeworks | ||
|
|
||
| gem https://github.com/lysenko-sergei-developer/lysenko_kottans_rack | ||
| travis https://travis-ci.org/lysenko-sergei-developer/lysenko_kottans_rack |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| x, y = 0, 1 | ||
|
|
||
| @n.times do | ||
| z = (x + y) | ||
| x = y | ||
| y = z | ||
| yield x | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ source "https://rubygems.org" | |
|
|
||
| gem "rack" | ||
| gem "rspec" | ||
| gem "oj" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,23 @@ | ||
| Application = Router.new do | ||
| get '/test', ->(env) { [200, {}, ['get test']] } | ||
| post '/test', ->(env) { [200, {}, ['post test']] } | ||
| class TestController < Controller #inherit Controller class | ||
| def show # method show for return params | ||
| response(:json, params) | ||
| end | ||
|
|
||
| def test # method test for return request method | ||
| response(:text, "Request method: #{request.request_method}") # params = "Request method: #{request.request_method}", | ||
| end | ||
| end | ||
|
|
||
| Application = Router.new do # create new instance of Router | ||
|
|
||
| # get '/test', ->(env) { [200, {}, ['get test']] } | ||
| # post '/test', ->(env) { [200, {}, ['post test']] } | ||
|
|
||
| get '/post/:name/:other_one', 'test#show' | ||
| get '/test', 'test#test' # "#{request.request_method} = get" | ||
|
|
||
| #get '/users/:id', ->(env) { [200, {}, ['post /users/:id']] } | ||
| # get '/users/:id/comments/:comment/rating/:rating', ->(env) { [200, {}, ['post with users comment rating']] } | ||
| # get '/users/articles/:title', ->(env) { [200, {}, ['post with user articles title']] } | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| class Controller | ||
| RESPONSE_TYPES = { # mini cheat, for work with response | ||
| text: ['text/plain', ->(c) { c.to_s }], # key where stored text params data | ||
| json: ['application/json', ->(c) { Oj.dump(c) }] # key where stored json | ||
| }.freeze # freeze this constant, for avoid random changing | ||
|
|
||
| def call(env) # env standart Rack params | ||
| @env = env # writed env into instance @env | ||
| @request = Rack::Request.new(env) # writed new rack request into instance @request | ||
| @request.params.merge!(env['router.params'] || {}) # merge with router.params if it exist | ||
| send(@action_name) # send @action_name | ||
|
|
||
| [200, @response_headers, [@response_body]] # return array with route | ||
| end | ||
|
|
||
| def self.action(action_name) # call action method for self object, action_name = call method name | ||
| proc { |env| new(action_name).call(env) } # proc create new action and call it | ||
| end | ||
|
|
||
| private | ||
| attr_reader :request # create getter for request | ||
|
|
||
| def initialize(action_name) # init with, call method name | ||
| @action_name = action_name # writed action_name into instance @action_name | ||
| end | ||
|
|
||
| def params # params return request params | ||
| request.params | ||
| end | ||
|
|
||
| def response(type, content) #response in header and body | ||
| @response_headers ||= {} # if @response_headers empty assign hash | ||
| @response_headers.merge!('Content-Type' => RESPONSE_TYPES[type][0]) # merge @response_headers with 'Content-Type' => RESPONSE_TYPES[text]['some text'] | ||
| @response_body = RESPONSE_TYPES[type][1].call(content) # assign @response_body to RESPONSE_TYPES[json][some json] and call content arguments | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,70 @@ | ||
| class Router | ||
| def call(env) | ||
| @routes[env['REQUEST_METHOD']][env['REQUEST_PATH']].call(env) | ||
| def call(env) # call method with env params | ||
| find_route(env).call(env) # call env | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def initialize(&block) | ||
| @routes = {} | ||
| instance_exec(&block) | ||
| def initialize(&block) # initialize of code blok | ||
| @routes = [] # set to null @routes | ||
| instance_exec(&block) #call &block | ||
| end | ||
|
|
||
| def get(path, rack_app) | ||
| def find_route(env) # check if route exist | ||
| @routes.each do |route| # in @routes all route | ||
| # check if requst method of call and requst method of route matche | ||
| # and requst path of call math with route path | ||
| if env['REQUEST_METHOD'] == route[:method] && env['REQUEST_PATH'] =~ route[:regexp] | ||
| # if all matches, assing to router.params extract params | ||
| env['router.params'] = extract_params(route[:pattern], env['REQUEST_PATH']) | ||
| return route[:app] # return applocation | ||
| end | ||
| end | ||
|
|
||
| return ->(_env) { [404, {}, ['not found']] } # if not exist return -> 404 | ||
| end | ||
|
|
||
| def get(path, rack_app) # get request | ||
| match('GET', path, rack_app) | ||
| end | ||
|
|
||
| def post(path, rack_app) | ||
| def post(path, rack_app) # post request | ||
| match('POST', path, rack_app) | ||
| end | ||
|
|
||
| def match(http_method, path, rack_app) | ||
| @routes[http_method] ||= {} | ||
| @routes[http_method][path] = rack_app | ||
| def match(http_method, path, rack_app) # match | ||
| rack_app = get_controller_action(rack_app) if rack_app.is_a?(String) | ||
| @routes << { pattern: path, app: rack_app, regexp: path_to_regexp(path), method: http_method } | ||
| end | ||
|
|
||
| def get_controller_action(str) | ||
| controller_name, action_name = str.split('#') # tests#show => ['tests', 'show'] | ||
| controller_name = to_upper_camel_case(controller_name)# ['tests', 'show'] => ['TestsController', 'show'] | ||
| Kernel.const_get(controller_name).send(:action, action_name) | ||
| end | ||
|
|
||
| def to_upper_camel_case(str) # up | ||
| str # 'public_pages/tests' => PublicPages::TestsController | ||
| .split('/') # ['public_pages', 'test'] | ||
| .map { |part| part.split('_').map(&:capitalize).join } # ['PublicPages', 'Test'] | ||
| .join('::') + 'Controller' | ||
| end | ||
|
|
||
|
|
||
| # /post/:name | ||
| def path_to_regexp(path) # return processing path | ||
| Regexp.new('\A' + path.gsub(/:[\w-]+/, '[\w-]+') + '\Z') | ||
| end | ||
|
|
||
| # /post/:name | ||
| # /post/test_one | ||
| # { name: 'test_one' } | ||
| def extract_params(pattern, path) | ||
| pattern | ||
| .split('/') # ['post', ':name'] | ||
| .zip(path.split('/')) # [['post', 'post'],[':name', 'post']] | ||
| .reject { |e| e.first == e.last } # [[':name', 'post']] | ||
| .map { |e| [e.first[1..-1], e.last] } # [['name', 'post']] | ||
| .to_h # { name = > post } | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| require 'rack' | ||
| require 'oj' | ||
|
|
||
| require './lib/controller' | ||
| require './lib/router' | ||
| require './app/app' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| RSpec.describe Controller do | ||
| let(:controller) do | ||
|
|
||
| Class.new(Controller) do | ||
| def text_action | ||
| response(:text, 'test text') | ||
| end | ||
|
|
||
| def json_action | ||
| response(:json, params) | ||
| end | ||
|
|
||
| def data_params | ||
| response(:json, some_key: params['some_info']) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'shows request status' do | ||
| it 'for action with text type' do | ||
| expect(controller.action(:text_action).call(Rack::MockRequest.env_for('/test_in_text'))) | ||
| .to eq([200, { 'Content-Type' => 'text/plain' }, ['test text']]) | ||
| end | ||
|
|
||
| it 'for action with json type' do | ||
| expect(controller.action(:json_action).call(Rack::MockRequest.env_for('/test_in_json'))) | ||
| .to eq([200, { 'Content-Type' => 'application/json' }, ["{}"]]) | ||
| end | ||
| end | ||
|
|
||
| it 'shows readed param from address line to params hash' do | ||
| expect(controller.action(:data_params).call(Rack::MockRequest.env_for('/message?some_info=key'))) | ||
| .to eq([200, { 'Content-Type' => 'application/json' }, ["{\":some_key\":\"key\"}"]]) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule hw3
added at
fd37b1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can omit
returnhere