dzl 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dzl/dsl_proxies/parameter_block.rb +6 -2
- data/lib/dzl/dsl_subjects/router.rb +5 -1
- data/lib/dzl/examples/fun_with_global_params.rb +16 -0
- data/lib/dzl/examples/fun_with_params.rb +13 -0
- data/lib/dzl/request.rb +1 -1
- data/lib/dzl/version.rb +1 -1
- data/spec/fun_with_global_params_spec.rb +40 -0
- data/spec/fun_with_handlers_spec.rb +7 -0
- data/spec/fun_with_params_spec.rb +47 -1
- data/spec/trey_spec.rb +3 -3
- metadata +26 -7
@@ -61,10 +61,14 @@ class Dzl::DSLProxies::ParameterBlock < Dzl::DSLProxy
|
|
61
61
|
def import_pblock(*pblocks)
|
62
62
|
pblocks.each do |pblock|
|
63
63
|
next unless @subject.router.pblocks.has_key?(pblock)
|
64
|
-
@subject.router.pblocks[pblock]
|
64
|
+
pb = @subject.router.pblocks[pblock]
|
65
|
+
pb.params.each do |name, param|
|
65
66
|
@subject.params[name] = param.clone
|
66
67
|
end
|
68
|
+
pb.opts.each do |name, opt|
|
69
|
+
@subject.opts[name] = opt.clone
|
70
|
+
end
|
67
71
|
end
|
68
72
|
end
|
69
73
|
alias_method :import_parameters, :import_pblock
|
70
|
-
end
|
74
|
+
end
|
@@ -88,7 +88,11 @@ class Dzl::DSLSubjects::Router < Dzl::DSLSubject
|
|
88
88
|
)
|
89
89
|
true
|
90
90
|
else
|
91
|
-
|
91
|
+
if validation.error == :request_method_not_supported
|
92
|
+
errors[endpoint.route] ||= :request_method_not_supported
|
93
|
+
else
|
94
|
+
errors[endpoint.route] = validation.error
|
95
|
+
end
|
92
96
|
false
|
93
97
|
end
|
94
98
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'dzl/examples/base'
|
2
|
+
|
3
|
+
class Dzl::Examples::FunWithGlobalParams < Dzl::Examples::Base
|
4
|
+
defaults do
|
5
|
+
content_type 'application/json'
|
6
|
+
end
|
7
|
+
|
8
|
+
global_pblock do
|
9
|
+
protect do
|
10
|
+
api_key header: 'x_api_key', valid_keys: ['valid-key']
|
11
|
+
end
|
12
|
+
required(:number) {type Fixnum}
|
13
|
+
end
|
14
|
+
|
15
|
+
post '/globally_protected'
|
16
|
+
end
|
@@ -54,6 +54,19 @@ class Dzl::Examples::FunWithParams < Dzl::Examples::Base
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
+
get '/multi_endpoint_route' do
|
58
|
+
protect do
|
59
|
+
api_key header: 'x_api_key', valid_keys: ['valid-key']
|
60
|
+
end
|
61
|
+
handle{'OK'}
|
62
|
+
end
|
63
|
+
|
64
|
+
post '/multi_endpoint_route' do
|
65
|
+
required(:number) { type Fixnum }
|
66
|
+
end
|
67
|
+
|
68
|
+
put '/multi_endpoint_route'
|
69
|
+
|
57
70
|
endpoint '/api_proc' do
|
58
71
|
protect do
|
59
72
|
api_key header: 'x_api_key', validate_with: lambda {|key| key.match(/valid/)}
|
data/lib/dzl/request.rb
CHANGED
@@ -68,7 +68,7 @@ class Dzl::Request < Rack::Request
|
|
68
68
|
protected
|
69
69
|
def preformatted_params
|
70
70
|
@preformatted_params ||= begin
|
71
|
-
if content_type == "application/json"
|
71
|
+
if content_type == "application/json" && !body.blank?
|
72
72
|
JSON.parse(body).recursively_symbolize_keys!
|
73
73
|
else
|
74
74
|
{}
|
data/lib/dzl/version.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rack/test'
|
3
|
+
require 'dzl/examples/fun_with_global_params'
|
4
|
+
|
5
|
+
describe Dzl::Examples::FunWithGlobalParams do
|
6
|
+
include Rack::Test::Methods
|
7
|
+
|
8
|
+
def app; Dzl::Examples::FunWithGlobalParams; end
|
9
|
+
|
10
|
+
describe '/globally_protected' do
|
11
|
+
before(:each) {header 'Content-Type', 'application/json'}
|
12
|
+
let(:post_body) {{'number' => 23}.to_json}
|
13
|
+
let(:valid_key) {{'HTTP_X_API_KEY' => 'valid-key'}}
|
14
|
+
|
15
|
+
it 'should 401 if no api key provided' do
|
16
|
+
post '/globally_protected'
|
17
|
+
last_response.status.should == 401
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should 401 if invalid api key provided' do
|
21
|
+
post '/globally_protected', {}, {"HTTP_X_API_KEY" => 'invalid-key'}
|
22
|
+
last_response.status.should == 401
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should accept valid api key' do
|
26
|
+
post '/globally_protected', post_body, valid_key
|
27
|
+
last_response.status.should == 200
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should reject invalid required parameter' do
|
31
|
+
invalid_body = {'number' => 'string'}.to_json
|
32
|
+
post('/globally_protected', invalid_body, valid_key) do |response|
|
33
|
+
response.status.should == 404
|
34
|
+
JSON.parse(response.body)['errors']['/globally_protected'].should == {
|
35
|
+
'number' => 'type_conversion_error'
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -29,6 +29,13 @@ describe 'handlers' do
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
it "should handle empty body for content-type application/json" do
|
33
|
+
get '/say_bar?baz=no&bam=nope&bar=Hello%2C%20world', '', {"CONTENT_TYPE" => "application/json"} do |response|
|
34
|
+
response.status.should == 200
|
35
|
+
response.body.should == 'Hello, world'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
32
39
|
it 'calls error hooks on errors' do
|
33
40
|
Object.should_receive(:first_error_hook)
|
34
41
|
Object.should_receive(:second_error_hook)
|
@@ -110,7 +110,7 @@ describe Dzl::Examples::FunWithParams do
|
|
110
110
|
it 'converts :bar to time' do
|
111
111
|
get '/foo/2012-01-01' do |response|
|
112
112
|
response.status.should == 200
|
113
|
-
JSON.parse(response.body)['params']['bar'].should == '2012-01-01T00:00:00
|
113
|
+
Time.parse(JSON.parse(response.body)['params']['bar']).should == Time.parse('2012-01-01T00:00:00')
|
114
114
|
end
|
115
115
|
end
|
116
116
|
end
|
@@ -189,6 +189,52 @@ describe Dzl::Examples::FunWithParams do
|
|
189
189
|
end
|
190
190
|
end
|
191
191
|
|
192
|
+
describe 'get /multi_endpoint_route' do
|
193
|
+
it 'should 401 if no api key provided' do
|
194
|
+
get '/multi_endpoint_route'
|
195
|
+
last_response.status.should == 401
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'should 401 if invalid api key provided' do
|
199
|
+
get '/multi_endpoint_route', {}, {"HTTP_X_API_KEY" => 'invalid-key'}
|
200
|
+
last_response.status.should == 401
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'should accept valid api key' do
|
204
|
+
get '/multi_endpoint_route', {}, {"HTTP_X_API_KEY" => 'valid-key'}
|
205
|
+
last_response.status.should == 200
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
describe 'post /multi_endpoint_route' do
|
210
|
+
it 'should not allow a request with no parameters in the body' do
|
211
|
+
header 'Content-Type', 'application/json'
|
212
|
+
post('/multi_endpoint_route', {}.to_json) do |response|
|
213
|
+
response.status.should == 404
|
214
|
+
JSON.parse(response.body)['errors']['/multi_endpoint_route'].should == {
|
215
|
+
'number' => 'missing_required_param'
|
216
|
+
}
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'should not allow string instead of number' do
|
221
|
+
header 'Content-Type', 'application/json'
|
222
|
+
post('/multi_endpoint_route', {'number' => 'string'}.to_json) do |response|
|
223
|
+
response.status.should == 404
|
224
|
+
JSON.parse(response.body)['errors']['/multi_endpoint_route'].should == {
|
225
|
+
'number' => 'type_conversion_error'
|
226
|
+
}
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
describe 'put /multi_endpoint_route' do
|
232
|
+
it 'should return a valid response' do
|
233
|
+
put '/multi_endpoint_route', {}
|
234
|
+
last_response.status.should == 200
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
192
238
|
describe '/api_proc' do
|
193
239
|
it 'should 401 if no api key provided' do
|
194
240
|
get '/api_proc'
|
data/spec/trey_spec.rb
CHANGED
@@ -85,11 +85,11 @@ describe 'trey support' do
|
|
85
85
|
|
86
86
|
it "understands time parameters" do
|
87
87
|
get('/page_insights', req_params.merge({
|
88
|
-
since:
|
89
|
-
until:
|
88
|
+
since: Time.parse(req_params[:since]),
|
89
|
+
until: Time.parse(req_params[:until]),
|
90
90
|
})) do |response|
|
91
91
|
response.status.should == 200
|
92
|
-
JSON.parse(response.body)['params']['since'].should == '2012-01-01'
|
92
|
+
Time.parse(JSON.parse(response.body)['params']['since']).should == Time.parse('2012-01-01')
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dzl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-07-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rack
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,15 @@ dependencies:
|
|
22
22
|
version: 1.4.1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 1.4.1
|
26
31
|
- !ruby/object:Gem::Dependency
|
27
32
|
name: activesupport
|
28
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
none: false
|
30
35
|
requirements:
|
31
36
|
- - ~>
|
@@ -33,7 +38,12 @@ dependencies:
|
|
33
38
|
version: 3.2.2
|
34
39
|
type: :runtime
|
35
40
|
prerelease: false
|
36
|
-
version_requirements:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.2.2
|
37
47
|
description: Small, fast racktivesupport web framework with handy DSL and explicit
|
38
48
|
parameter validation.
|
39
49
|
email:
|
@@ -78,6 +88,7 @@ files:
|
|
78
88
|
- lib/dzl/dsl_subjects/scope.rb
|
79
89
|
- lib/dzl/errors.rb
|
80
90
|
- lib/dzl/examples/base.rb
|
91
|
+
- lib/dzl/examples/fun_with_global_params.rb
|
81
92
|
- lib/dzl/examples/fun_with_handlers.rb
|
82
93
|
- lib/dzl/examples/fun_with_hashes.rb
|
83
94
|
- lib/dzl/examples/fun_with_hooks.rb
|
@@ -105,6 +116,7 @@ files:
|
|
105
116
|
- lib/hash_validator/hash_validator.rb
|
106
117
|
- spec/dsl_subject_spec.rb
|
107
118
|
- spec/endpoint_doc_spec.rb
|
119
|
+
- spec/fun_with_global_params_spec.rb
|
108
120
|
- spec/fun_with_handlers_spec.rb
|
109
121
|
- spec/fun_with_hashes_spec.rb
|
110
122
|
- spec/fun_with_hooks_spec.rb
|
@@ -133,21 +145,28 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
145
|
- - ! '>='
|
134
146
|
- !ruby/object:Gem::Version
|
135
147
|
version: '0'
|
148
|
+
segments:
|
149
|
+
- 0
|
150
|
+
hash: -426579451222087061
|
136
151
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
152
|
none: false
|
138
153
|
requirements:
|
139
154
|
- - ! '>='
|
140
155
|
- !ruby/object:Gem::Version
|
141
156
|
version: '0'
|
157
|
+
segments:
|
158
|
+
- 0
|
159
|
+
hash: -426579451222087061
|
142
160
|
requirements: []
|
143
161
|
rubyforge_project:
|
144
|
-
rubygems_version: 1.8.
|
162
|
+
rubygems_version: 1.8.24
|
145
163
|
signing_key:
|
146
164
|
specification_version: 3
|
147
165
|
summary: Parameter validation and request routing DSL & web framework
|
148
166
|
test_files:
|
149
167
|
- spec/dsl_subject_spec.rb
|
150
168
|
- spec/endpoint_doc_spec.rb
|
169
|
+
- spec/fun_with_global_params_spec.rb
|
151
170
|
- spec/fun_with_handlers_spec.rb
|
152
171
|
- spec/fun_with_hashes_spec.rb
|
153
172
|
- spec/fun_with_hooks_spec.rb
|