hobby 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/hobby.gemspec +1 -1
- data/lib/hobby.rb +59 -3
- data/spec/helper.rb +0 -2
- data/spec/router_matchers.rb +33 -0
- data/spec/router_spec.rb +4 -1
- metadata +4 -6
- data/lib/hobby/app.rb +0 -59
- data/lib/hobby/rspec/router.rb +0 -35
- data/spec/router_group_spec.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b0ae0b8efe38c2fa31277c8170148944aef182b
|
4
|
+
data.tar.gz: ad2dd203359e369d4df8a17836e099f97f00a7e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60dc8490104a8edbad0ec129d0fe916984bafecdabc8f234952521d2f39bc067c916d4d3e8c1afa5e4c82a8caeeed7eb73b5f4024593b8acdf73684728088513
|
7
|
+
data.tar.gz: 646a11c3a09a480b3ab53bcf4c99df141786c4d708e64fe8e975e9cea8eeabbbef60cfe66f777f527b9d6854b1ec4edcea865e318d8299f54c99d4385cdede9a
|
data/hobby.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'hobby'
|
7
|
-
spec.version = '0.0.
|
7
|
+
spec.version = '0.0.3'
|
8
8
|
spec.authors = ['Anatoly Chernow']
|
9
9
|
spec.email = ['chertoly@gmail.com']
|
10
10
|
spec.summary = %q{A minimal DSL over rack}
|
data/lib/hobby.rb
CHANGED
@@ -1,9 +1,65 @@
|
|
1
1
|
require 'rack'
|
2
2
|
require 'forwardable'
|
3
3
|
|
4
|
+
require 'hobby/router'
|
5
|
+
|
4
6
|
module Hobby
|
7
|
+
App = Hobby # to stay compatible old code
|
5
8
|
VERBS = %w[DELETE GET HEAD OPTIONS PATCH POST PUT]
|
6
|
-
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
+
def self.included app
|
11
|
+
app.extend Singleton
|
12
|
+
app.builder, app.router = Rack::Builder.new, Router.new
|
13
|
+
app.request, app.response = Rack::Request, Rack::Response
|
14
|
+
end
|
15
|
+
|
16
|
+
module Singleton
|
17
|
+
attr_accessor :builder, :router, :request, :response
|
18
|
+
|
19
|
+
def new (*)
|
20
|
+
builder.run super
|
21
|
+
builder.to_app
|
22
|
+
end
|
23
|
+
|
24
|
+
extend Forwardable
|
25
|
+
delegate [:map, :use] => :builder
|
26
|
+
|
27
|
+
VERBS.each do |verb|
|
28
|
+
define_method verb.downcase do |path = '/', &action|
|
29
|
+
router.add_route verb, path, &action
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def call env
|
35
|
+
dup.handle env
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
def handle env
|
40
|
+
route = self.class.router.route_for (@env = env)
|
41
|
+
|
42
|
+
if route
|
43
|
+
response.write instance_exec &route
|
44
|
+
else
|
45
|
+
response.status = 404
|
46
|
+
end
|
47
|
+
|
48
|
+
response
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
attr_reader :env
|
53
|
+
|
54
|
+
def request
|
55
|
+
@request ||= self.class.request.new env
|
56
|
+
end
|
57
|
+
|
58
|
+
def response
|
59
|
+
@response ||= self.class.response.new
|
60
|
+
end
|
61
|
+
|
62
|
+
def my
|
63
|
+
env.fetch :path_params, {}
|
64
|
+
end
|
65
|
+
end
|
data/spec/helper.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'devtools/spec_helper'
|
2
2
|
|
3
3
|
require 'hobby'
|
4
|
-
require 'hobby/rspec/router'
|
5
4
|
|
6
5
|
require 'minitest'
|
7
6
|
require 'minitest-power_assert'
|
@@ -17,5 +16,4 @@ end
|
|
17
16
|
|
18
17
|
RSpec.configure do |config|
|
19
18
|
config.expect_with :rspec, :minitest
|
20
|
-
config.include Hobby::RSpec::Router, type: :router
|
21
19
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module RouterMatchers
|
2
|
+
extend self
|
3
|
+
SOME_ROUTE = ->{:some_route}
|
4
|
+
|
5
|
+
def env_for path, verb = 'GET'
|
6
|
+
{'REQUEST_METHOD' => verb, 'PATH_INFO' => path }
|
7
|
+
end
|
8
|
+
|
9
|
+
def add_routes *routes
|
10
|
+
routes.each { |route| subject.add_route 'GET', route, &SOME_ROUTE }
|
11
|
+
end
|
12
|
+
|
13
|
+
extend RSpec::Matchers::DSL
|
14
|
+
|
15
|
+
define :find_route do |path, verb = 'GET'|
|
16
|
+
match do |subject|
|
17
|
+
env = env_for path, verb
|
18
|
+
route = subject.route_for env
|
19
|
+
|
20
|
+
params_are_ok = (@params ? (@params.to_a - env[:path_params].to_a).empty? : true)
|
21
|
+
|
22
|
+
route && (route.to_proc.call == SOME_ROUTE.call) && params_are_ok
|
23
|
+
end
|
24
|
+
|
25
|
+
chain :and_set_params do |**params|
|
26
|
+
@params = params
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.included example_group
|
31
|
+
example_group.extend self
|
32
|
+
end
|
33
|
+
end
|
data/spec/router_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hobby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anatoly Chernow
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -40,11 +40,9 @@ files:
|
|
40
40
|
- Rakefile
|
41
41
|
- hobby.gemspec
|
42
42
|
- lib/hobby.rb
|
43
|
-
- lib/hobby/app.rb
|
44
43
|
- lib/hobby/router.rb
|
45
44
|
- lib/hobby/router/route.rb
|
46
45
|
- lib/hobby/router/routes.rb
|
47
|
-
- lib/hobby/rspec/router.rb
|
48
46
|
- spec/app_spec.rb
|
49
47
|
- spec/apps/Decorator.rb
|
50
48
|
- spec/apps/Env.rb
|
@@ -55,7 +53,7 @@ files:
|
|
55
53
|
- spec/apps/Use.rb
|
56
54
|
- spec/apps/WithoutPath.rb
|
57
55
|
- spec/helper.rb
|
58
|
-
- spec/
|
56
|
+
- spec/router_matchers.rb
|
59
57
|
- spec/router_spec.rb
|
60
58
|
homepage: https://github.com/ch1c0t/hobby
|
61
59
|
licenses:
|
@@ -92,5 +90,5 @@ test_files:
|
|
92
90
|
- spec/apps/Use.rb
|
93
91
|
- spec/apps/WithoutPath.rb
|
94
92
|
- spec/helper.rb
|
95
|
-
- spec/
|
93
|
+
- spec/router_matchers.rb
|
96
94
|
- spec/router_spec.rb
|
data/lib/hobby/app.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
module Hobby
|
2
|
-
module App
|
3
|
-
def self.included app
|
4
|
-
app.extend Singleton
|
5
|
-
app.builder, app.router = Rack::Builder.new, Router.new
|
6
|
-
app.request, app.response = Rack::Request, Rack::Response
|
7
|
-
end
|
8
|
-
|
9
|
-
module Singleton
|
10
|
-
attr_accessor :builder, :router, :request, :response
|
11
|
-
|
12
|
-
def new (*)
|
13
|
-
builder.run super
|
14
|
-
builder.to_app
|
15
|
-
end
|
16
|
-
|
17
|
-
extend Forwardable
|
18
|
-
delegate [:map, :use] => :builder
|
19
|
-
|
20
|
-
VERBS.each do |verb|
|
21
|
-
define_method verb.downcase do |path = '/', &action|
|
22
|
-
router.add_route verb, path, &action
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def call env
|
28
|
-
dup.handle env
|
29
|
-
end
|
30
|
-
|
31
|
-
protected
|
32
|
-
def handle env
|
33
|
-
route = self.class.router.route_for (@env = env)
|
34
|
-
|
35
|
-
if route
|
36
|
-
response.write instance_exec &route
|
37
|
-
else
|
38
|
-
response.status = 404
|
39
|
-
end
|
40
|
-
|
41
|
-
response
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
attr_reader :env
|
46
|
-
|
47
|
-
def request
|
48
|
-
@request ||= self.class.request.new env
|
49
|
-
end
|
50
|
-
|
51
|
-
def response
|
52
|
-
@response ||= self.class.response.new
|
53
|
-
end
|
54
|
-
|
55
|
-
def my
|
56
|
-
env.fetch :path_params, {}
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
data/lib/hobby/rspec/router.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module Hobby::RSpec
|
2
|
-
module Router
|
3
|
-
extend self
|
4
|
-
SOME_ROUTE = ->{:some_route}
|
5
|
-
|
6
|
-
def env_for path, verb = 'GET'
|
7
|
-
{'REQUEST_METHOD' => verb, 'PATH_INFO' => path }
|
8
|
-
end
|
9
|
-
|
10
|
-
def add_routes *routes
|
11
|
-
routes.each { |route| subject.add_route 'GET', route, &SOME_ROUTE }
|
12
|
-
end
|
13
|
-
|
14
|
-
extend RSpec::Matchers::DSL
|
15
|
-
|
16
|
-
define :find_route do |path, verb = 'GET'|
|
17
|
-
match do |subject|
|
18
|
-
env = env_for path, verb
|
19
|
-
route = subject.route_for env
|
20
|
-
|
21
|
-
params_are_ok = (@params ? (@params.to_a - env[:path_params].to_a).empty? : true)
|
22
|
-
|
23
|
-
route && (route.to_proc.call == SOME_ROUTE.call) && params_are_ok
|
24
|
-
end
|
25
|
-
|
26
|
-
chain :and_set_params do |**params|
|
27
|
-
@params = params
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.included example_group
|
32
|
-
example_group.extend self
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/spec/router_group_spec.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
describe Hobby::RSpec::Router do
|
4
|
-
before do
|
5
|
-
@Group = Class.new do
|
6
|
-
def subject
|
7
|
-
@router ||= Hobby::Router.new
|
8
|
-
end
|
9
|
-
end.include described_class
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'contains a few helpers in its singleton' do
|
13
|
-
[:env_for, :add_routes, :find_route].each do |name|
|
14
|
-
assert { @Group.respond_to? name }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe 'env_for' do
|
19
|
-
context 'two arguments passed' do
|
20
|
-
it 'returns an environment' do
|
21
|
-
env = { "REQUEST_METHOD" => 'POST', "PATH_INFO" => '/some_path' }
|
22
|
-
assert { env == (@Group.env_for '/some_path', 'POST') }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'one argument passed' do
|
27
|
-
it 'defaults to GET' do
|
28
|
-
env = { "REQUEST_METHOD" => 'GET', "PATH_INFO" => '/some_path' }
|
29
|
-
assert { env == (@Group.env_for '/some_path') }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe 'add_routes' do
|
35
|
-
it do
|
36
|
-
routes = %w! first second third!
|
37
|
-
group = @Group.new
|
38
|
-
|
39
|
-
group.add_routes *routes
|
40
|
-
|
41
|
-
routes.each do |route|
|
42
|
-
expect(group.subject.route_for group.env_for 'first').to be_truthy
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|