@@ -36,7 +36,7 @@ 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
4141 version ' v1' , :using => :header , :vendor => ' twitter'
4242
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,7 +99,7 @@ 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
@@ -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,30 +236,32 @@ 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
258260By default, Grape supports _ XML_ , _ JSON_ , _ Atom_ , _ RSS_ , and _ text_ content-types.
259261Your 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
@@ -273,10 +275,10 @@ You can also set the default format. The order for choosing the format is the fo
273275* Use the default format, if specified by the ` default_format ` option.
274276* Default to ` :txt ` otherwise.
275277
276- ``` ruby
278+ ``` ruby
277279class Twitter ::API < Grape ::API
278280 format :json
279- defalt_format :json
281+ default_format :json
280282end
281283```
282284
@@ -287,7 +289,7 @@ must go into the `spec/request` group. You may want your API code to go into
287289` app/api ` - you can match that layout under ` spec ` by adding the following in
288290` spec/spec_helper.rb ` .
289291
290- ``` ruby
292+ ``` ruby
291293RSpec .configure do |config |
292294 config.include RSpec ::Rails ::RequestExampleGroup , :type => :request , :example_group => {
293295 :file_path => /spec\/ api/
297299
298300A simple RSpec API test makes a ` get ` request and parses the response.
299301
300- ``` ruby
302+ ``` ruby
301303require ' spec_helper'
302304
303305describe Twitter ::API do
@@ -317,7 +319,7 @@ Grape lets you add a description to an API along with any other optional
317319elements that can also be inspected at runtime.
318320This can be useful for generating documentation.
319321
320- ``` ruby
322+ ``` ruby
321323class TwitterAPI < Grape ::API
322324
323325 version ' v1'
@@ -342,7 +344,7 @@ contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`,
342344follows the API path may contain any number of keys and its values are also
343345accessible via dynamically-generated ` route_[name] ` functions.
344346
345- ``` ruby
347+ ``` ruby
346348TwitterAPI ::versions # yields [ 'v1', 'v2' ]
347349TwitterAPI ::routes # yields an array of Grape::Route objects
348350TwitterAPI ::routes[0 ].route_version # yields 'v1'
@@ -351,7 +353,7 @@ TwitterAPI::routes[0].route_description # yields [ { "s" => { :desc => "string t
351353
352354Parameters can also be tagged to the method declaration itself.
353355
354- ``` ruby
356+ ``` ruby
355357class StringAPI < Grape ::API
356358 get " split/:string" , { :params => [ " token" ], :optional_params => [ " limit" ] } do
357359 params[:string ].split(params[:token ], (params[:limit ] || 0 ))
@@ -364,7 +366,7 @@ StringAPI::routes[0].route_optional_params # yields an array [ "limit" ]
364366
365367It's possible to retrieve the information about the current route from within an API call with ` route ` .
366368
367- ``` ruby
369+ ``` ruby
368370class MyAPI < Grape ::API
369371 desc " Returns a description of a parameter." , { :params => { " id" => " a required id" } }
370372 get " params/:id" do
@@ -387,7 +389,7 @@ In Grape this option can be used as well when a method is defined.
387389
388390For instance when you're API needs to get part of an URL, for instance:
389391
390- ``` ruby
392+ ``` ruby
391393class UrlAPI < Grape ::API
392394 namespace :urls do
393395 get ' /(*:url)' , :anchor => false do
0 commit comments