Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 281 lines (196 sloc) 15.21 kB
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
1 # RocketPants!
8f55fce Add a skeleton readme
Darcy Laycock authored
2
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
3 First thing's first, you're probably asking yourself - "Why the ridiculous name?". It's simple, really - RocketPants is memorable, and sounds completely bad ass. - everything a library needs.
98d2351 @Sutto README work and License tweaks
authored
4
5e788d8 @levibuzolic Fixed typo in README
levibuzolic authored
5 At its core, RocketPants is a set of tools (built around existing toolsets such as ActionPack) to make it easier to build well-designed APIs in Ruby and more importantly, along side Rails. You can think of it like [Grape](https://github.com/intridea/grape), a fantastic library which RocketPants was originally inspired by but with deeper Rails and ActionPack integration.
98d2351 @Sutto README work and License tweaks
authored
6
7 ## Key Features
8
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
9 Why use RocketPants over alternatives like Grape or normal Rails? The reasons we built it come down to a couple of simple things:
98d2351 @Sutto README work and License tweaks
authored
10
11 1. **It's opinionated** (like Grape) - In this case, we dictate a certain JSON structure we've found nice to work with (after having worked with and investigated a large number of other apis), it makes it simple to add metadata along side requests and the like.
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
12 2. **Simple and Often Automatic Response Metadata** - RocketPants automatically takes care of sending metadata about paginated responses and arrays where possible. This means as a user, you only need to worry about writing `expose object_or_presenter` in your controller and RocketPants will do it's best to send as much information back to the user.
13 3. **Extended Error Support** - RocketPants has a built in framework to manage errors it knows how to handle (in the forms of mapping exceptions to a well defined JSON structure) as well as tools to make it simple to hook up to Airbrake and do things such as including an error identifier in the response.
14 4. **It's built on ActionPack** - One of the key differentiators to Grape is that RocketPants embraces ActionPack and uses the modular components included from Rails 3.0 onwards to provide things you're familiar with already such as filters.
15 5. **Semi-efficient Caching Support** - Thanks to a combination of Rails middleware and collection vs. resource distinctions, RocketPants makes it relatively easy to implement "Efficient Validation" (See [here](http://rtomayko.github.com/rack-cache/faq)). As a developer, this means you get even more benefits of http caching where possible, without the need to generate full requests when etags are present.
98d2351 @Sutto README work and License tweaks
authored
16
74b4567 @Sutto Add link to example app (not yet there)
authored
17 ## Example App
18
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
19 Learn better by reading code? There is also have an example app mixing models and api clients over at [Sutto/transperth-api](https://github.com/Sutto/transperth-api) that is built using RocketPants.
74b4567 @Sutto Add link to example app (not yet there)
authored
20
98d2351 @Sutto README work and License tweaks
authored
21 ## General Structure
22
23 RocketPants builds upon the mixin-based approach to ActionController-based rails applications that Rails 3 made possible. Instead of including everything like Rails does in `ActionController::Base`, RocketPants only includes the bare minimum to make apis. In the near future, it may be modified to work with `ActionController::Base` for the purposes of better compatibility with other gems.
24
25 Out of the box, we use the following ActionController components:
26
27 * `ActionController::HideActions` - Lets you hide methods from actions.
28 * `ActionController::UrlFor` - `url_for` helpers / tweaks by Rails to make integration with routes work better.
29 * `ActionController::Redirecting` - Allows you to use `redirect_to`.
30 * `ActionController::ConditionalGet` - Adds support for Rails caching controls, e.g. `fresh_when` and `expires_in`.
31 * `ActionController::RackDelegation` - Lets you reset the session and set the response body.
32 * `ActionController::RecordIdentifier` - Gives `dom_class` and `dom_id` methods, used for polymorphic routing.
33 * `ActionController::MimeResponds` - Gives `respond_to` with mime type controls.
34 * `AbstractController::Callbacks` - Adds support for callbacks / filters.
35 * `ActionController::Rescue` - Lets you use `rescue_from`.
36
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
37 And added our own:
98d2351 @Sutto README work and License tweaks
authored
38
39 * `RocketPants::UrlFor` - Automatically includes the current version when generating URLs from the controller.
40 * `RocketPants::Respondable` - The core of RocketPants, the code that handles converting objects to the different container types.
41 * `RocketPants::Versioning` - Allows versioning requirements on the controller to ensure it is only callable with a specific api version.
42 * `RocketPants::Instrumentation` - Adds Instrumentation notifications making it easy to use and hook into with Rails.
43 * `RocketPants::Caching` - Implements time-based caching for index actions and etag-based efficient validation for singular resources.
44 * `RocketPants::ErrorHandling` - Short hand to create errors as well as simplifications to catch and render a standardised error representation.
45 * `RocketPants::Rescuable` - Allows you to hook in to rescuing exceptions and to make it easy to post notifications to tools such as AirBrake.
46
47 To use RocketPants, instead of inheriting from `ActionController::Base`, just inherit from `RocketPants::Base`.
48
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
49 Likewise, in Rails applications RocketPants also adds `RocketPants::CacheMiddleware` before the controller endpoints to implement ["Efficient Validation"](http://rtomayko.github.com/rack-cache/faq).
98d2351 @Sutto README work and License tweaks
authored
50
149db46 @Sutto More README docs / tweaks
authored
51 ## Installing RocketPants
52
53 Installing RocketPants is a simple matter of adding:
54
55 gem 'rocket_pants', '~> 1.0'
56
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
57 To your `Gemfile` and running `bundle install`. Next, instead of inherited from `ActionController::Base`, simply inherit from `RocketPants::Base` instead. If you're working with an API-only application, I typically change this in `ApplicationController` and inherit from `ApplicationController` as usual. Otherwise, I generate a new `ApiController` base controller along side `ApplicationController` which instead inherits from `RocketPants::Base` and place all my logic there.
149db46 @Sutto More README docs / tweaks
authored
58
59 ## Configuration
60
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
61 Setting up RocketPants in your rails application is pretty simple and requires a minimal amount of effort. Inside your environment configuration, RocketPants offers the following options to control how it's configured (and their expanded alternatives):
149db46 @Sutto More README docs / tweaks
authored
62
63 - `config.rocket_pants.use_caching` - Defaulting to true for production environments and false elsewhere, defines whether RocketPants caching setup as described below is used.
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
64 - `config.rocket_pants.cache` - A `Moneta::Store` instance used as the RocketPants cache, defaulting to a moneta memory instance. Change for proper caching. (See [here](https://github.com/wycats/moneta) for more information on Moneta.)
149db46 @Sutto More README docs / tweaks
authored
65
66 ## Version Controllers / Routes
67
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
68 The current preferred way of dealing with version APIs in RocketPants is to do it using routes in the form of `/:version/:endpoint` - e.g. `GET /1/users/324`. RocketPants has support in the router and controller level for enforcing and controlling this. In the controller, it's a matter of specifying the required API versions:
149db46 @Sutto More README docs / tweaks
authored
69
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
70 ```ruby
71 class UsersController < RocketPants::Base
72 version 1 # A single version
73 # or...
74 version 2..3 # 2-3 support this controller
75 end
76 ```
149db46 @Sutto More README docs / tweaks
authored
77
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
78 And in the case of multiple versions, I strongly encourage namespaces the controllers inside modules. If the version param (as specified) by the URL does not match, then the specified controller will return an `:invalid_version` error as shown below.
149db46 @Sutto More README docs / tweaks
authored
79
80 Next, in your `config/routes.rb` file, you can also declare versions using the following syntax and it will automatically set up the routes for you:
81
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
82 ```ruby
83 api :version => 1 do
84 get 'x', :to => 'test#item'
85 end
86 ```
149db46 @Sutto More README docs / tweaks
authored
87
88 Which will route `GET /1/x` to `TestController#item`.
89
90 Likewise, you can specify a route for multiple versions by:
91
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
92 ```ruby
93 api :versions => 1..3 do
94 get 'x', :to => 'test#item'
95 end
96 ```
149db46 @Sutto More README docs / tweaks
authored
97
98d2351 @Sutto README work and License tweaks
authored
98 ## Working with data
99
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
100 When using RocketPants, you write your controllers the same as how you would with normal ActionController, the only thing that changes is how you handle data. `head` and `redirect_to` still work exactly the same as in Rails, but instead of using `respond_with` and `render` you instead use RocketPant's `exposes` methods (and it's kind). Namely:
3f8d609 @Sutto More type conversion docs
authored
101
102 - `expose` / `exposes` - The core of all type conversion, will check the type of data and automatically convert it to the correct time (for either a singular, collection or paginated resource).
103 - `paginated` - Render an object as a paginated collection of data.
104 - `collection` - Renders a collection of objects - e.g. an array of users.
105 - `resource` - Renders a single object.
106
107 Along side the above that wrap data, it also provides:
108
109 - `responds` - Renders JSON, normalizing the object first (unwrapped).
110 - `render_json` - Renders an object as JSON.
111
112 ### Singular Resources
113
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
114 Singular resources will be converted to JSON via `serializable_hash`, passing through any objects
3f8d609 @Sutto More type conversion docs
authored
115 and then wrapped in an object as the `response` key:
116
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
117 ```json
118 {
119 "response": {
120 "your": "serialized-object"
121 }
122 }
123 ```
3f8d609 @Sutto More type conversion docs
authored
124
125 ### Collections
126
127 Similar to singular resources, but also include extra data about the count of items.
128
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
129 ```json
130 {
131 "response": [{
132 "name": "object-one"
133 }, {
134 "name": "object-two"
135 }],
136 "count": 2
137 }
138 ```
3f8d609 @Sutto More type conversion docs
authored
139
140 ### Paginated Collections
141
142 The final type, similar to paginated objects but it includes details about the paginated data:
143
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
144 ```json
145 {
146 "response": [
147 {"name": "object-one"},
148 {"name": "object-two"},
149 {"name": "object-three"},
150 {"name": "object-four"},
151 {"name": "object-five"}
152 ],
153 "count": 5,
154 "pagination": {
155 "previous": 1,
156 "next": 3,
157 "current": 2,
158 "per_page": 5,
159 "count": 23
160 "pages": 5
161 }
162 }
163 ```
98d2351 @Sutto README work and License tweaks
authored
164
165 ## Registering / Dealing with Errors
166
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
167 One of the built in features of RocketPants is the ability to handle rescuing / controlling exceptions and more importantly to handle mapping exceptions to names, messages and error codes.
4da07ec @Sutto Started writing docs on the error handling
authored
168
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
169 This comes in useful when you wish to automatically convert exceptions such as `ActiveRecord::RecordNotFound` to a structured bit of data in the response. Namely, it makes it trivial to generate objects that follow the JSON structure of:
4da07ec @Sutto Started writing docs on the error handling
authored
170
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
171 ```json
172 {
173 "error": "standard_error_name",
174 "error_description": "A translated error message describing what happened."
175 }
176 ```
4da07ec @Sutto Started writing docs on the error handling
authored
177
178 It also adds a facilities to make it easy to add extra information to the response.
179
180 RocketPants will also attempt to convert all errors in the controller, defaulting to the `"system"` exception name and message as the error description. We also provide a registry to allow throwing exception from their symbolic name like so:
181
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
182 ```ruby
183 error! :not_found
184 ```
4da07ec @Sutto Started writing docs on the error handling
authored
185
186 In the controller.
187
188 Out of the box, the following exceptions come pre-registered and setup:
189
190 - `:throttled` - The user has hit an api throttled error.
191 - `:unauthenticated` - The user doesn't have valid authentication details.
192 - `:invalid_version` - An invalid API version was specified.
193 - `:not_implemented` - The specified endpoint is not yet implemented.
194 - `:not_found` - The given resource could not be found.
195
98d2351 @Sutto README work and License tweaks
authored
196 ## Implementing Efficient Validation
197
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
198 One of the core design principles built into RocketPants is simple support for "Efficient Validation" as described in the [Rack::Cache FAQ](http://rtomayko.github.com/rack-cache/faq) - Namely, it adds simple support for object-level caching using etags with fast verification thanks to the `RocketPants::CacheMiddleware` cache middleware.
149db46 @Sutto More README docs / tweaks
authored
199
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
200 To do this, it uses `RocketPants.cache`, by default any Moneta-based store, to keep a mapping of object -> current cache key. RocketPants will then generate the etag when caching is enabled in the controller for singular-responses, generating an etag that can be quickly validated.
149db46 @Sutto More README docs / tweaks
authored
201
202 For example, you'd add the following to your model:
203
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
204 ```ruby
205 class User < ActiveRecord::Base
206 include RocketPants::Cacheable
207 end
208 ```
149db46 @Sutto More README docs / tweaks
authored
209
210 And then in your controller, you'd have something like:
211
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
212 ```ruby
213 class UsersController < RocketPants::Base
149db46 @Sutto More README docs / tweaks
authored
214
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
215 version 1
149db46 @Sutto More README docs / tweaks
authored
216
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
217 # Time based, e.g. collections, will be cached for 5 minutes - whilst singular
218 # items e.g. show will use etag-based caching:
219 caches :show, :index, :caches_for => 5.minutes
149db46 @Sutto More README docs / tweaks
authored
220
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
221 def index
222 expose User.all
223 end
149db46 @Sutto More README docs / tweaks
authored
224
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
225 def show
226 expose User.find(params[:id])
227 end
149db46 @Sutto More README docs / tweaks
authored
228
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
229 end
230 ```
149db46 @Sutto More README docs / tweaks
authored
231
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
232 When the user hits the index endpoint, it will generate an expiry-based caching header that caches the result for up to 5 minutes. When the user instead hits the show endpoint, it will generate a special etag that contains and object identifier portion and an object cache key. Inside `RocketPants.cache`, we store the mapping and then inside `RocketPants::CacheMiddleware`, we simply check if the given cache key matches the specified object identifier. If it does, we return a not modified response otherwise we pass it through to controller - giving the advantage of efficient caching without having to hit the full database on every request.
98d2351 @Sutto README work and License tweaks
authored
233
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
234 ## Using with RSpec
73b2f4e Add misc. test helpers
Darcy Laycock authored
235
98d2351 @Sutto README work and License tweaks
authored
236 RocketPants includes a set of helpers to make testing controllers built on `RocketPants::Base` simpler.
237
19b8bc5 @Sutto RSpec integration docs
authored
238 * `be_singular_resource` - Checks the response is a single resource - e.g. `response.should be_siingular_resource`.
239 * `be_collection_resource` - Checks the response is collection of resources - e.g. `response.should be_collection_resource`.
240 * `be_paginated_response` - Checks the response is paginated - e.g. `response.should be_paginated_response`.
241 * `be_api_error(type = any)` - Checks it returned an error for the specified exception (or check the response is an error without any argument) - e.g. `response.should be_api_error RocketPants::NotFound`.
242 * `have_exposed(data, options = {})` - Given an object and conversion options, lets you check the output exposed the same object. e.g: `response.should have_exposed user`
243
244 Likewise, it adds the following helper methods:
245
246 - `parsed_body` - A parsed-JSON representation of the response.
247 - `decoded_body` - A `Hashie::Mash` of the response body.
248
249 To set up the integration, in your `spec/spec_helper.rb` add:
250
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
251 ```ruby
252 config.include RocketPants::TestHelper, :type => :controller
253 config.include RocketPants::RSpecMatchers, :type => :controller
254 ```
19b8bc5 @Sutto RSpec integration docs
authored
255
256 Inside the `RSpec.configure do |config|` block.
98d2351 @Sutto README work and License tweaks
authored
257
8ced3cb @Sutto Update README.md
authored
258 ## Contributors
259
260 - [Darcy Laycock](https://github.com/Sutto) - Main developer, current maintainer.
261 - [Steve Webb](https://github.com/swebb) - Helped with original work at [The Frontier Group](https://github.com/thefrontiergroup), inc. original design.
262 - [Fred Wu](https://github.com/fredwu) - README fixes.
263 - [Levi Buzolic](https://github.com/levibuzolic) - README fixes.
264
98d2351 @Sutto README work and License tweaks
authored
265 ## Contributing
266
267 We encourage all community contributions. Keeping this in mind, please follow these general guidelines when contributing:
268
269 * Fork the project
270 * Create a topic branch for what you’re working on (git checkout -b awesome_feature)
271 * Commit away, push that up (git push your\_remote awesome\_feature)
272 * Create a new GitHub Issue with the commit, asking for review. Alternatively, send a pull request with details of what you added.
273 * Once it’s accepted, if you want access to the core repository feel free to ask! Otherwise, you can continue to hack away in your own fork.
274
275 Other than that, our guidelines very closely match the GemCutter guidelines [here](http://wiki.github.com/qrush/gemcutter/contribution-guidelines).
276
277 (Thanks to [GemCutter](http://wiki.github.com/qrush/gemcutter/) for the contribution guide)
73b2f4e Add misc. test helpers
Darcy Laycock authored
278
98d2351 @Sutto README work and License tweaks
authored
279 ## License
73b2f4e Add misc. test helpers
Darcy Laycock authored
280
45ed4af @fredwu Fixed some small typos and enhanced syntax highlighting in the README
fredwu authored
281 RocketPants is released under the MIT License (see the [license file](https://github.com/filtersquad/rocket_pants/blob/master/LICENSE)) and is copyright Filter Squad, 2012.
Something went wrong with that request. Please try again.