grape 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of grape might be problematic. Click here for more details.
- checksums.yaml +8 -8
- data/.rubocop.yml +65 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +17 -1
- data/Gemfile +1 -0
- data/README.md +16 -8
- data/RELEASING.md +105 -0
- data/grape.gemspec +1 -1
- data/lib/grape.rb +1 -0
- data/lib/grape/api.rb +88 -54
- data/lib/grape/cookies.rb +4 -6
- data/lib/grape/endpoint.rb +81 -69
- data/lib/grape/error_formatter/base.rb +5 -4
- data/lib/grape/error_formatter/json.rb +3 -3
- data/lib/grape/error_formatter/txt.rb +1 -1
- data/lib/grape/error_formatter/xml.rb +4 -4
- data/lib/grape/exceptions/base.rb +7 -7
- data/lib/grape/exceptions/incompatible_option_values.rb +13 -0
- data/lib/grape/exceptions/invalid_formatter.rb +1 -1
- data/lib/grape/exceptions/invalid_versioner_option.rb +1 -1
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +1 -4
- data/lib/grape/exceptions/missing_mime_type.rb +1 -1
- data/lib/grape/exceptions/missing_option.rb +1 -1
- data/lib/grape/exceptions/missing_vendor_option.rb +1 -1
- data/lib/grape/exceptions/unknown_options.rb +1 -1
- data/lib/grape/exceptions/unknown_validator.rb +1 -1
- data/lib/grape/exceptions/validation.rb +2 -2
- data/lib/grape/exceptions/validation_errors.rb +1 -1
- data/lib/grape/formatter/base.rb +5 -4
- data/lib/grape/formatter/serializable_hash.rb +7 -6
- data/lib/grape/http/request.rb +2 -2
- data/lib/grape/locale/en.yml +2 -0
- data/lib/grape/middleware/auth/base.rb +3 -3
- data/lib/grape/middleware/auth/basic.rb +1 -1
- data/lib/grape/middleware/auth/oauth2.rb +18 -20
- data/lib/grape/middleware/base.rb +1 -1
- data/lib/grape/middleware/error.rb +19 -19
- data/lib/grape/middleware/filter.rb +3 -3
- data/lib/grape/middleware/formatter.rb +29 -23
- data/lib/grape/middleware/versioner.rb +1 -1
- data/lib/grape/middleware/versioner/accept_version_header.rb +8 -6
- data/lib/grape/middleware/versioner/header.rb +16 -14
- data/lib/grape/middleware/versioner/param.rb +7 -7
- data/lib/grape/middleware/versioner/path.rb +7 -9
- data/lib/grape/parser/base.rb +3 -2
- data/lib/grape/path.rb +1 -1
- data/lib/grape/route.rb +6 -4
- data/lib/grape/util/content_types.rb +2 -1
- data/lib/grape/util/deep_merge.rb +5 -5
- data/lib/grape/util/hash_stack.rb +2 -2
- data/lib/grape/validations.rb +34 -30
- data/lib/grape/validations/coerce.rb +6 -5
- data/lib/grape/validations/default.rb +0 -1
- data/lib/grape/validations/presence.rb +1 -1
- data/lib/grape/validations/regexp.rb +2 -2
- data/lib/grape/validations/values.rb +16 -0
- data/lib/grape/version.rb +1 -1
- data/spec/grape/api_spec.rb +229 -210
- data/spec/grape/endpoint_spec.rb +56 -54
- data/spec/grape/entity_spec.rb +31 -33
- data/spec/grape/exceptions/missing_mime_type_spec.rb +3 -9
- data/spec/grape/middleware/auth/basic_spec.rb +8 -8
- data/spec/grape/middleware/auth/digest_spec.rb +5 -5
- data/spec/grape/middleware/auth/oauth2_spec.rb +23 -23
- data/spec/grape/middleware/base_spec.rb +6 -6
- data/spec/grape/middleware/error_spec.rb +11 -15
- data/spec/grape/middleware/exception_spec.rb +45 -25
- data/spec/grape/middleware/formatter_spec.rb +56 -45
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +25 -25
- data/spec/grape/middleware/versioner/header_spec.rb +54 -54
- data/spec/grape/middleware/versioner/param_spec.rb +17 -18
- data/spec/grape/middleware/versioner/path_spec.rb +6 -6
- data/spec/grape/middleware/versioner_spec.rb +1 -1
- data/spec/grape/util/hash_stack_spec.rb +26 -27
- data/spec/grape/validations/coerce_spec.rb +39 -34
- data/spec/grape/validations/default_spec.rb +12 -13
- data/spec/grape/validations/presence_spec.rb +18 -22
- data/spec/grape/validations/regexp_spec.rb +9 -9
- data/spec/grape/validations/values_spec.rb +64 -0
- data/spec/grape/validations_spec.rb +127 -70
- data/spec/shared/versioning_examples.rb +5 -5
- data/spec/support/basic_auth_encode_helpers.rb +0 -1
- data/spec/support/versioned_helpers.rb +5 -6
- metadata +10 -4
data/spec/grape/endpoint_spec.rb
CHANGED
@@ -2,22 +2,25 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Grape::Endpoint do
|
4
4
|
subject { Class.new(Grape::API) }
|
5
|
-
|
5
|
+
|
6
|
+
def app
|
7
|
+
subject
|
8
|
+
end
|
6
9
|
|
7
10
|
describe '#initialize' do
|
8
11
|
it 'takes a settings stack, options, and a block' do
|
9
|
-
p =
|
12
|
+
p = proc { }
|
10
13
|
expect {
|
11
14
|
Grape::Endpoint.new(Grape::Util::HashStack.new, {
|
12
|
-
:
|
13
|
-
:
|
15
|
+
path: '/',
|
16
|
+
method: :get
|
14
17
|
}, &p)
|
15
18
|
}.not_to raise_error
|
16
19
|
end
|
17
20
|
end
|
18
21
|
|
19
22
|
it 'sets itself in the env upon call' do
|
20
|
-
subject.get('/'){ "Hello world." }
|
23
|
+
subject.get('/') { "Hello world." }
|
21
24
|
get '/'
|
22
25
|
last_request.env['api.endpoint'].should be_kind_of(Grape::Endpoint)
|
23
26
|
end
|
@@ -66,7 +69,7 @@ describe Grape::Endpoint do
|
|
66
69
|
end
|
67
70
|
it 'includes headers passed as symbols' do
|
68
71
|
env = Rack::MockRequest.env_for("/headers")
|
69
|
-
env[
|
72
|
+
env["HTTP_SYMBOL_HEADER".to_sym] = "Goliath passes symbols"
|
70
73
|
body = subject.call(env)[2].body.first
|
71
74
|
JSON.parse(body)["Symbol-Header"].should == "Goliath passes symbols"
|
72
75
|
end
|
@@ -77,10 +80,10 @@ describe Grape::Endpoint do
|
|
77
80
|
subject.get('/get/cookies') do
|
78
81
|
cookies['my-awesome-cookie1'] = 'is cool'
|
79
82
|
cookies['my-awesome-cookie2'] = {
|
80
|
-
:
|
81
|
-
:
|
82
|
-
:
|
83
|
-
:
|
83
|
+
value: 'is cool too',
|
84
|
+
domain: 'my.example.com',
|
85
|
+
path: '/',
|
86
|
+
secure: true,
|
84
87
|
}
|
85
88
|
cookies[:cookie3] = 'symbol'
|
86
89
|
cookies['cookie4'] = 'secret code here'
|
@@ -93,7 +96,7 @@ describe Grape::Endpoint do
|
|
93
96
|
"cookie4=secret+code+here",
|
94
97
|
"my-awesome-cookie1=is+cool",
|
95
98
|
"my-awesome-cookie2=is+cool+too; domain=my.example.com; path=/; secure"
|
96
|
-
|
99
|
+
]
|
97
100
|
end
|
98
101
|
|
99
102
|
it 'sets browser cookies and does not set response cookies' do
|
@@ -127,14 +130,14 @@ describe Grape::Endpoint do
|
|
127
130
|
end
|
128
131
|
sum
|
129
132
|
end
|
130
|
-
get
|
133
|
+
get '/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2'
|
131
134
|
last_response.body.should == '3'
|
132
135
|
cookies = Hash[last_response.headers['Set-Cookie'].split("\n").map do |set_cookie|
|
133
136
|
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
|
134
|
-
[
|
137
|
+
[cookie.name, cookie]
|
135
138
|
end]
|
136
139
|
cookies.size.should == 2
|
137
|
-
[
|
140
|
+
["and_this", "delete_this_cookie"].each do |cookie_name|
|
138
141
|
cookie = cookies[cookie_name]
|
139
142
|
cookie.should_not be_nil
|
140
143
|
cookie.value.should == "deleted"
|
@@ -147,7 +150,7 @@ describe Grape::Endpoint do
|
|
147
150
|
sum = 0
|
148
151
|
cookies.each do |name, val|
|
149
152
|
sum += val.to_i
|
150
|
-
cookies.delete name, { :
|
153
|
+
cookies.delete name, { path: '/test' }
|
151
154
|
end
|
152
155
|
sum
|
153
156
|
end
|
@@ -155,10 +158,10 @@ describe Grape::Endpoint do
|
|
155
158
|
last_response.body.should == '3'
|
156
159
|
cookies = Hash[last_response.headers['Set-Cookie'].split("\n").map do |set_cookie|
|
157
160
|
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
|
158
|
-
[
|
161
|
+
[cookie.name, cookie]
|
159
162
|
end]
|
160
163
|
cookies.size.should == 2
|
161
|
-
[
|
164
|
+
["and_this", "delete_this_cookie"].each do |cookie_name|
|
162
165
|
cookie = cookies[cookie_name]
|
163
166
|
cookie.should_not be_nil
|
164
167
|
cookie.value.should == "deleted"
|
@@ -173,7 +176,7 @@ describe Grape::Endpoint do
|
|
173
176
|
subject.params do
|
174
177
|
requires :first
|
175
178
|
optional :second
|
176
|
-
optional :third, :
|
179
|
+
optional :third, default: 'third-default'
|
177
180
|
group :nested do
|
178
181
|
optional :fourth
|
179
182
|
end
|
@@ -232,7 +235,7 @@ describe Grape::Endpoint do
|
|
232
235
|
|
233
236
|
it 'stringifies if that option is passed' do
|
234
237
|
subject.get '/declared' do
|
235
|
-
declared(params, :
|
238
|
+
declared(params, stringify: true)["first"].should == "one"
|
236
239
|
""
|
237
240
|
end
|
238
241
|
|
@@ -242,7 +245,7 @@ describe Grape::Endpoint do
|
|
242
245
|
|
243
246
|
it 'does not include missing attributes if that option is passed' do
|
244
247
|
subject.get '/declared' do
|
245
|
-
declared(params, :
|
248
|
+
error! 400, "expected nil" if declared(params, include_missing: false)[:second]
|
246
249
|
""
|
247
250
|
end
|
248
251
|
|
@@ -280,8 +283,8 @@ describe Grape::Endpoint do
|
|
280
283
|
|
281
284
|
context 'with special requirements' do
|
282
285
|
it 'parses email param with provided requirements for params' do
|
283
|
-
subject.get('/:person_email', :
|
284
|
-
|
286
|
+
subject.get('/:person_email', requirements: { person_email: /.*/ }) do
|
287
|
+
params[:person_email]
|
285
288
|
end
|
286
289
|
|
287
290
|
get '/someone@example.com'
|
@@ -292,11 +295,8 @@ describe Grape::Endpoint do
|
|
292
295
|
end
|
293
296
|
|
294
297
|
it 'parses many params with provided regexps' do
|
295
|
-
subject.get('/:person_email/test/:number',
|
296
|
-
:
|
297
|
-
:person_email => /someone@(.*).com/,
|
298
|
-
:number => /[0-9]/ }) do
|
299
|
-
params[:person_email] << params[:number]
|
298
|
+
subject.get('/:person_email/test/:number', requirements: { person_email: /someone@(.*).com/, number: /[0-9]/ }) do
|
299
|
+
params[:person_email] << params[:number]
|
300
300
|
end
|
301
301
|
|
302
302
|
get '/someone@example.com/test/1'
|
@@ -314,12 +314,12 @@ describe Grape::Endpoint do
|
|
314
314
|
|
315
315
|
context 'namespace requirements' do
|
316
316
|
before :each do
|
317
|
-
subject.namespace :outer, :
|
317
|
+
subject.namespace :outer, requirements: { person_email: /abc@(.*).com/ } do
|
318
318
|
get('/:person_email') do
|
319
319
|
params[:person_email]
|
320
320
|
end
|
321
321
|
|
322
|
-
namespace :inner, :
|
322
|
+
namespace :inner, requirements: { number: /[0-9]/, person_email: /someone@(.*).com/ }do
|
323
323
|
get '/:person_email/test/:number' do
|
324
324
|
params[:person_email] << params[:number]
|
325
325
|
end
|
@@ -354,31 +354,33 @@ describe Grape::Endpoint do
|
|
354
354
|
end
|
355
355
|
|
356
356
|
it 'converts JSON bodies to params' do
|
357
|
-
post '/request_body', MultiJson.dump(:
|
357
|
+
post '/request_body', MultiJson.dump(user: 'Bobby T.'), { 'CONTENT_TYPE' => 'application/json' }
|
358
358
|
last_response.body.should == 'Bobby T.'
|
359
359
|
end
|
360
360
|
|
361
361
|
it 'does not convert empty JSON bodies to params' do
|
362
|
-
put '/request_body', '', {'CONTENT_TYPE' => 'application/json'}
|
362
|
+
put '/request_body', '', { 'CONTENT_TYPE' => 'application/json' }
|
363
363
|
last_response.body.should == ''
|
364
364
|
end
|
365
365
|
|
366
366
|
it 'converts XML bodies to params' do
|
367
|
-
post '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
|
367
|
+
post '/request_body', '<user>Bobby T.</user>', { 'CONTENT_TYPE' => 'application/xml' }
|
368
368
|
last_response.body.should == 'Bobby T.'
|
369
369
|
end
|
370
370
|
|
371
371
|
it 'converts XML bodies to params' do
|
372
|
-
put '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
|
372
|
+
put '/request_body', '<user>Bobby T.</user>', { 'CONTENT_TYPE' => 'application/xml' }
|
373
373
|
last_response.body.should == 'Bobby T.'
|
374
374
|
end
|
375
375
|
|
376
376
|
it 'does not include parameters not defined by the body' do
|
377
377
|
subject.post '/omitted_params' do
|
378
|
-
params[:version]
|
379
|
-
params[:user]
|
378
|
+
error! 400, "expected nil" if params[:version]
|
379
|
+
params[:user]
|
380
380
|
end
|
381
|
-
post '/omitted_params', MultiJson.dump(:
|
381
|
+
post '/omitted_params', MultiJson.dump(user: 'Bob'), { 'CONTENT_TYPE' => 'application/json' }
|
382
|
+
last_response.status.should == 201
|
383
|
+
last_response.body.should == "Bob"
|
382
384
|
end
|
383
385
|
end
|
384
386
|
|
@@ -388,7 +390,7 @@ describe Grape::Endpoint do
|
|
388
390
|
subject.put '/request_body' do
|
389
391
|
params[:user]
|
390
392
|
end
|
391
|
-
put '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
|
393
|
+
put '/request_body', '<user>Bobby T.</user>', { 'CONTENT_TYPE' => 'application/xml' }
|
392
394
|
last_response.status.should == 406
|
393
395
|
last_response.body.should == '{"error":"The requested content-type \'application/xml\' is not supported."}'
|
394
396
|
end
|
@@ -419,7 +421,7 @@ describe Grape::Endpoint do
|
|
419
421
|
|
420
422
|
it 'accepts an object and render it in format' do
|
421
423
|
subject.get '/hey' do
|
422
|
-
error!({'dude' => 'rad'}, 403)
|
424
|
+
error!({ 'dude' => 'rad' }, 403)
|
423
425
|
end
|
424
426
|
|
425
427
|
get '/hey.json'
|
@@ -450,7 +452,7 @@ describe Grape::Endpoint do
|
|
450
452
|
|
451
453
|
it 'support permanent redirect' do
|
452
454
|
subject.get('/hey') do
|
453
|
-
redirect "/ha", :
|
455
|
+
redirect "/ha", permanent: true
|
454
456
|
end
|
455
457
|
get '/hey'
|
456
458
|
last_response.status.should eq 301
|
@@ -464,10 +466,10 @@ describe Grape::Endpoint do
|
|
464
466
|
params[:text]
|
465
467
|
end
|
466
468
|
|
467
|
-
post '/new', :
|
469
|
+
post '/new', text: 'abc'
|
468
470
|
last_response.body.should == 'abc'
|
469
471
|
|
470
|
-
post '/new', :
|
472
|
+
post '/new', text: 'def'
|
471
473
|
last_response.body.should == 'def'
|
472
474
|
end
|
473
475
|
|
@@ -501,7 +503,7 @@ describe Grape::Endpoint do
|
|
501
503
|
describe '.generate_api_method' do
|
502
504
|
it 'raises NameError if the method name is already in use' do
|
503
505
|
expect {
|
504
|
-
Grape::Endpoint.generate_api_method("version", &proc{})
|
506
|
+
Grape::Endpoint.generate_api_method("version", &proc { })
|
505
507
|
}.to raise_error(NameError)
|
506
508
|
end
|
507
509
|
it 'raises ArgumentError if a block is not given' do
|
@@ -510,15 +512,15 @@ describe Grape::Endpoint do
|
|
510
512
|
}.to raise_error(ArgumentError)
|
511
513
|
end
|
512
514
|
it 'returns a Proc' do
|
513
|
-
Grape::Endpoint.generate_api_method("GET test for a proc", &proc{}).should be_a Proc
|
515
|
+
Grape::Endpoint.generate_api_method("GET test for a proc", &proc { }).should be_a Proc
|
514
516
|
end
|
515
517
|
end
|
516
518
|
|
517
519
|
context 'filters' do
|
518
520
|
describe 'before filters' do
|
519
521
|
it 'runs the before filter if set' do
|
520
|
-
subject.before{ env['before_test'] = "OK" }
|
521
|
-
subject.get('/before_test'){ env['before_test'] }
|
522
|
+
subject.before { env['before_test'] = "OK" }
|
523
|
+
subject.get('/before_test') { env['before_test'] }
|
522
524
|
|
523
525
|
get '/before_test'
|
524
526
|
last_response.body.should == "OK"
|
@@ -527,15 +529,15 @@ describe Grape::Endpoint do
|
|
527
529
|
|
528
530
|
describe 'after filters' do
|
529
531
|
it 'overrides the response body if it sets it' do
|
530
|
-
subject.after{ body "after" }
|
531
|
-
subject.get('/after_test'){ "during" }
|
532
|
+
subject.after { body "after" }
|
533
|
+
subject.get('/after_test') { "during" }
|
532
534
|
get '/after_test'
|
533
535
|
last_response.body.should == 'after'
|
534
536
|
end
|
535
537
|
|
536
538
|
it 'does not override the response body with its return' do
|
537
|
-
subject.after{ "after" }
|
538
|
-
subject.get('/after_test'){ "body" }
|
539
|
+
subject.after { "after" }
|
540
|
+
subject.get('/after_test') { "body" }
|
539
541
|
get '/after_test'
|
540
542
|
last_response.body.should == "body"
|
541
543
|
end
|
@@ -547,7 +549,7 @@ describe Grape::Endpoint do
|
|
547
549
|
|
548
550
|
verbs.each do |verb|
|
549
551
|
it 'allows for the anchoring option with a #{verb.upcase} method' do
|
550
|
-
subject.send(verb, '/example', :
|
552
|
+
subject.send(verb, '/example', anchor: true) do
|
551
553
|
verb
|
552
554
|
end
|
553
555
|
send(verb, '/example/and/some/more')
|
@@ -563,7 +565,7 @@ describe Grape::Endpoint do
|
|
563
565
|
end
|
564
566
|
|
565
567
|
it 'responds to /example/and/some/more for the non-anchored #{verb.upcase} method' do
|
566
|
-
subject.send(verb, '/example', :
|
568
|
+
subject.send(verb, '/example', anchor: false) do
|
567
569
|
verb
|
568
570
|
end
|
569
571
|
send(verb, '/example/and/some/more')
|
@@ -581,9 +583,9 @@ describe Grape::Endpoint do
|
|
581
583
|
get '/url'
|
582
584
|
last_response.body.should == "http://example.org/url"
|
583
585
|
end
|
584
|
-
[
|
586
|
+
['v1', :v1].each do |version|
|
585
587
|
it 'should include version #{version}' do
|
586
|
-
subject.version version, :
|
588
|
+
subject.version version, using: :path
|
587
589
|
subject.get('/url') do
|
588
590
|
request.url
|
589
591
|
end
|
@@ -592,7 +594,7 @@ describe Grape::Endpoint do
|
|
592
594
|
end
|
593
595
|
end
|
594
596
|
it 'should include prefix' do
|
595
|
-
subject.version 'v1', :
|
597
|
+
subject.version 'v1', using: :path
|
596
598
|
subject.prefix 'api'
|
597
599
|
subject.get('/url') do
|
598
600
|
request.url
|
data/spec/grape/entity_spec.rb
CHANGED
@@ -3,13 +3,16 @@ require 'grape_entity'
|
|
3
3
|
|
4
4
|
describe Grape::Entity do
|
5
5
|
subject { Class.new(Grape::API) }
|
6
|
-
|
6
|
+
|
7
|
+
def app
|
8
|
+
subject
|
9
|
+
end
|
7
10
|
|
8
11
|
describe '#present' do
|
9
12
|
it 'sets the object as the body if no options are provided' do
|
10
13
|
subject.get '/example' do
|
11
|
-
present(
|
12
|
-
body.should == {:
|
14
|
+
present(abc: 'def')
|
15
|
+
body.should == { abc: 'def' }
|
13
16
|
end
|
14
17
|
get '/example'
|
15
18
|
end
|
@@ -18,7 +21,7 @@ describe Grape::Entity do
|
|
18
21
|
subject.get '/example' do
|
19
22
|
entity_mock = Object.new
|
20
23
|
entity_mock.should_receive(:represent)
|
21
|
-
present Object.new, :
|
24
|
+
present Object.new, with: entity_mock
|
22
25
|
end
|
23
26
|
get '/example'
|
24
27
|
end
|
@@ -27,7 +30,7 @@ describe Grape::Entity do
|
|
27
30
|
entity = Class.new(Grape::Entity)
|
28
31
|
entity.stub(:represent).and_return("Hiya")
|
29
32
|
|
30
|
-
subject.represent Object, :
|
33
|
+
subject.represent Object, with: entity
|
31
34
|
subject.get '/example' do
|
32
35
|
present Object.new
|
33
36
|
end
|
@@ -39,12 +42,16 @@ describe Grape::Entity do
|
|
39
42
|
entity = Class.new(Grape::Entity)
|
40
43
|
entity.stub(:represent).and_return("Hiya")
|
41
44
|
|
42
|
-
class TestObject
|
45
|
+
class TestObject
|
46
|
+
end
|
47
|
+
|
43
48
|
class FakeCollection
|
44
|
-
def first
|
49
|
+
def first
|
50
|
+
TestObject.new
|
51
|
+
end
|
45
52
|
end
|
46
53
|
|
47
|
-
subject.represent TestObject, :
|
54
|
+
subject.represent TestObject, with: entity
|
48
55
|
subject.get '/example' do
|
49
56
|
present [TestObject.new]
|
50
57
|
end
|
@@ -66,7 +73,7 @@ describe Grape::Entity do
|
|
66
73
|
|
67
74
|
subclass = Class.new(Object)
|
68
75
|
|
69
|
-
subject.represent Object, :
|
76
|
+
subject.represent Object, with: entity
|
70
77
|
subject.get '/example' do
|
71
78
|
present subclass.new
|
72
79
|
end
|
@@ -104,13 +111,13 @@ describe Grape::Entity do
|
|
104
111
|
|
105
112
|
it 'adds a root key to the output if one is given' do
|
106
113
|
subject.get '/example' do
|
107
|
-
present({:
|
108
|
-
body.should == {:
|
114
|
+
present({ abc: 'def' }, root: :root)
|
115
|
+
body.should == { root: { abc: 'def' } }
|
109
116
|
end
|
110
117
|
get '/example'
|
111
118
|
end
|
112
119
|
|
113
|
-
[
|
120
|
+
[:json, :serializable_hash].each do |format|
|
114
121
|
|
115
122
|
it 'presents with #{format}' do
|
116
123
|
entity = Class.new(Grape::Entity)
|
@@ -125,7 +132,7 @@ describe Grape::Entity do
|
|
125
132
|
@id = id
|
126
133
|
end
|
127
134
|
end
|
128
|
-
present c.new(1), :
|
135
|
+
present c.new(1), with: entity
|
129
136
|
end
|
130
137
|
|
131
138
|
get '/example'
|
@@ -146,8 +153,8 @@ describe Grape::Entity do
|
|
146
153
|
@id = id
|
147
154
|
end
|
148
155
|
end
|
149
|
-
examples = [
|
150
|
-
present examples, :
|
156
|
+
examples = [c.new(1), c.new(2)]
|
157
|
+
present examples, with: entity
|
151
158
|
end
|
152
159
|
|
153
160
|
get '/examples'
|
@@ -171,7 +178,7 @@ describe Grape::Entity do
|
|
171
178
|
@name = args[:name] || "no name set"
|
172
179
|
end
|
173
180
|
end
|
174
|
-
present c.new(
|
181
|
+
present c.new(name: "johnnyiller"), with: entity
|
175
182
|
end
|
176
183
|
get '/example'
|
177
184
|
last_response.status.should == 200
|
@@ -200,7 +207,7 @@ XML
|
|
200
207
|
@name = args[:name] || "no name set"
|
201
208
|
end
|
202
209
|
end
|
203
|
-
present c.new(
|
210
|
+
present c.new(name: "johnnyiller"), with: entity
|
204
211
|
end
|
205
212
|
get '/example'
|
206
213
|
last_response.status.should == 200
|
@@ -229,7 +236,7 @@ XML
|
|
229
236
|
end
|
230
237
|
end
|
231
238
|
|
232
|
-
present c.new(
|
239
|
+
present c.new(name: "johnnyiller"), with: entity
|
233
240
|
end
|
234
241
|
|
235
242
|
get '/example?callback=abcDef'
|
@@ -239,12 +246,6 @@ XML
|
|
239
246
|
end
|
240
247
|
|
241
248
|
context "present with multiple entities" do
|
242
|
-
let(:user) do
|
243
|
-
end
|
244
|
-
|
245
|
-
before :each do
|
246
|
-
end
|
247
|
-
|
248
249
|
it "present with multiple entities using optional symbol" do
|
249
250
|
user = Class.new do
|
250
251
|
attr_reader :name
|
@@ -252,8 +253,8 @@ XML
|
|
252
253
|
@name = args[:name] || "no name set"
|
253
254
|
end
|
254
255
|
end
|
255
|
-
user1 = user.new(
|
256
|
-
user2 = user.new(
|
256
|
+
user1 = user.new(name: 'user1')
|
257
|
+
user2 = user.new(name: 'user2')
|
257
258
|
|
258
259
|
entity = Class.new(Grape::Entity)
|
259
260
|
entity.expose :name
|
@@ -261,21 +262,18 @@ XML
|
|
261
262
|
subject.format :json
|
262
263
|
subject.get '/example' do
|
263
264
|
present :page, 1
|
264
|
-
present :user1, user1, :
|
265
|
-
present :user2, user2, :
|
265
|
+
present :user1, user1, with: entity
|
266
|
+
present :user2, user2, with: entity
|
266
267
|
end
|
267
268
|
get '/example'
|
268
269
|
expect_response_json = {
|
269
270
|
"page" => 1,
|
270
|
-
"user1" => {"name" => "user1"},
|
271
|
-
"user2" => {"name" => "user2"}
|
271
|
+
"user1" => { "name" => "user1" },
|
272
|
+
"user2" => { "name" => "user2" }
|
272
273
|
}
|
273
274
|
JSON(last_response.body).should == expect_response_json
|
274
275
|
end
|
275
276
|
|
276
277
|
end
|
277
|
-
|
278
|
-
|
279
278
|
end
|
280
|
-
|
281
279
|
end
|