File tree 4 files changed +47
-0
lines changed 4 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ A [Grape](http://github.com/ruby-grape/grape) API mounted on Rack.
10
10
* [ ping] ( api/ping.rb ) : a hello world example that returns a JSON document
11
11
* [ post_put] ( api/post_put.rb ) : a simple ` POST ` and ` PUT ` example
12
12
* [ post_json] ( api/post_json.rb ) : an example that shows a ` POST ` of JSON data
13
+ * [ get_json] ( api/get_json.rb ) : an example that pre-processes params sent as JSON data
13
14
* [ rescue_from] ( api/rescue_from.rb ) : an example of ` rescue_from ` that wraps all exceptions in an HTTP error code 500
14
15
* [ path_versioning] ( api/path_versioning.rb ) : an example that uses path-based versioning
15
16
* [ header_versioning] ( api/header_versioning.rb ) : an example that uses vendor header-based versioning
Original file line number Diff line number Diff line change
1
+ module Acme
2
+ class GetJson < Grape ::API
3
+ format :json
4
+ desc 'Flips reticulated in a collection of splines passed as JSON in a query string.'
5
+ resource :reticulated_splines do
6
+ before do
7
+ params . each_pair do |k , v |
8
+ params [ k ] = JSON . parse ( v ) rescue v
9
+ end
10
+ end
11
+ params do
12
+ requires :splines , type : Array do
13
+ requires :id , type : Integer
14
+ requires :reticulated , type : Boolean
15
+ end
16
+ end
17
+ get do
18
+ params [ :splines ] . map do |spline |
19
+ spline . merge ( reticulated : !spline [ :reticulated ] )
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ class API < Grape::API
9
9
mount ::Acme ::PostPut
10
10
mount ::Acme ::WrapResponse
11
11
mount ::Acme ::PostJson
12
+ mount ::Acme ::GetJson
12
13
mount ::Acme ::ContentType
13
14
mount ::Acme ::UploadFile
14
15
mount ::Acme ::Entities ::API
Original file line number Diff line number Diff line change
1
+ require 'spec_helper'
2
+
3
+ describe Acme ::API do
4
+ include Rack ::Test ::Methods
5
+
6
+ def app
7
+ Acme ::API
8
+ end
9
+
10
+ it 'flips reticulated in a collection of splines' do
11
+ get '/api/reticulated_splines' , 'splines' => [
12
+ { id : 1 , reticulated : false } ,
13
+ { id : 2 , reticulated : true }
14
+ ] . to_json
15
+ expect ( last_response . status ) . to eq ( 200 )
16
+ expect ( last_response . body ) . to eq ( [
17
+ { id : 1 , reticulated : true } ,
18
+ { id : 2 , reticulated : false }
19
+ ] . to_json )
20
+ end
21
+ end
You can’t perform that action at this time.
0 commit comments