hobbit 0.1.0 → 0.2.0

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: a40ad5190f62122b017865652dd404d685679bbd
4
- data.tar.gz: 68ab170cf365e621b439e3c95c37d68b27391213
3
+ metadata.gz: f680a28141ffc906e11a72dd899d9dc21defb1d5
4
+ data.tar.gz: a6296b3812d60e4a72927dc6ec59d97d524bc763
5
5
  SHA512:
6
- metadata.gz: 655b5ebd8ff4cb2df724c477ab376c801f9d3a2c303a0ad417ad0670600d4900bba76eb0404ca0879df63ffc9323ffdc1d64abcb45b802289d04e8c868ac4d46
7
- data.tar.gz: 3024565ad30b77a25403b7f7863667293101ba3bbbf7960d9f15f99733e28128f87bc29e3fc877cc1938f69b5a75519581aa83287718e9fe9ff7b6e692813fe8
6
+ metadata.gz: ae7659b05503edeac4b9659893ee95a3b5b3600ac4fd84b800c0a4561f41b9482ecfad567792cb6f0d4f55a75122e9a5e3407ceb2e2a5ba190fb8045ef34ca34
7
+ data.tar.gz: 88d899534b1103ef23cec457d48c13d210c4dddbbbad85e57296d0fcd569767066dd1e41f2be9134f3f736d379010fe0f1bfb449b46ac6eff0d1f44241f0716c
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gemspec
data/LICENSE CHANGED
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -31,8 +31,6 @@ $ gem install hobbit
31
31
  * Extensible with standard ruby classes and modules, with no extra logic. See
