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
hw2 superedriver #33
Open
superedriver
wants to merge
11
commits into
denysxftr:master
Choose a base branch
from
superedriver: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
hw2 superedriver #33
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7ec6dc
Made home task #1 refactor
superedriver 0db20ef
made the 2-nd hw
superedriver 638ba45
made hw2
superedriver 33d231c
Made router not static for routes
superedriver 2368cce
Added some description
superedriver a2359eb
Modified some description
superedriver 0633d16
Remade structure
superedriver 3935274
Remade structure
superedriver cc8ed7f
Added hw3 and hw4
superedriver eaf564f
Merge branch 'new_structure'
superedriver 6878b48
Fixed pathes for require
superedriver 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .idea/ |
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,24 @@ | ||
| class Fibonacci | ||
| attr_reader :result_array | ||
| include Enumerable | ||
|
|
||
| def each | ||
| result_array.each{ |item| yield item } | ||
| self | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def initialize(length) | ||
| raise TypeError, "Expected argument to be Integer" unless [Fixnum, Bignum].include? length.class | ||
| raise ArgumentError, "Expected argument >= 1. Got #{length}" if length < 1 | ||
|
|
||
| previous_item = next_item = 1 | ||
| @result_array = (1..length).inject([]) do |result| | ||
| result << previous_item | ||
| previous_item, next_item = next_item, previous_item | ||
| next_item += previous_item | ||
| result | ||
| 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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| # frozen_string_literal: true | ||
| source "https://rubygems.org" | ||
|
|
||
| gem "pry" | ||
| 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,14 @@ | ||
| class TestsController < Controller | ||
| def show | ||
| response(:json, params) | ||
| end | ||
|
|
||
| def test | ||
| response(:text, "Required method #{request.request_method}") | ||
| end | ||
| end | ||
|
|
||
| Application = Router.new do | ||
| get '/test', ->(env) { [200, {}, ['get test']] } | ||
| post '/test', ->(env) { [200, {}, ['post test']] } | ||
| get '/post/:name/:other_one', 'tests#show' | ||
| get '/test', 'tests#test' | ||
| 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,35 @@ | ||
| class Controller | ||
| RESPONSE_TYPES = { | ||
| text: ['text/plain', -> (c) { c.to_s }], | ||
| json: ['application/json', -> (c) { Oj.dump(c) }], | ||
| } | ||
|
|
||
| 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 |
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,2 +1,6 @@ | ||
| require 'pry' | ||
| 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,89 @@ | ||
| require 'spec_helper' | ||
|
|
||
| RSpec.describe Controller do | ||
| let(:controller) do | ||
| Class.new(described_class) do | ||
| def test_action | ||
| response(:text, 'simple test') | ||
| end | ||
|
|
||
| def text_action | ||
| response(:text, params.inspect) | ||
| end | ||
|
|
||
| def json_action | ||
| response(:json, params) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#action' do | ||
| it 'returns proc' do | ||
| expect(controller.action(:test)).to be_a(Proc) | ||
| end | ||
|
|
||
| it 'generated proc calls action' do | ||
| expect(controller.action(:test_action).call(Rack::MockRequest.env_for('/'))) | ||
| .to eq([200, { 'Content-Type' => 'text/plain' }, ['simple test']]) | ||
| end | ||
| end | ||
|
|
||
| describe 'request processing' do | ||
| subject do | ||
| controller | ||
| .action(action) | ||
| .call(Rack::MockRequest.env_for('/?a=b&e=r').merge!(router_params)) | ||
| end | ||
|
|
||
| let(:router_params) { {} } | ||
|
|
||
| context 'when text response' do | ||
| context 'response with simple text' do | ||
| let(:action) { :test_action } | ||
|
|
||
| it 'successfully responds' do | ||
| expect(subject) | ||
| .to eq([200, {'Content-Type'=>'text/plain'}, ['simple test']]) | ||
| end | ||
| end | ||
|
|
||
| context 'response with params' do | ||
| let(:action) { :text_action } | ||
|
|
||
| it 'successfully responds' do | ||
| expect(subject) | ||
| .to eq([200, {"Content-Type"=>"text/plain"}, ["{\"a\"=>\"b\", \"e\"=>\"r\"}"]]) | ||
| end | ||
| end | ||
| let(:action) { :test_action } | ||
|
|
||
| it 'successfully responds' do | ||
| expect(subject) | ||
| .to eq([200, {"Content-Type"=>"text/plain"}, ["simple test"]]) | ||
| end | ||
| end | ||
|
|
||
| context 'when json response' do | ||
| let(:action) { :json_action } | ||
|
|
||
| it 'successfully responds' do | ||
| expect(subject) | ||
| .to eq([200, {"Content-Type"=>"application/json"}, ["{\"a\":\"b\",\"e\":\"r\"}"]]) | ||
| end | ||
| end | ||
|
|
||
| context 'when has router params' do | ||
| let(:router_params) { { 'router.params' => { 'param' => 'value' } } } | ||
| let(:action) { :json_action } | ||
|
|
||
| it 'successfully responds' do | ||
| expect(subject) | ||
| .to eq([ | ||
| 200, | ||
| {"Content-Type"=>"application/json"}, | ||
| ["{\"a\":\"b\",\"e\":\"r\",\"param\":\"value\"}"]] | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
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.
you can you percent literals https://github.com/bbatsov/ruby-style-guide#percent-literals