|
1 | | -# Grape |
2 | | -[](http://travis-ci.org/intridea/grape) |
| 1 | +# Grape [](http://travis-ci.org/intridea/grape) |
| 2 | + |
| 3 | +**Note:** This is the `master` branch of Grape where we're trying to maintain things to be relatively stable. If you want to live on the edge, check out the [frontier](https://github.com/intridea/grape/tree/frontier). |
3 | 4 |
|
4 | 5 | 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. |
5 | 6 |
|
@@ -116,39 +117,6 @@ class API < Grape::API |
116 | 117 | end |
117 | 118 | ```` |
118 | 119 |
|
119 | | - |
120 | | -## Working with Entities |
121 | | - |
122 | | -A common problem in designing Ruby APIs is that you probably don't want |
123 | | -the exact structure of your data models exposed. ActiveRecord, for |
124 | | -instance, will dump all of its attributes. While you can override |
125 | | -`#as_json` to alter this behavior somewhat, what is really needed is an |
126 | | -intermediary layer between the model and the API. This is where the |
127 | | -`Grape::Entity` class comes in. |
128 | | - |
129 | | -```ruby |
130 | | -module Entities |
131 | | - class User < Grape::Entity |
132 | | - expose :first_name, :last_name |
133 | | - expose :email, :if => {:authenticated => true} |
134 | | - expose :name, :id => {:version => 'v1'} # deprecated |
135 | | - end |
136 | | -end |
137 | | - |
138 | | -class API < Grape::API |
139 | | - version 'v1', 'v2' |
140 | | - |
141 | | - get '/users/:id' do |
142 | | - present User.find(params[:id]), |
143 | | - :with => Entities::User, |
144 | | - :authenticated => env.key?('api.token') |
145 | | - end |
146 | | -end |
147 | | -``` |
148 | | - |
149 | | -For more information about Entities, view the project's YARD |
150 | | -documentation. |
151 | | - |
152 | 120 | ## Raising Errors |
153 | 121 |
|
154 | 122 | You can raise errors explicitly. |
|
0 commit comments