dzl 1.0.0.beta3 → 1.0.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dzl/dsl_proxies/router.rb +1 -0
- data/lib/dzl/dsl_subjects/endpoint.rb +3 -3
- data/lib/dzl/dsl_subjects/parameter.rb +16 -2
- data/lib/dzl/dsl_subjects/parameter_block.rb +7 -5
- data/lib/dzl/examples/fun_with_params.rb +8 -1
- data/lib/dzl/request.rb +16 -2
- data/lib/dzl/version.rb +1 -1
- data/spec/fun_with_params_spec.rb +39 -1
- data/spec/fun_with_requests_spec.rb +8 -1
- metadata +6 -6
@@ -71,9 +71,9 @@ class Dzl::DSLSubjects::Endpoint < Dzl::DSLSubject
|
|
71
71
|
@route_splits = @route.split('/').select{|s| not s.empty?}
|
72
72
|
|
73
73
|
route_regex_string = @route_splits.collect do |route_part|
|
74
|
-
route_part.starts_with?(':') ? "
|
75
|
-
end.push('
|
74
|
+
route_part.starts_with?(':') ? "/[a-zA-Z0-9._~-]+" : "/#{route_part}"
|
75
|
+
end.push('/?$').join('')
|
76
76
|
|
77
77
|
@route_regex = Regexp.new('^' + route_regex_string)
|
78
78
|
end
|
79
|
-
end
|
79
|
+
end
|
@@ -47,7 +47,7 @@ class Dzl::DSLSubjects::Parameter < Dzl::DSLSubject
|
|
47
47
|
# Returns a symbol describe the error if error,
|
48
48
|
# returns the transformed value if not
|
49
49
|
# TODO symbol values?
|
50
|
-
def validate(input)
|
50
|
+
def validate(input, opts = {})
|
51
51
|
# Validate type
|
52
52
|
unless input
|
53
53
|
if @opts[:required]
|
@@ -61,7 +61,21 @@ class Dzl::DSLSubjects::Parameter < Dzl::DSLSubject
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
input =
|
64
|
+
input = begin
|
65
|
+
if opts[:preformatted]
|
66
|
+
if input.is_a?(param_type)
|
67
|
+
Dzl::ValueOrError.new(
|
68
|
+
v: input
|
69
|
+
)
|
70
|
+
else
|
71
|
+
Dzl::ValueOrError.new(
|
72
|
+
e: :type_conversion_error
|
73
|
+
)
|
74
|
+
end
|
75
|
+
else
|
76
|
+
convert_type(input)
|
77
|
+
end
|
78
|
+
end
|
65
79
|
return input if input.error?
|
66
80
|
|
67
81
|
input = prevalidate_transform(input.value)
|
@@ -17,12 +17,14 @@ class Dzl::DSLSubjects::ParameterBlock < Dzl::DSLSubject
|
|
17
17
|
pname, param = pary
|
18
18
|
parandidate_key = param.opts[:header] ? :headers : :params
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
param = @params[pname].validate(parandidates[parandidate_key][pname], {
|
21
|
+
preformatted: request.preformatted_keys.include?(pname)
|
22
|
+
})
|
23
|
+
|
24
|
+
if param.error?
|
25
|
+
errors[pname] = param.error
|
24
26
|
else
|
25
|
-
parandidates[parandidate_key][pname] =
|
27
|
+
parandidates[parandidate_key][pname] = param.value unless param.value == :__no_value__
|
26
28
|
end
|
27
29
|
end || {}
|
28
30
|
|
@@ -68,4 +68,11 @@ class Dzl::Examples::FunWithParams < Dzl::Examples::Base
|
|
68
68
|
end
|
69
69
|
|
70
70
|
endpoint '/foo/:bar'
|
71
|
-
|
71
|
+
|
72
|
+
endpoint '/rofl/:copter', :post do
|
73
|
+
optional(:candy) { type Array }
|
74
|
+
optional(:cookies, :sunshine) { type Fixnum }
|
75
|
+
optional :more
|
76
|
+
required :sunshine, :steak
|
77
|
+
end
|
78
|
+
end
|
data/lib/dzl/request.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
class Dzl::Request < Rack::Request
|
2
2
|
attr_accessor :silent
|
3
|
+
attr_reader :preformatted_keys
|
3
4
|
|
4
5
|
def initialize(env)
|
5
6
|
super(env)
|
6
7
|
|
7
8
|
@endpoints = Hash.new({})
|
9
|
+
@preformatted_keys = []
|
8
10
|
end
|
9
11
|
|
10
12
|
def headers
|
@@ -19,7 +21,14 @@ class Dzl::Request < Rack::Request
|
|
19
21
|
end
|
20
22
|
|
21
23
|
def params
|
22
|
-
@params ||=
|
24
|
+
@params ||= begin
|
25
|
+
@request_body = body.read
|
26
|
+
unless preformatted_params.empty?
|
27
|
+
@preformatted_keys = preformatted_params.keys.collect {|k| k.to_sym}
|
28
|
+
end
|
29
|
+
|
30
|
+
super.merge(preformatted_params)
|
31
|
+
end
|
23
32
|
end
|
24
33
|
|
25
34
|
def overwrite_headers(new_headers)
|
@@ -51,4 +60,9 @@ class Dzl::Request < Rack::Request
|
|
51
60
|
def silent?
|
52
61
|
@silent == true
|
53
62
|
end
|
54
|
-
|
63
|
+
|
64
|
+
protected
|
65
|
+
def preformatted_params
|
66
|
+
@preformatted_params ||= (content_type == "application/json") ? JSON.parse(@request_body) : {}
|
67
|
+
end
|
68
|
+
end
|
data/lib/dzl/version.rb
CHANGED
@@ -200,4 +200,42 @@ describe Dzl::Examples::FunWithParams do
|
|
200
200
|
end
|
201
201
|
end
|
202
202
|
end
|
203
|
-
|
203
|
+
|
204
|
+
describe '/rofl/:copter' do
|
205
|
+
it 'infers json-encoded body parameters' do
|
206
|
+
example_body = {
|
207
|
+
'candy' => [1,3,4],
|
208
|
+
'cookies' => 7,
|
209
|
+
'steak' => 'eww',
|
210
|
+
'sunshine' => 9
|
211
|
+
}.to_json
|
212
|
+
header "Content-Type", "application/json"
|
213
|
+
post('/rofl/haha?more=vars', example_body) do |response|
|
214
|
+
response.successful?.should be_true
|
215
|
+
params = JSON.parse(response.body)['params']
|
216
|
+
params['copter'].should == 'haha'
|
217
|
+
params['candy'].should == [1, 3, 4]
|
218
|
+
params['sunshine'].should == 9
|
219
|
+
params['more'].should == "vars"
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'still validates types' do
|
224
|
+
example_body = {
|
225
|
+
'candy' => 'this-will-be-a-one-element-array',
|
226
|
+
'cookies' => ['o', 'o', 'p', 's'],
|
227
|
+
'steak' => 'eww',
|
228
|
+
'sunshine' => 9
|
229
|
+
}.to_json
|
230
|
+
header "Content-Type", "application/json"
|
231
|
+
post('/rofl/haha?more=vars', example_body) do |response|
|
232
|
+
response.successful?.should be_false
|
233
|
+
errors = JSON.parse(response.body)['errors']['/rofl/:copter']
|
234
|
+
errors.should == {
|
235
|
+
'cookies' => 'type_conversion_error',
|
236
|
+
'candy' => 'type_conversion_error'
|
237
|
+
}
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
@@ -13,6 +13,13 @@ describe 'endpoint request method' do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
it 'ignores an extra trailing slash' do
|
17
|
+
get('/foo/') do |response|
|
18
|
+
response.status.should == 200
|
19
|
+
response.body.should == 'get'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
16
23
|
it 'allows specification through endpoint options' do
|
17
24
|
post('/post_op') do |response|
|
18
25
|
response.status.should == 200
|
@@ -98,4 +105,4 @@ describe 'endpoint request method' do
|
|
98
105
|
end
|
99
106
|
end
|
100
107
|
end
|
101
|
-
end
|
108
|
+
end
|
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.0.
|
4
|
+
version: 1.0.0.beta4
|
5
5
|
prerelease: 6
|
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-03-
|
13
|
+
date: 2012-03-26 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rack
|
17
|
-
requirement: &
|
17
|
+
requirement: &70279829324860 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: 1.4.1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70279829324860
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: activesupport
|
28
|
-
requirement: &
|
28
|
+
requirement: &70279829322760 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: 3.2.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70279829322760
|
37
37
|
description: Small, fast racktivesupport web framework with handy DSL and explicit
|
38
38
|
parameter validation.
|
39
39
|
email:
|