Skip to content
This repository was archived by the owner on May 1, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions controuter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions controuter/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
4 changes: 4 additions & 0 deletions controuter/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in controuter.gemspec
gemspec
21 changes: 21 additions & 0 deletions controuter/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
41 changes: 41 additions & 0 deletions controuter/README.md
Original file line number Diff line number Diff line change
@@ -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).

2 changes: 2 additions & 0 deletions controuter/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"
task :default => :spec
14 changes: 14 additions & 0 deletions controuter/bin/console
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions controuter/bin/setup
Original file line number Diff line number Diff line change
@@ -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
Binary file added controuter/controuter-0.2.4.gem
Binary file not shown.
35 changes: 35 additions & 0 deletions controuter/controuter.gemspec
Original file line number Diff line number Diff line change
@@ -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
87 changes: 87 additions & 0 deletions controuter/lib/controuter.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions controuter/lib/controuter/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Controuter
VERSION = "0.2.4"
end
41 changes: 41 additions & 0 deletions controuter/spec/controuter_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every class/module tests should be separated in files
like /spec/lib/router_spec.rb
It's a good practice

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
2 changes: 2 additions & 0 deletions controuter/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "controuter"
11 changes: 11 additions & 0 deletions hw1/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions hw2/app/app.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion hw2/lib/router.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 24 additions & 0 deletions hw2/spec/lib/router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions hw3/.travis.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions hw3/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true
source "https://rubygems.org"

gem "rack"
gem "rspec"
gem "oj"
gem "controuter" #custom gem
Loading