You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.markdown
+72-15Lines changed: 72 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,11 @@
4
4
5
5
## What is Grape?
6
6
7
-
Grape is a REST-like API micro-framework for Ruby. It is built to complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily provide APIs. It has built-in support for common conventions such as multiple formats, subdomain/prefix restriction, and versioning.
7
+
Grape is a REST-like API micro-framework for Ruby. It is built to complement
8
+
existing web application frameworks such as Rails and Sinatra by providing a
9
+
simple DSL to easily provide APIs. It has built-in support for common
10
+
conventions such as multiple formats, subdomain/prefix restriction, and
11
+
versioning.
8
12
9
13
## Project Tracking
10
14
@@ -19,7 +23,9 @@ Grape is available as a gem, to install it just install the gem:
19
23
20
24
## Basic Usage
21
25
22
-
Grape APIs are Rack applications that are created by subclassing `Grape::API`. Below is a simple example showing some of the more common features of Grape in the context of recreating parts of the Twitter API.
26
+
Grape APIs are Rack applications that are created by subclassing `Grape::API`.
27
+
Below is a simple example showing some of the more common features of Grape in
28
+
the context of recreating parts of the Twitter API.
23
29
24
30
```ruby
25
31
classTwitter::API < Grape::API
@@ -68,7 +74,8 @@ class Twitter::API < Grape::API
68
74
end
69
75
```
70
76
71
-
This would create a Rack application that could be used like so (in a Rackup config.ru file):
77
+
This would create a Rack application that could be used like so (in a Rackup
By default, the first matching version is used when no Accept header is supplied. This behavior is similar to routing in Rails.
93
-
To circumvent this default behaviour, one could use the `:strict` option. When this option is set to `true`, a `404 Not found` error is returned when no correct Accept header is supplied.
99
+
By default, the first matching version is used when no Accept header is
100
+
supplied. This behavior is similar to routing in Rails.
101
+
To circumvent this default behaviour, one could use the `:strict` option. When
102
+
this option is set to `true`, a `404 Not found` error is returned when no
103
+
correct Accept header is supplied.
94
104
95
-
Serialization takes place automatically. For more detailed usage information, please visit the [Grape Wiki](http://github.com/intridea/grape/wiki).
105
+
Serialization takes place automatically. For more detailed usage information,
106
+
please visit the [Grape Wiki](http://github.com/intridea/grape/wiki).
96
107
97
108
## Helpers
98
109
@@ -132,7 +143,8 @@ You can raise errors explicitly.
132
143
error!("Access Denied", 401)
133
144
```
134
145
135
-
You can also return JSON formatted objects explicitly by raising error! and passing a hash instead of a message.
146
+
You can also return JSON formatted objects explicitly by raising error! and
@@ -157,15 +169,17 @@ class Twitter::API < Grape::API
157
169
end
158
170
```
159
171
160
-
The error format can be specified using `error_format`. Available formats are `:json` and `:txt` (default).
172
+
The error format can be specified using `error_format`. Available formats are
173
+
`:json` and `:txt` (default).
161
174
162
175
```ruby
163
176
classTwitter::API < Grape::API
164
177
error_format :json
165
178
end
166
179
```
167
180
168
-
You can rescue all exceptions with a code block. The `rack_response` wrapper automatically sets the default error code and content-type.
181
+
You can rescue all exceptions with a code block. The `rack_response` wrapper
182
+
automatically sets the default error code and content-type.
169
183
170
184
```ruby
171
185
classTwitter::API < Grape::API
@@ -175,7 +189,8 @@ class Twitter::API < Grape::API
175
189
end
176
190
```
177
191
178
-
You can also rescue specific exceptions with a code block and handle the Rack response at the lowest level.
192
+
You can also rescue specific exceptions with a code block and handle the Rack
193
+
response at the lowest level.
179
194
180
195
```ruby
181
196
classTwitter::API < Grape::API
@@ -196,7 +211,10 @@ end
196
211
197
212
## Writing Tests
198
213
199
-
You can test a Grape API with RSpec. Tests make HTTP requests, therefore they must go into the `spec/request` group. You may want your API code to go into `app/api` - you can match that layout under `spec` by adding the following in `spec/spec_helper.rb`.
214
+
You can test a Grape API with RSpec. Tests make HTTP requests, therefore they
215
+
must go into the `spec/request` group. You may want your API code to go into
216
+
`app/api` - you can match that layout under `spec` by adding the following in
217
+
`spec/spec_helper.rb`.
200
218
201
219
```ruby
202
220
RSpec.configure do |config|
@@ -224,7 +242,8 @@ end
224
242
225
243
## Describing and Inspecting an API
226
244
227
-
Grape lets you add a description to an API along with any other optional elements that can also be inspected at runtime.
245
+
Grape lets you add a description to an API along with any other optional
246
+
elements that can also be inspected at runtime.
228
247
This can be useful for generating documentation.
229
248
230
249
```ruby
@@ -246,7 +265,11 @@ class TwitterAPI < Grape::API
246
265
end
247
266
```
248
267
249
-
Grape then exposes arrays of API versions and compiled routes. Each route contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`, `route_path` and `route_params`. The description and the optional hash that follows the API path may contain any number of keys and its values are also accessible via dynamically-generated `route_[name]` functions.
268
+
Grape then exposes arrays of API versions and compiled routes. Each route
269
+
contains a `route_prefix`, `route_version`, `route_namespace`, `route_method`,
270
+
`route_path` and `route_params`. The description and the optional hash that
271
+
follows the API path may contain any number of keys and its values are also
272
+
accessible via dynamically-generated `route_[name]` functions.
StringAPI::routes[0].route_optional_params # yields an array [ "limit" ]
269
292
```
270
293
294
+
## Anchoring
295
+
296
+
Grape by default anchors all request paths, which means that the request URL
297
+
should match from start to end to match, otherwise a `404 Not Found` is
298
+
returned.
299
+
However, this is sometimes not what you want, because it is not always known up
300
+
front what can be expected from the call.
301
+
This is because Rack-mount by default anchors requests to match from the start
302
+
to the end, or not at all. Rails solves this problem by using a `:anchor =>
303
+
false` option in your routes.
304
+
In Grape this option can be used as well when a method is defined.
305
+
306
+
For instance when you're API needs to get part of an URL, for instance:
307
+
308
+
```ruby
309
+
classUrlAPI < Grape::API
310
+
namespace :urlsdo
311
+
get '/(*:url)', :anchor => falsedo
312
+
some_data
313
+
end
314
+
end
315
+
end
316
+
```
317
+
318
+
This will match all paths starting with '/urls/'. There is one caveat though:
319
+
the `params[:url]` parameter only holds the first part of the request url.
320
+
Luckily this can be circumvented by using the described above syntax for path
321
+
specification and using the `PATH_INFO` Rack environment variable, using
322
+
`env["PATH_INFO"]`. This will hold everyting that comes after the '/urls/'
323
+
part.
324
+
271
325
## Note on Patches/Pull Requests
272
326
273
327
* Fork the project.
274
328
* Make your feature addition or bug fix.
275
-
* Add tests for it. This is important so I don't break it in a future version unintentionally.
276
-
* Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
329
+
* Add tests for it. This is important so I don't break it in a future version
330
+
unintentionally.
331
+
* Commit, do not mess with Rakefile, version, or history. (if you want to have
332
+
your own version, that is fine but bump version in a commit by itself I can
333
+
ignore when I pull)
277
334
* Send me a pull request. Bonus points for topic branches.
0 commit comments