Skip to content
This repository has been archived by the owner on May 1, 2018. It is now read-only.

HW 3 Babych Svitlana #19

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Homework 4

- Travis CI for gem: https://travis-ci.org/SvitlanaLana/web_config .

- Travis CI for app: https://travis-ci.org/SvitlanaLana/kottans_homework4 .

- Deployed app: https://aqueous-atoll-31147.herokuapp.com/
21 changes: 21 additions & 0 deletions hw1/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Fibonacci
include Enumerable

def initialize(number)
validate(number)
@n = number
end

def each(&block)
curr = 1
prev = 0
(1..@n).each do |i|
block.call(curr)
curr, prev = prev + curr, curr
end
end

def validate(number)
raise ArgumentError unless number.is_a?(Integer) && number >=0
end
end
4 changes: 4 additions & 0 deletions hw2/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ source "https://rubygems.org"

gem "rack"
gem "rspec"

group 'test' do
gem 'pry'
end
8 changes: 8 additions & 0 deletions hw2/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.1)
diff-lcs (1.2.5)
method_source (0.8.2)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (2.0.1)
rspec (3.5.0)
rspec-core (~> 3.5.0)
Expand All @@ -16,11 +22,13 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
slop (3.6.0)

PLATFORMS
ruby

DEPENDENCIES
pry
rack
rspec

Expand Down
4 changes: 4 additions & 0 deletions hw2/app/app.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Application = Router.new do
get '/test', ->(env) { [200, {}, ['get test']] }
post '/test', ->(env) { [200, {}, ['post test']] }

get '/posts/:id/edit', ->(env) do
[200, {}, ["Post #{env['PARAMS'][:id]}"]]
end
end
28 changes: 26 additions & 2 deletions hw2/lib/router.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
class Router
def call(env)
@routes[env['REQUEST_METHOD']][env['REQUEST_PATH']].call(env)
env['PARAMS'] = {}
lamda = nil
@routes[env['REQUEST_METHOD']].each do |key, value|
if env['REQUEST_PATH'] =~ key
lamda = value
env['PARAMS'] = key.match(env['REQUEST_PATH'])
break
end
end
lamda = ->(env) { [404, {}, ['Page not found']] } unless lamda
Copy link
Owner

Choose a reason for hiding this comment

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

you can use lambda ||= ->(env) { [404, {}, ['Page not found']] }

lamda.call(env)
end

private
Expand All @@ -20,6 +30,20 @@ def post(path, rack_app)

def match(http_method, path, rack_app)
@routes[http_method] ||= {}
@routes[http_method][path] = rack_app
@routes[http_method][prepare_route_regex(path)] = rack_app
end

def prepare_route_regex(path)
arr = path.split('/')
temp = arr.map do |e|
if e[0] == ':'
name = e[1..-1]
"(?<#{name}>\\w+)"
else
e
end
end
reg_str = "^#{temp.join('\/')}$"
Regexp.new(reg_str)
end
end
44 changes: 33 additions & 11 deletions hw2/spec/lib/router_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
require 'spec_helper'
require 'pry'

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']] }
get '/post/:name/edit/:field', ->(env) { [200, {}, ['url include argument'] ] }
end
end

let(:env) { { 'REQUEST_METHOD' => 'GET'} }

it 'url have argument' do
env['REQUEST_PATH'] = '/test'
Copy link
Owner

@denysxftr denysxftr Nov 27, 2016

Choose a reason for hiding this comment

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

I think you can move this to let

subject.call(env)
expect(env.keys).to include('PARAMS')
end

context 'when request is GET' do
let(:env) { { 'REQUEST_PATH' => '/test', 'REQUEST_METHOD' => 'GET'} }
let(:env) { { 'REQUEST_METHOD' => 'GET'} }

it 'matches request' do
env['REQUEST_PATH'] = '/test'
Copy link
Owner

Choose a reason for hiding this comment

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

and this

expect(subject.call(env)).to eq [200, {}, ['get test']]
end

context 'and have url arguments' do
before { env['REQUEST_PATH'] = '/post/vasya/edit/12' }

it 'matches url', focue: true do
expect(subject.call(env)).to eq [200, {}, ['url include argument']]
end

it 'add arguments from url' do
subject.call(env)
expect(env['PARAMS'][:name]).to eq 'vasya'
expect(env['PARAMS'][:field]).to eq '12'
end

it '404' do
Copy link
Owner

Choose a reason for hiding this comment

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

and here you can have a separate context for 404 case

env['REQUEST_PATH'] = '/post/vasya/edit/12/qwe'
expect(subject.call(env)).to eq [404, {}, ['Page not found']]
end
end
end

context 'when request is POST' do
Expand Down
3 changes: 3 additions & 0 deletions hw3/hw4/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: ruby
rvm:
- 2.3
7 changes: 7 additions & 0 deletions hw3/hw4/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 "web_config", path: '../web_config'
37 changes: 37 additions & 0 deletions hw3/hw4/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
PATH
remote: ../web_config
specs:
web_config (0.1.0)
oj
rack

GEM
remote: https://rubygems.org/
specs:
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
rack
rspec
web_config!

BUNDLED WITH
1.13.6
27 changes: 27 additions & 0 deletions hw3/hw4/app/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rack'
require 'web_config'
require 'oj'

class UsersController < WebConfig::Controller
def show
response(:json, "Hello, #{params['user']}")
end
end

class BlogsController < WebConfig::Controller
def new
response(:html, 'view/example.html')
end
end

class WelcomesController < WebConfig::Controller
def show
response(:text, "Request method: #{request.request_method}")
end
end

Application = WebConfig::Router.new do
get '/example/:user', 'users#show'
get '/example/:user/:blog', 'blogs#new'
get '/example', 'welcomes#show'
end
3 changes: 3 additions & 0 deletions hw3/hw4/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require './main'

run Application
1 change: 1 addition & 0 deletions hw3/hw4/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require './app/app'
8 changes: 8 additions & 0 deletions hw3/hw4/view/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<body>

<h1>My first Blog</h1>

</body>
</html>
9 changes: 9 additions & 0 deletions hw3/web_config/.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 hw3/web_config/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
5 changes: 5 additions & 0 deletions hw3/web_config/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.3.0
before_install: gem install bundler -v 1.13.6
74 changes: 74 additions & 0 deletions hw3/web_config/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at [email protected]. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
6 changes: 6 additions & 0 deletions hw3/web_config/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in web_config.gemspec
gem "rack"
gem "rspec"
gem 'oj'
4 changes: 4 additions & 0 deletions hw3/web_config/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 web_config.gemspec
gemspec
Loading