32
32
  [hobbit-contrib](https://github.com/patriciomacadden/hobbit-contrib).
33
33
  * Zero configuration.
34
- * Request and response classes could be injected (Defaults to `Rack::Request`
35
- and `Hobbit::Response`, respectively).
36
34
 
37
35
  ## Philosophy
38
36
 
@@ -42,7 +40,7 @@ its extensions instead of providing such functionality.
42
40
 
43
41
  ## Usage
44
42
 
45
- `Hobbit` applications are just instances of classes that inherits from
43
+ Hobbit applications are just instances of classes that inherits from
46
44
  `Hobbit::Base`, which complies the
47
45
  [Rack SPEC](http://rack.rubyforge.org/doc/SPEC.html).
48
46
 
@@ -120,6 +118,8 @@ When a route gets called you have this methods available:
120
118
  * `request`: a `Rack::Request` instance.
121
119
  * `response`: a `Hobbit::Response` instance.
122
120
 
121
+ And any other method defined in your application.
122
+
123
123
  #### Available methods
124
124
 
125
125
  * `delete`
@@ -176,7 +176,7 @@ end
176
176
 
177
177
  ### Built on top of rack
178
178
 
179
- Each hobbit application is a Rack stack (See this
179
+ Each Hobbit application is a Rack stack (See this
180
180
  [blog post](http://m.onkey.org/ruby-on-rack-2-the-builder) for more
181
181
  information).
182
182
 
@@ -212,10 +212,13 @@ You can add any Rack middleware to the stack by using the `use` class method:
212
212
  require 'hobbit'
213
213
 
214
214
  class App < Hobbit::Base
215
- include Hobbit::Session
216
215
  use Rack::Session::Cookie, secret: SecureRandom.hex(64)
217
216
  use Rack::ShowExceptions
218
217
 
218
+ def session
219
+ env['rack.session']
220
+ end
221
+
219
222
  get '/' do
220
223
  session[:name] = 'hobbit'
221
224
  end
@@ -226,23 +229,9 @@ end
226
229
  run App.new
227
230
  ```
228
231
 
229
- ### Request and response classes
230
-
231
- You can inject the request (`Rack::Request`) and response (`Hobbit::Response`)
232
- classes for your application. Do it like this:
233
-
234
- ```ruby
235
- class App < Hobbit::Base
236
- settings[:request_class] = MyRequest
237
- settings[:response_class] = MyResponse
238
-
239
- # the rest of your app...
240
- end
241
- ```
242
-
243
232
  ### Security
244
233
 
245
- By default, `hobbit` (nor Rack) comes without any protection against web
234
+ By default, Hobbit (nor Rack) comes without any protection against web
246
235
  attacks. The use of [rack-protection](https://github.com/rkh/rack-protection)
247
236
  is highly recommended:
248
237
 
@@ -261,7 +250,7 @@ class App < Hobbit::Base
261
250
  end
262
251
  ```
263
252
 
264
- Please see the [rack-protection](https://github.com/rkh/rack-protection)
253
+ See the [rack-protection](https://github.com/rkh/rack-protection)
265
254
  documentation for futher information.
266
255
 
267
256
  ### Testing
@@ -305,12 +294,12 @@ describe App do
305
294
  end
306
295
  ```
307
296
 
308
- Please see the [rack-test](https://github.com/brynary/rack-test) documentation
297
+ See the [rack-test](https://github.com/brynary/rack-test) documentation
309
298
  for futher information.
310
299
 
311
300
  ### Extensions
312
301
 
313
- You can extend hobbit by creating standard ruby modules. See an example:
302
+ You can extend Hobbit by creating standard ruby modules. See an example:
314
303
 
315
304
  ```ruby
316
305
  module MyExtension
@@ -334,8 +323,7 @@ end
334
323
  [hobbit-contrib](https://github.com/patriciomacadden/hobbit-contrib) is a ruby
335
324
  gem that comes with a lot of hobbit extensions, such as:
336
325
 
337
- * `Hobbit::Render`: provides a (very) basic template rendering module.
338
- * `Hobbit::EnhancedRender`: provides an enhanced template rendering module.
326
+ * `Hobbit::Render`: provides basic template rendering.
339
327
  * `Hobbit::Session`: provides helper methods for handling user sessions.
340
328
  * `Hobbit::Environment`: provides helper methods for handling application
341
329
  environments.
@@ -346,10 +334,10 @@ Sinatra-like error handling.
346
334
 
347
335
  ... And many more!
348
336
 
349
- ## Futher documentation
337
+ ## Community
350
338
 
351
- The [hobbit wiki](https://github.com/patriciomacadden/hobbit/wiki) contains
352
- guides, how-tos and recipes for a better hobbit experience.
339
+ * [Wiki](https://github.com/patriciomacadden/hobbit/wiki): Guides, how-tos and recipes
340
+ * IRC: [#hobbitrb](irc://chat.freenode.net/#hobbitrb) on [http://freenode.net](http://freenode.net)
353
341
 
354
342
  ## Contributing
355
343
 
@@ -361,4 +349,4 @@ guides, how-tos and recipes for a better hobbit experience.
361
349
 
362
350
  ## License
363
351
 
364
- See the [LICENSE](https://github.com/patriciomacadden/hobbit/blob/master/LICENSE).
352
+ See the [LICENSE](https://github.com/patriciomacadden/hobbit/blob/master/LICENSE).
data/Rakefile CHANGED
@@ -6,4 +6,4 @@ Rake::TestTask.new do |t|
6
6
  t.pattern = 'spec/**/*_spec.rb'
7
7
  end
8
8
 
9
- task default: :test
9
+ task default: :test
data/lib/hobbit/base.rb CHANGED
@@ -2,16 +2,16 @@ module Hobbit
2
2
  class Base
3
3
  class << self
4
4
  %w(DELETE GET HEAD OPTIONS PATCH POST PUT).each do |verb|
5
- define_method(verb.downcase) { |path = '', &block| routes[verb] << compile_route!(path, &block) }
5
+ define_method(verb.downcase) { |path = '', &block| routes[verb] << compile_route(path, &block) }
6
6
  end
7
7
 
8
8
  def map(path, &block)
9
9
  stack.map(path, &block)
10
10
  end
11
11
 
12
- alias :new! :new
12
+ alias :_new :new
13
13
  def new(*args, &block)
14
- stack.run new!(*args, &block)
14
+ stack.run _new(*args, &block)
15
15
  stack
16
16
  end
17
17
 
@@ -19,10 +19,6 @@ module Hobbit
19
19
  @routes ||= Hash.new { |hash, key| hash[key] = [] }
20
20
  end
21
21
 
22
- def settings
23
- @settings ||= { request_class: Rack::Request, response_class: Hobbit::Response }
24
- end
25
-
26
22
  def stack
27
23
  @stack ||= Rack::Builder.new
28
24
  end
@@ -33,7 +29,7 @@ module Hobbit
33
29
 
34
30
  private
35
31
 
36
- def compile_route!(path, &block)
32
+ def compile_route(path, &block)
37
33
  route = { block: block, compiled_path: nil, extra_params: [], path: path }
38
34
 
39
35
  compiled_path = path.gsub(/:\w+/) do |match|
@@ -54,8 +50,8 @@ module Hobbit
54
50
 
55
51
  def _call(env)
56
52
  @env = env
57
- @request = self.class.settings[:request_class].new(@env)
58
- @response = self.class.settings[:response_class].new
53
+ @request = Rack::Request.new(@env)
54
+ @response = Hobbit::Response.new
59
55
  route_eval
60
56
  @response.finish
61
57
  end
@@ -1,3 +1,3 @@
1
1
  module Hobbit
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/spec/base_spec.rb CHANGED
@@ -5,6 +5,10 @@ describe Hobbit::Base do
5
5
  include Rack::Test::Methods
6
6
 
7
7
  def app
8
+ @app
9
+ end
10
+
11
+ before do
8
12
  mock_app do
9
13
  %w(DELETE GET HEAD OPTIONS PATCH POST PUT).each do |verb|
10
14
  class_eval "#{verb.downcase} { '#{verb}' }"
@@ -33,33 +37,20 @@ EOS
33
37
  class_eval str
34
38
  end
35
39
 
36
- describe '::settings' do
37
- let(:settings) { app.to_app.class.settings }
38
-
39
- it 'must return a hash with (at least) a request_class and response_class keys' do
40
- settings.must_be_kind_of Hash
41
- settings.must_include :request_class
42
- settings.must_include :response_class
43
- end
44
-
45
- it 'must be initialized with request_class as Rack::Request' do
46
- settings[:request_class].must_equal Rack::Request
47
- end
48
-
49
- it 'must be initialized with request_class as Hobbit::Response' do
50
- settings[:response_class].must_equal Hobbit::Response
51
- end
52
- end
40
+ describe '::map' do
41
+ before do
42
+ mock_app do
43
+ map '/map' do
44
+ run Proc.new { |env| [200, {}, ['from map']] }
45
+ end
53
46
 
54
- describe '::stack' do
55
- it 'must return an instance of Rack::Builder' do
56
- app.to_app.class.stack.must_be_kind_of Rack::Builder
47
+ get('/') { 'hello world' }
48
+ end
57
49
  end
58
- end
59
50
 
60
- describe '::map' do
61
51
  it 'must mount a application to the rack stack' do
62
- skip '::map'
52
+ get '/map'
53
+ last_response.body.must_equal 'from map'
63
54
  end
64
55
  end
65
56
 
@@ -75,9 +66,101 @@ EOS
75
66
  end
76
67
  end
77
68
 
69
+ describe '::stack' do
70
+ it 'must return an instance of Rack::Builder' do
71
+ app.to_app.class.stack.must_be_kind_of Rack::Builder
72
+ end
73
+ end
74
+
78
75
  describe '::use' do
76
+ before do
77
+ mock_app do
78
+ middleware = Class.new do
79
+ def initialize(app = nil)
80
+ @app = app
81
+ end
82
+
83
+ def call(env)
84
+ request = Rack::Request.new(env)
85
+ @app.call(env) unless request.path_info == '/use'
86
+ [200, {}, 'from use']
87
+ end
88
+ end
89
+
90
+ use middleware
91
+
92
+ get('/') { 'hello world' }
93
+ end
94
+ end
95
+
79
96
  it 'must add a middleware to the rack stack' do
80
- skip '::use'
97
+ get '/use'
98
+ last_response.body.must_equal 'from use'
99
+ end
100
+ end
101
+
102
+ describe '::compile_route' do
103
+ let(:block) { block = Proc.new { |env| [200, {}, []] } }
104
+
105
+ it 'must compile an empty string' do
106
+ path = ''
107
+ route = Hobbit::Base.send :compile_route, path, &block
108
+ route[:block].call({}).must_equal block.call({})
109
+ route[:compiled_path].to_s.must_equal /^$/.to_s
110
+ route[:extra_params].must_equal []
111
+ route[:path].must_equal path
112
+ end
113
+
114
+ it 'must compile /' do
115
+ path = '/'
116
+ route = Hobbit::Base.send :compile_route, path, &block
117
+ route[:block].call({}).must_equal block.call({})
118
+ route[:compiled_path].to_s.must_equal /^\/$/.to_s
119
+ route[:extra_params].must_equal []
120
+ route[:path].must_equal path
121
+ end
122
+
123
+ it 'must compile with .' do
124
+ path = '/route.json'
125
+ route = Hobbit::Base.send :compile_route, path, &block
126
+ route[:block].call({}).must_equal block.call({})
127
+ route[:compiled_path].to_s.must_equal /^\/route.json$/.to_s
128
+ route[:extra_params].must_equal []
129
+ route[:path].must_equal path
130
+ end
131
+
132
+ it 'must compile with -' do
133
+ path = '/hello-world'
134
+ route = Hobbit::Base.send :compile_route, path, &block
135
+ route[:block].call({}).must_equal block.call({})
136
+ route[:compiled_path].to_s.must_equal /^\/hello-world$/.to_s
137
+ route[:extra_params].must_equal []
138
+ route[:path].must_equal path
139
+ end
140
+
141
+ it 'must compile with params' do
142
+ path = '/hello/:name'
143
+ route = Hobbit::Base.send :compile_route, path, &block
144
+ route[:block].call({}).must_equal block.call({})
145
+ route[:compiled_path].to_s.must_equal /^\/hello\/([^\/?#]+)$/.to_s
146
+ route[:extra_params].must_equal [:name]
147
+ route[:path].must_equal path
148
+
149
+ path = '/say/:something/to/:someone'
150
+ route = Hobbit::Base.send :compile_route, path, &block
151
+ route[:block].call({}).must_equal block.call({})
152
+ route[:compiled_path].to_s.must_equal /^\/say\/([^\/?#]+)\/to\/([^\/?#]+)$/.to_s
153
+ route[:extra_params].must_equal [:something, :someone]
154
+ route[:path].must_equal path
155
+ end
156
+
157
+ it 'must compile with . and params' do
158
+ path = '/route/:id.json'
159
+ route = Hobbit::Base.send :compile_route, path, &block
160
+ route[:block].call({}).must_equal block.call({})
161
+ route[:compiled_path].to_s.must_equal /^\/route\/([^\/?#]+).json$/.to_s
162
+ route[:extra_params].must_equal [:id]
163
+ route[:path].must_equal path
81
164
  end
82
165
  end
83
166
 
@@ -129,4 +212,4 @@ EOS
129
212
  it 'must respond to call' do
130
213
  app.to_app.must_respond_to :call
131
214
  end
132
- end
215
+ end
@@ -16,7 +16,7 @@ module Hobbit
16
16
  module Mock
17
17
  def mock_app(&block)
18
18
  app = Class.new Hobbit::Base, &block
19
- app.new
19
+ @app = app.new
20
20
  end
21
21
  end
22
22
  end
data/spec/version_spec.rb CHANGED
@@ -4,4 +4,4 @@ describe Hobbit::VERSION do
4
4
  it 'wont be nil' do
5
5
  Hobbit::VERSION.wont_be_nil
6
6
  end
7
- end
7
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patricio Mac Adden
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-09 00:00:00.000000000 Z
11
+ date: 2013-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  version: '0'
151
151
  requirements: []
152
152
  rubyforge_project:
153
- rubygems_version: 2.0.0
153
+ rubygems_version: 2.0.3
154
154
  signing_key:
155
155
  specification_version: 4
156
156
  summary: A minimalistic microframework built on top of rack