hobby 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a09d8721fee0c8b802dfc4137a45d746024a10d
4
- data.tar.gz: b5eebe2c4534c37f43bfde807e243606747bb9b5
3
+ metadata.gz: 2a3c9210b9735f56e28762ceeb49fa621b5a5f0b
4
+ data.tar.gz: 8969c233002b7d0d334adb1a2457b420d07d2bb1
5
5
  SHA512:
6
- metadata.gz: f985469d665eb47e06ed8fdfcfb248c08365987b07a010570cef81c2bdad00711012fa99171bccc9218482e5728215dc6146c4ea263583895282d8d13ef35826
7
- data.tar.gz: 6344ca6071eb2e297b42b3171902abeb34cb3f7ecb4d148499849ab87d0ee15cca2cf9c5dfdf248e9a91365324e7620e71129cc7a1be9b675895f44bf14822cc
6
+ metadata.gz: 6e751d77257376799bcd4fc942294612056e259225a56b45ab24e1923d8deb744ea0539eb15568e468c9d82ba4f58d7ad9169aefd1d019d4a499332943d3d51e
7
+ data.tar.gz: 4c5dd14d04d1a91ddfc8e030abf63e9d8a2cae6798ac8d2e2a8260d9c9e494d52daa9167f4d0afbf948d27ca189c41b5b6bd189fee9b9d477e34068b77634807
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.8'
7
+ spec.version = '0.0.9'
8
8
  spec.authors = ['Anatoly Chernow']
9
9
  spec.email = ['chertoly@gmail.com']
10
10
  spec.summary = %q{A minimal DSL over rack}
@@ -2,10 +2,11 @@ class Hobby::Router
2
2
  class Route
3
3
  def initialize verb, path, &action
4
4
  @verb, @path, @action = verb, path, action
5
+ @params = {}
5
6
  end
6
7
 
7
8
  attr_reader :verb, :path
8
- attr_accessor :action
9
+ attr_accessor :action, :params
9
10
 
10
11
  alias to_proc action
11
12
  end
data/lib/hobby/router.rb CHANGED
@@ -15,7 +15,10 @@ module Hobby
15
15
 
16
16
  def route_for env
17
17
  route, params = @routes["#{env['REQUEST_METHOD']}#{env['PATH_INFO']}"]
18
- env[:path_params] = params if params
18
+
19
+ route = route.dup
20
+ route.params = params if params
21
+
19
22
  route
20
23
  end
21
24
  end
data/lib/hobby.rb CHANGED
@@ -38,7 +38,7 @@ module Hobby
38
38
  protected
39
39
  def handle env
40
40
  catch :halt do
41
- route = self.class.router.route_for (@env = env)
41
+ @route = self.class.router.route_for (@env = env)
42
42
 
43
43
  body = route ? (instance_exec &route) : not_found
44
44
  response.write body
@@ -48,7 +48,7 @@ module Hobby
48
48
  end
49
49
 
50
50
  private
51
- attr_reader :env
51
+ attr_reader :env, :route
52
52
 
53
53
  def request
54
54
  @request ||= self.class.request.new env
@@ -59,7 +59,7 @@ module Hobby
59
59
  end
60
60
 
61
61
  def my
62
- env.fetch :path_params, {}
62
+ route.params
63
63
  end
64
64
 
65
65
  def halt
@@ -69,4 +69,9 @@ module Hobby
69
69
  def not_found
70
70
  response.status = 404
71
71
  end
72
+
73
+ def content_type type
74
+ mime_type = Rack::Mime::MIME_TYPES.fetch ".#{type}"
75
+ response.add_header 'Content-Type', mime_type
76
+ end
72
77
  end
data/readme.adoc CHANGED
@@ -193,11 +193,12 @@ The following methods are predefined:
193
193
  * `env`: a `Hash`, http://www.rubydoc.info/github/rack/rack/master/file/SPEC#The_Environment[a Rack environment].
194
194
  * `request`: a http://www.rubydoc.info/gems/rack/Rack/Request[`Rack::Request`].
195
195
  * `response`: a http://www.rubydoc.info/gems/rack/Rack/Response[`Rack::Response`].
196
- * `my`: a `Hash` which stores route variables. See <<routes-with-variables>> for a usage example.
196
+ * `route`: a `Hobby::Router::Route`, the currently executing route.
197
+ * `route.params`, or a shortcut `my`: a `Hash` which stores route params. See <<routes-params>> for a usage example.
197
198
  * `halt`: returns the `response` immediately. See <<halting>> for a usage example.
198
199
 
199
- [[routes-with-variables]]
200
- === Routes with variables
200
+ [[routes-params]]
201
+ === Routes with params
201
202
 
202
203
  [source,ruby]
203
204
  ----
data/spec/app_spec.rb CHANGED
@@ -155,5 +155,13 @@ describe Hobby::App do
155
155
  assert { last_response.status == 400 }
156
156
  end
157
157
  end
158
+
159
+ describe ContentType do
160
+ it do
161
+ get '/'
162
+ assert { last_response.body == "alert('string');" }
163
+ assert { last_response.headers['Content-Type'] == 'application/javascript' }
164
+ end
165
+ end
158
166
  end
159
167
  end
@@ -0,0 +1,4 @@
1
+ get do
2
+ content_type :js
3
+ "alert('string');"
4
+ end
@@ -17,7 +17,7 @@ module RouterMatchers
17
17
  env = env_for path, verb
18
18
  route = subject.route_for env
19
19
 
20
- params_are_ok = (@params ? (@params.to_a - env[:path_params].to_a).empty? : true)
20
+ params_are_ok = (@params ? (@params.to_a - route.params.to_a).empty? : true)
21
21
 
22
22
  route && (route.to_proc.call == SOME_ROUTE.call) && params_are_ok
23
23
  end
data/spec/router_spec.rb CHANGED
@@ -37,4 +37,13 @@ describe Hobby::Router do
37
37
 
38
38
  assert { subject.instance_variable_get(:@routes).include? 'GET/hello/ololo' }
39
39
  end
40
+
41
+ it 'dups a route for each request' do
42
+ router = described_class.new
43
+ route = router.add_route 'GET', '/some_path' do :some_action end
44
+
45
+ duped_route = router.route_for env_for '/some_path'
46
+
47
+ assert { not route.equal? duped_route }
48
+ end
40
49
  end
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.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anatoly Chernow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-31 00:00:00.000000000 Z
11
+ date: 2017-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -44,6 +44,7 @@ files:
44
44
  - lib/hobby/router/routes.rb
45
45
  - readme.adoc
46
46
  - spec/app_spec.rb
47
+ - spec/apps/ContentType.rb
47
48
  - spec/apps/Decorator.rb
48
49
  - spec/apps/Env.rb
49
50
  - spec/apps/Halting.rb
@@ -82,6 +83,7 @@ specification_version: 4
82
83
  summary: A minimal DSL over rack
83
84
  test_files:
84
85
  - spec/app_spec.rb
86
+ - spec/apps/ContentType.rb
85
87
  - spec/apps/Decorator.rb
86
88
  - spec/apps/Env.rb
87
89
  - spec/apps/Halting.rb