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 +4 -4
- data/Gemfile +1 -1
- data/LICENSE +1 -1
- data/README.md +17 -29
- data/Rakefile +1 -1
- data/lib/hobbit/base.rb +6 -10
- data/lib/hobbit/version.rb +1 -1
- data/spec/base_spec.rb +108 -25
- data/spec/minitest_helper.rb +1 -1
- data/spec/version_spec.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f680a28141ffc906e11a72dd899d9dc21defb1d5
|
4
|
+
data.tar.gz: a6296b3812d60e4a72927dc6ec59d97d524bc763
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae7659b05503edeac4b9659893ee95a3b5b3600ac4fd84b800c0a4561f41b9482ecfad567792cb6f0d4f55a75122e9a5e3407ceb2e2a5ba190fb8045ef34ca34
|
7
|
+
data.tar.gz: 88d899534b1103ef23cec457d48c13d210c4dddbbbad85e57296d0fcd569767066dd1e41f2be9134f3f736d379010fe0f1bfb449b46ac6eff0d1f44241f0716c
|
data/Gemfile
CHANGED
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
|
-
|
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
|
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,
|
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
|
-
|
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
|
-
|
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
|
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
|
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
|
-
##
|
337
|
+
## Community
|
350
338
|
|
351
|
-
|
352
|
-
|
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
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
|
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 :
|
12
|
+
alias :_new :new
|
13
13
|
def new(*args, &block)
|
14
|
-
stack.run
|
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
|
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 =
|
58
|
-
@response =
|
53
|
+
@request = Rack::Request.new(@env)
|
54
|
+
@response = Hobbit::Response.new
|
59
55
|
route_eval
|
60
56
|
@response.finish
|
61
57
|
end
|
data/lib/hobbit/version.rb
CHANGED
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 '::
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
55
|
-
|
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
|
-
|
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
|
-
|
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
|
data/spec/minitest_helper.rb
CHANGED
data/spec/version_spec.rb
CHANGED
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.
|
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-
|
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.
|
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
|