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
@@ -8,176 +8,202 @@ Grape is a REST-like API micro-framework for Ruby. It is built to complement exi
8
8
Grape is available as a gem, to install it just install the gem:
9
9
10
10
gem install grape
11
-
11
+
12
12
## Basic Usage
13
13
14
14
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.
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`.
A simple RSpec API test makes a `get` request and parses the response.
131
152
132
-
require 'spec_helper'
153
+
```ruby
154
+
require'spec_helper'
133
155
134
-
describe Twitter::API do
135
-
describe "GET /api/v1/statuses" do
136
-
it "returns an empty array of statuses" do
137
-
get "/api/v1/statuses"
138
-
response.status.should == 200
139
-
JSON.parse(response.body).should == []
140
-
end
141
-
end
156
+
describe Twitter::APIdo
157
+
describe "GET /api/v1/statuses"do
158
+
it "returns an empty array of statuses"do
159
+
get "/api/v1/statuses"
160
+
response.status.should ==200
161
+
JSON.parse(response.body).should == []
142
162
end
163
+
end
164
+
end
165
+
```
143
166
144
167
## Inspecting an API
145
168
146
169
Grape 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`.
147
170
148
-
class TwitterAPI < Grape::API
171
+
```ruby
172
+
classTwitterAPI < Grape::API
149
173
150
-
version 'v1'
151
-
get "version" do
152
-
api.version
153
-
end
154
-
155
-
version 'v2'
156
-
namespace "ns" do
157
-
get "version" do
158
-
api.version
159
-
end
160
-
end
174
+
version 'v1'
175
+
get "version"do
176
+
api.version
177
+
end
161
178
179
+
version 'v2'
180
+
namespace "ns"do
181
+
get "version"do
182
+
api.version
162
183
end
184
+
end
185
+
end
163
186
164
-
TwitterAPI::versions # yields [ 'v1', 'v2' ]
165
-
TwitterAPI::routes # yields an array of Grape::Route objects
166
-
TwitterAPI::routes[0].route_version # yields 'v1'
187
+
TwitterAPI::versions # yields [ 'v1', 'v2' ]
188
+
TwitterAPI::routes # yields an array of Grape::Route objects
189
+
TwitterAPI::routes[0].route_version # yields 'v1'
190
+
```
167
191
168
192
Grape also supports storing additional parameters with the route information. This can be useful for generating documentation. The optional hash that follows the API path may contain any number of keys and its values are also accessible via a dynamically-generated `route_[name]` function.
169
193
170
-
class StringAPI < Grape::API
171
-
get "split/:string", { :params => [ "token" ], :optional_params => [ "limit" ] } do
0 commit comments