@@ -36,9 +36,9 @@ Grape APIs are Rack applications that are created by subclassing `Grape::API`.
3636Below is a simple example showing some of the more common features of Grape in
3737the context of recreating parts of the Twitter API.
3838
39- ``` ruby
39+ ``` ruby
4040class Twitter ::API < Grape ::API
41- version ' v1' , :using => :header , :vendor => ' twitter' , :format => :json
41+ version ' v1' , :using => :header , :vendor => ' twitter'
4242
4343 helpers do
4444 def current_user
8787
8888The above sample creates a Rack application that can be run from a rackup * config.ru* file:
8989
90- ``` ruby
90+ ``` ruby
9191run Twitter ::API
9292```
9393And would respond to the following routes:
@@ -99,22 +99,22 @@ And would respond to the following routes:
9999
100100In a Rails application, modify * config/routes* :
101101
102- ``` ruby
102+ ``` ruby
103103mount Twitter ::API => " /"
104104```
105105
106106## Versioning
107107
108108Versioning is handled with HTTP Accept head by default, but can be configures
109- to [ use different strategies] ( https://github.com/intridea/grape/wiki/API-Versioning ) .
109+ to [ use different strategies] ( https://github.com/intridea/grape/wiki/API-Versioning ) .
110110For example, to request the above with a version, you would make the following
111111request:
112112
113113 curl -H Accept=application/vnd.twitter-v1+json http://localhost:9292/statuses/public_timeline
114114
115115By default, the first matching version is used when no Accept header is
116- supplied. This behavior is similar to routing in Rails. To circumvent this default behavior,
117- one could use the ` :strict ` option. When this option is set to ` true ` , a ` 404 Not found ` error
116+ supplied. This behavior is similar to routing in Rails. To circumvent this default behavior,
117+ one could use the ` :strict ` option. When this option is set to ` true ` , a ` 404 Not found ` error
118118is returned when no correct Accept header is supplied.
119119
120120Serialization takes place automatically.
@@ -124,7 +124,7 @@ Serialization takes place automatically.
124124You can define helper methods that your endpoints can use with the ` helpers `
125125macro by either giving a block or a module:
126126
127- ```` ruby
127+ ``` ruby
128128module MyHelpers
129129 def say_hello (user )
130130 " hey there #{ user.name } "
@@ -147,13 +147,13 @@ class API < Grape::API
147147 say_hello(current_user)
148148 end
149149end
150- ````
150+ ```
151151
152152## Cookies
153153
154154You can set, get and delete your cookies very simply using ` cookies ` method:
155155
156- ```` ruby
156+ ``` ruby
157157class API < Grape ::API
158158 get ' /counter' do
159159 cookies[:counter ] ||= 0
@@ -165,32 +165,32 @@ class API < Grape::API
165165 { :result => cookies.delete(:counter ) }
166166 end
167167end
168- ````
168+ ```
169169
170170To set more than value use hash-based syntax:
171171
172- ```` ruby
172+ ``` ruby
173173cookies[:counter ] = {
174174 :value => 0 ,
175175 :expires => Time .tomorrow,
176176 :domain => ' .example.com' ,
177177 :path => ' /'
178178}
179179cookies[:counter ][:value ] += 1
180- ````
180+ ```
181181
182182## Raising Errors
183183
184184You can raise errors explicitly.
185185
186- ``` ruby
186+ ``` ruby
187187error!(" Access Denied" , 401 )
188188```
189189
190190You can also return JSON formatted objects explicitly by raising error! and
191191passing a hash instead of a message.
192192
193- ``` ruby
193+ ``` ruby
194194error!({ " error" => " unexpected error" , " detail" => " missing widget" }, 500 )
195195```
196196
@@ -199,15 +199,15 @@ error!({ "error" => "unexpected error", "detail" => "missing widget" }, 500)
199199Grape can be told to rescue all exceptions and instead return them in
200200text or json formats.
201201
202- ``` ruby
202+ ``` ruby
203203class Twitter ::API < Grape ::API
204204 rescue_from :all
205205end
206206```
207207
208208You can also rescue specific exceptions.
209209
210- ``` ruby
210+ ``` ruby
211211class Twitter ::API < Grape ::API
212212 rescue_from ArgumentError , NotImplementedError
213213end
216216The error format can be specified using ` error_format ` . Available formats are
217217` :json ` and ` :txt ` (default).
218218
219- ``` ruby
219+ ``` ruby
220220class Twitter ::API < Grape ::API
221221 error_format :json
222222end
225225You can rescue all exceptions with a code block. The ` rack_response ` wrapper
226226automatically sets the default error code and content-type.
227227
228- ``` ruby
228+ ``` ruby
229229class Twitter ::API < Grape ::API
230230 rescue_from :all do |e |
231231 rack_response({ :message => " rescued from #{ e.class .name } " })
@@ -236,43 +236,60 @@ end
236236You can also rescue specific exceptions with a code block and handle the Rack
237237response at the lowest level.
238238
239- ``` ruby
239+ ``` ruby
240240class Twitter ::API < Grape ::API
241241 rescue_from :all do |e |
242242 Rack ::Response .new ([ e.message ], 500 , { " Content-type" => " text/error" }).finish
243243 end
244244end
245245```
246246
247- class Twitter::API < Grape::API
248- rescue_from ArgumentError do |e|
249- Rack::Response.new([ "ArgumentError: #{e.message}" ], 500)
250- end
251- rescue_from NotImplementedError do |e|
252- Rack::Response.new([ "NotImplementedError: #{e.message}" ], 500)
253- end
254- end
247+ ``` ruby
248+ class Twitter ::API < Grape ::API
249+ rescue_from ArgumentError do |e |
250+ Rack ::Response .new ([ " ArgumentError: #{ e.message } " ], 500 )
251+ end
252+ rescue_from NotImplementedError do |e |
253+ Rack ::Response .new ([ " NotImplementedError: #{ e.message } " ], 500 )
254+ end
255+ end
256+ ```
255257
256258## Content-Types
257259
258- By default, Grape supports _ XML_ , _ JSON_ , _ Atom_ , _ RSS_ , and _ text_ content-types.
259- Your API can declare additional types to support. Response format is determined by the
260+ By default, Grape supports _ XML_ , _ JSON_ , _ Atom_ , _ RSS_ , and _ text_ content-types.
261+ Your API can declare additional types to support. Response format is determined by the
260262request's extension or ` Accept ` header.
261263
262- ``` ruby
264+ ``` ruby
263265class Twitter ::API < Grape ::API
264266 content_type :xls , " application/vnd.ms-excel"
265267end
266268```
267269
270+ You can also set the default format. The order for choosing the format is the following.
271+
272+ * Use the file extension, if specified. If the file is .json, choose the JSON format.
273+ * Use the format, if specified by the ` format ` option.
274+ * Attempt to find an acceptable format from the ` Accept ` header.
275+ * Use the default format, if specified by the ` default_format ` option.
276+ * Default to ` :txt ` otherwise.
277+
278+ ``` ruby
279+ class Twitter ::API < Grape ::API
280+ format :json
281+ default_format :json
282+ end
283+ ```
284+
268285## Writing Tests
269286
270287You can test a Grape API with RSpec. Tests make HTTP requests, therefore they
271288must go into the ` spec/request ` group. You may want your API code to go into
272289` app/api ` - you can match that layout under ` spec ` by adding the following in
273290` spec/spec_helper.rb ` .
274291
275- ``` ruby
292+ ``` ruby
276293RSpec .configure do |config |
277294 config.include RSpec ::Rails ::RequestExampleGroup , :type => :request , :example_group => {
278295 :file_path => /spec\/ api/
282299
283300A simple RSpec API test makes a ` get ` request and parses the response.
284301
285- ``` ruby
302+ ``` ruby
286303require ' spec_helper'
287304
288305describe Twitter ::API do
@@ -299,10 +316,10 @@ end
299316## Describing and Inspecting an API
300317
301318Grape lets you add a description to an API along with any other optional
302- elements that can also be inspected at runtime.
319+ elements that can also be inspected at runtime.
303320This can be useful for generating documentation.
304321
305- ``` ruby
322+ ``` ruby
306323class TwitterAPI < Grape ::API
307324
308325 version ' v1'
@@ -327,7 +344,7 @@ contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`,
327344follows the API path may contain any number of keys and its values are also
328345accessible via dynamically-generated ` route_[name] ` functions.
329346
330- ``` ruby
347+ ``` ruby
331348TwitterAPI ::versions # yields [ 'v1', 'v2' ]
332349TwitterAPI ::routes # yields an array of Grape::Route objects
333350TwitterAPI ::routes[0 ].route_version # yields 'v1'
@@ -336,7 +353,7 @@ TwitterAPI::routes[0].route_description # yields [ { "s" => { :desc => "string t
336353
337354Parameters can also be tagged to the method declaration itself.
338355
339- ``` ruby
356+ ``` ruby
340357class StringAPI < Grape ::API
341358 get " split/:string" , { :params => [ " token" ], :optional_params => [ " limit" ] } do
342359 params[:string ].split(params[:token ], (params[:limit ] || 0 ))
@@ -349,7 +366,7 @@ StringAPI::routes[0].route_optional_params # yields an array [ "limit" ]
349366
350367It's possible to retrieve the information about the current route from within an API call with ` route ` .
351368
352- ``` ruby
369+ ``` ruby
353370class MyAPI < Grape ::API
354371 desc " Returns a description of a parameter." , { :params => { " id" => " a required id" } }
355372 get " params/:id" do
@@ -372,7 +389,7 @@ In Grape this option can be used as well when a method is defined.
372389
373390For instance when you're API needs to get part of an URL, for instance:
374391
375- ``` ruby
392+ ``` ruby
376393class UrlAPI < Grape ::API
377394 namespace :urls do
378395 get ' /(*:url)' , :anchor => false do
@@ -403,5 +420,5 @@ MIT License. See LICENSE for details.
403420
404421## Copyright
405422
406- Copyright (c) 2010-2012 Michael Bleigh and Intridea, Inc.
423+ Copyright (c) 2010-2012 Michael Bleigh and Intridea, Inc.
407424
0 commit comments