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

HW1 #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

HW1 #32

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
2 changes: 1 addition & 1 deletion check.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby ./hw1/fibonacci_test.rb
ruby ./hw1/fibonacci_test.rb && (cd hw2 && bundle install && bundle exec rspec)
9 changes: 9 additions & 0 deletions gem_example/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 gem_example/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 gem_example/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.1
before_install: gem install bundler -v 1.13.5
4 changes: 4 additions & 0 deletions gem_example/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
21 changes: 21 additions & 0 deletions gem_example/web_config/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Denis Sergienko

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 gem_example/web_config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# WebConfig

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/web_config`. 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 'web_config'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install web_config

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. 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]/web_config.


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

6 changes: 6 additions & 0 deletions gem_example/web_config/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions gem_example/web_config/bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "web_config"

# 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 gem_example/web_config/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
6 changes: 6 additions & 0 deletions gem_example/web_config/lib/web_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "web_config/version"
require "web_config/config"

module WebConfig
# Your code goes here...
end
13 changes: 13 additions & 0 deletions gem_example/web_config/lib/web_config/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class WebConfig::Config
attr_reader :host

def self.load(file)
host = file.readlines
raise if host.empty?
new(host: host)
end

def initialize(host: 'localhost')
@host = host
end
end
3 changes: 3 additions & 0 deletions gem_example/web_config/lib/web_config/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module WebConfig
VERSION = "0.1.0"
end
2 changes: 2 additions & 0 deletions gem_example/web_config/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 "web_config"
57 changes: 57 additions & 0 deletions gem_example/web_config/spec/web_config/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "spec_helper"

describe WebConfig::Config do
describe '.load' do
let(:file) { instance_double('File') }
subject { described_class.load(file) }

before do
expect(file).to receive(:readlines).and_return(host)
end

context 'when file is valid' do
let(:host) { '172.17.0.1' }

it 'parses value' do
expect(subject.host).to eq host
end
end

context 'when file is not valid' do
let(:host) { '' }

it 'raises exception' do
expect { subject }.to raise_error(RuntimeError)
end
end
end

describe '#host' do
subject { config_instance.host }

context 'when there is default value' do
let(:config_instance) { described_class.new }

it 'returns default value' do
expect(subject).to eq 'localhost'
end
end

context 'when there is not default value' do
let(:config_instance) { described_class.new(host: host) }
let(:host) { '172.17.0.1' }

it 'returns specified value' do
expect(subject).to eq host
end
end
end

# describe '#port' do
# context 'when there is default value' do
# end
#
# context 'when there is not default value' do
# end
# end
end
7 changes: 7 additions & 0 deletions gem_example/web_config/spec/web_config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "spec_helper"

describe WebConfig do
it "has a version number" do
expect(WebConfig::VERSION).not_to be nil
end
end
35 changes: 35 additions & 0 deletions gem_example/web_config/web_config.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 'web_config/version'

Gem::Specification.new do |spec|
spec.name = "web_config"
spec.version = WebConfig::VERSION
spec.authors = ["Denis Sergienko"]
spec.email = ["[email protected]"]

spec.summary = %q{hekusfhsjkjsdk}
spec.homepage = "http://example.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://mygemserver.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
19 changes: 19 additions & 0 deletions hw1/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Fibonacci
include Enumerable

def initialize(length)
@length = length
end

def each(&block)
@result = []
for i in 1..@length
if i < 3
@result[i] = 1
else
@result[i] = @result[i - 1] + @result[i - 2]
end
yield @result[i]
end
end
end
2 changes: 2 additions & 0 deletions hw2/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
5 changes: 5 additions & 0 deletions hw2/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true
source "https://rubygems.org"

gem "rack"
gem "rspec"
28 changes: 28 additions & 0 deletions hw2/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.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

BUNDLED WITH
1.13.5
4 changes: 4 additions & 0 deletions hw2/app/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Application = Router.new do
get '/test', ->(env) { [200, {}, ['get test']] }
post '/test', ->(env) { [200, {}, ['post test']] }
end
3 changes: 3 additions & 0 deletions hw2/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require './main'

run Application
45 changes: 45 additions & 0 deletions hw2/lib/router.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Router
def call(env)
find_route(env).call(env)
end

private

def initialize(&block)
@routes = []
instance_exec(&block)
end

def find_route(env)
@routes.each do |route|
if env['REQUEST_METHOD'] == route[:method] && env['REQUEST_PATH'] =~ route[:regexp]
return route[:app]
end
end

->(_env) { [404, {}, ['page not found']] }
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 << {
method: http_method,
pattern: path,
app: rack_app,
regexp: path_to_regexp(path)
}
end

private

def path_to_regexp(path)
Regexp.new('\A' + path.gsub(/:[\w-]+/, '[\w-]+') + '\Z')
end
end
2 changes: 2 additions & 0 deletions hw2/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './lib/router'
require './app/app'
Loading