acts_as_api 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/Gemfile +12 -0
- data/History.txt +8 -0
- data/README.rdoc +29 -225
- data/Rakefile +20 -28
- data/acts_as_api.gemspec +31 -0
- data/examples/introduction/docco.css +186 -0
- data/examples/introduction/index.html +340 -0
- data/examples/introduction/index.rb +132 -0
- data/examples/introduction/layout.mustache +64 -0
- data/lib/acts_as_api.rb +11 -25
- data/lib/acts_as_api/api_template.rb +14 -0
- data/lib/acts_as_api/base.rb +61 -56
- data/lib/acts_as_api/config.rb +34 -0
- data/lib/acts_as_api/rails_renderer.rb +15 -0
- data/lib/acts_as_api/rendering.rb +11 -8
- data/lib/acts_as_api/version.rb +4 -0
- data/spec/controllers/respond_with_users_controller_spec.rb +5 -0
- data/spec/controllers/users_controller_spec.rb +161 -0
- data/spec/models/base_spec.rb +437 -0
- data/spec/rails_app/.gitignore +4 -0
- data/spec/rails_app/Rakefile +7 -0
- data/spec/rails_app/app/controllers/application_controller.rb +3 -0
- data/spec/rails_app/app/controllers/respond_with_users_controller.rb +15 -0
- data/spec/rails_app/app/controllers/users_controller.rb +21 -0
- data/spec/rails_app/app/helpers/application_helper.rb +2 -0
- data/spec/rails_app/app/models/task.rb +3 -0
- data/spec/rails_app/app/models/untouched.rb +2 -0
- data/spec/rails_app/app/models/user.rb +69 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +14 -0
- data/spec/rails_app/config.ru +4 -0
- data/spec/rails_app/config/application.rb +42 -0
- data/spec/rails_app/config/boot.rb +6 -0
- data/spec/rails_app/config/database.yml +23 -0
- data/spec/rails_app/config/environment.rb +5 -0
- data/spec/rails_app/config/environments/development.rb +26 -0
- data/spec/rails_app/config/environments/production.rb +49 -0
- data/spec/rails_app/config/environments/test.rb +35 -0
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails_app/config/initializers/inflections.rb +10 -0
- data/spec/rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails_app/config/initializers/secret_token.rb +7 -0
- data/spec/rails_app/config/initializers/session_store.rb +8 -0
- data/spec/rails_app/config/locales/en.yml +5 -0
- data/spec/rails_app/config/routes.rb +7 -0
- data/spec/rails_app/db/migrate/20110214201640_create_tables.rb +35 -0
- data/spec/rails_app/db/schema.rb +34 -0
- data/spec/rails_app/db/seeds.rb +7 -0
- data/spec/rails_app/lib/tasks/.gitkeep +0 -0
- data/spec/rails_app/public/404.html +26 -0
- data/spec/rails_app/public/422.html +26 -0
- data/spec/rails_app/public/500.html +26 -0
- data/spec/rails_app/public/favicon.ico +0 -0
- data/spec/rails_app/public/images/rails.png +0 -0
- data/spec/rails_app/public/index.html +239 -0
- data/spec/rails_app/public/javascripts/application.js +2 -0
- data/spec/rails_app/public/javascripts/controls.js +965 -0
- data/spec/rails_app/public/javascripts/dragdrop.js +974 -0
- data/spec/rails_app/public/javascripts/effects.js +1123 -0
- data/spec/rails_app/public/javascripts/prototype.js +6001 -0
- data/spec/rails_app/public/javascripts/rails.js +191 -0
- data/spec/rails_app/public/robots.txt +5 -0
- data/spec/rails_app/public/stylesheets/.gitkeep +0 -0
- data/spec/rails_app/script/rails +6 -0
- data/spec/spec_helper.rb +12 -13
- data/spec/support/api_test_helpers.rb +23 -0
- metadata +137 -35
- data/Manifest.txt +0 -15
- data/script/console +0 -10
- data/script/destroy +0 -14
- data/script/generate +0 -14
- data/spec/acts_as_api_spec.rb +0 -87
- data/tasks/rspec.rake +0 -21
@@ -0,0 +1,340 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html;charset=utf-8">
|
5
|
+
<title>acts_as_api</title>
|
6
|
+
<link rel="stylesheet" href="./docco.css">
|
7
|
+
<link href="http://fonts.googleapis.com/css?family=Copse:regular" rel="stylesheet" type="text/css" >
|
8
|
+
<style>
|
9
|
+
h1, h2, h3, h4, h5 {
|
10
|
+
font-family: 'Copse', serif;
|
11
|
+
font-style: normal;
|
12
|
+
font-weight: 700;
|
13
|
+
text-shadow: none;
|
14
|
+
text-decoration: none;
|
15
|
+
text-transform: none;
|
16
|
+
letter-spacing: 0em;
|
17
|
+
word-spacing: 0em;
|
18
|
+
line-height: 1.2;
|
19
|
+
}
|
20
|
+
</style>
|
21
|
+
</head>
|
22
|
+
<body>
|
23
|
+
<div id='container'>
|
24
|
+
<div id="background"></div>
|
25
|
+
<table cellspacing=0 cellpadding=0>
|
26
|
+
<thead>
|
27
|
+
<tr>
|
28
|
+
<th class=docs>
|
29
|
+
<h1>acts_as_api</h1>
|
30
|
+
<p>Makes creating XML/JSON responses in Rails 3 easy and fun.</p>
|
31
|
+
</th>
|
32
|
+
<th class=code></th>
|
33
|
+
</tr>
|
34
|
+
</thead>
|
35
|
+
<tbody>
|
36
|
+
<tr id='section-1'>
|
37
|
+
<td class=docs>
|
38
|
+
<div class="pilwrap">
|
39
|
+
<a class="pilcrow" href="#section-1">¶</a>
|
40
|
+
</div>
|
41
|
+
<p>The built-in XML/JSON support of Rails is great but:
|
42
|
+
You surely don’t want to expose your models always with all attributes.</p>
|
43
|
+
|
44
|
+
<p>acts_as_api enriches the models and controllers of your app in a rails-like way so you can easily determine how your API responses should look like.</p>
|
45
|
+
</td>
|
46
|
+
<td class=code>
|
47
|
+
<div class='highlight'><pre></pre></div>
|
48
|
+
</td>
|
49
|
+
</tr>
|
50
|
+
<tr id='section-Features'>
|
51
|
+
<td class=docs>
|
52
|
+
<div class="pilwrap">
|
53
|
+
<a class="pilcrow" href="#section-Features">¶</a>
|
54
|
+
</div>
|
55
|
+
<h2>Features</h2>
|
56
|
+
|
57
|
+
<ul>
|
58
|
+
<li>DRY templates for your api responses</li>
|
59
|
+
<li>Easy but very flexible syntax for defining the templates</li>
|
60
|
+
<li>XML, JSON and JSON-P support out of the box, easy to extend</li>
|
61
|
+
<li>Minimal dependecies (you can also use it without Rails)</li>
|
62
|
+
<li>Does not rely on ActiveRecord (can be used with other ORMs like Mongoid)</li>
|
63
|
+
<li>Supports multiple api rendering templates for a models. This is especially useful for API versioning or for example for private vs. public access points to a user’s profile.</li>
|
64
|
+
</ul>
|
65
|
+
</td>
|
66
|
+
<td class=code>
|
67
|
+
<div class='highlight'><pre></pre></div>
|
68
|
+
</td>
|
69
|
+
</tr>
|
70
|
+
<tr id='section-3'>
|
71
|
+
<td class=docs>
|
72
|
+
<div class="pilwrap">
|
73
|
+
<a class="pilcrow" href="#section-3">¶</a>
|
74
|
+
</div>
|
75
|
+
<hr />
|
76
|
+
</td>
|
77
|
+
<td class=code>
|
78
|
+
<div class='highlight'><pre></pre></div>
|
79
|
+
</td>
|
80
|
+
</tr>
|
81
|
+
<tr id='section-Rails_3.0.x_Quickstart'>
|
82
|
+
<td class=docs>
|
83
|
+
<div class="pilwrap">
|
84
|
+
<a class="pilcrow" href="#section-Rails_3.0.x_Quickstart">¶</a>
|
85
|
+
</div>
|
86
|
+
<h2>Rails 3.0.x Quickstart</h2>
|
87
|
+
</td>
|
88
|
+
<td class=code>
|
89
|
+
<div class='highlight'><pre></pre></div>
|
90
|
+
</td>
|
91
|
+
</tr>
|
92
|
+
<tr id='section-5'>
|
93
|
+
<td class=docs>
|
94
|
+
<div class="pilwrap">
|
95
|
+
<a class="pilcrow" href="#section-5">¶</a>
|
96
|
+
</div>
|
97
|
+
<p>Add to gemfile</p>
|
98
|
+
</td>
|
99
|
+
<td class=code>
|
100
|
+
<div class='highlight'><pre><span class="n">gem</span> <span class="s1">'acts_as_api'</span></pre></div>
|
101
|
+
</td>
|
102
|
+
</tr>
|
103
|
+
<tr id='section-6'>
|
104
|
+
<td class=docs>
|
105
|
+
<div class="pilwrap">
|
106
|
+
<a class="pilcrow" href="#section-6">¶</a>
|
107
|
+
</div>
|
108
|
+
<p>Update your bundle</p>
|
109
|
+
</td>
|
110
|
+
<td class=code>
|
111
|
+
<div class='highlight'><pre><span class="n">bundle</span> <span class="n">install</span></pre></div>
|
112
|
+
</td>
|
113
|
+
</tr>
|
114
|
+
<tr id='section-Setting_up_your_Model'>
|
115
|
+
<td class=docs>
|
116
|
+
<div class="pilwrap">
|
117
|
+
<a class="pilcrow" href="#section-Setting_up_your_Model">¶</a>
|
118
|
+
</div>
|
119
|
+
<h3>Setting up your Model</h3>
|
120
|
+
</td>
|
121
|
+
<td class=code>
|
122
|
+
<div class='highlight'><pre></pre></div>
|
123
|
+
</td>
|
124
|
+
</tr>
|
125
|
+
<tr id='section-8'>
|
126
|
+
<td class=docs>
|
127
|
+
<div class="pilwrap">
|
128
|
+
<a class="pilcrow" href="#section-8">¶</a>
|
129
|
+
</div>
|
130
|
+
<p>Given you have a model <code>User</code>.
|
131
|
+
If you only want to expose the <code>first_name</code> and <code>last_name</code> attribute of a user via your api, you would do something like this:</p>
|
132
|
+
</td>
|
133
|
+
<td class=code>
|
134
|
+
<div class='highlight'><pre></pre></div>
|
135
|
+
</td>
|
136
|
+
</tr>
|
137
|
+
<tr id='section-9'>
|
138
|
+
<td class=docs>
|
139
|
+
<div class="pilwrap">
|
140
|
+
<a class="pilcrow" href="#section-9">¶</a>
|
141
|
+
</div>
|
142
|
+
<p>Within your model:</p>
|
143
|
+
|
144
|
+
<p>First you activate acts_as_api for your model by calling <code>acts_as_api</code>.</p>
|
145
|
+
|
146
|
+
<p>Then you define an api template to render the model with <code>api_accessible</code>.</p>
|
147
|
+
</td>
|
148
|
+
<td class=code>
|
149
|
+
<div class='highlight'><pre><span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
|
150
|
+
|
151
|
+
<span class="n">acts_as_api</span>
|
152
|
+
|
153
|
+
<span class="n">api_accessible</span> <span class="ss">:name_only</span> <span class="k">do</span> <span class="o">|</span><span class="n">template</span><span class="o">|</span>
|
154
|
+
<span class="n">template</span><span class="o">.</span><span class="n">add</span> <span class="ss">:first_name</span>
|
155
|
+
<span class="n">template</span><span class="o">.</span><span class="n">add</span> <span class="ss">:last_name</span>
|
156
|
+
<span class="k">end</span>
|
157
|
+
|
158
|
+
<span class="k">end</span></pre></div>
|
159
|
+
</td>
|
160
|
+
</tr>
|
161
|
+
<tr id='section-10'>
|
162
|
+
<td class=docs>
|
163
|
+
<div class="pilwrap">
|
164
|
+
<a class="pilcrow" href="#section-10">¶</a>
|
165
|
+
</div>
|
166
|
+
<p>An API template with the name <code>:name_only</code> was created.</p>
|
167
|
+
|
168
|
+
<p>See below how to use it in the controller:</p>
|
169
|
+
</td>
|
170
|
+
<td class=code>
|
171
|
+
<div class='highlight'><pre></pre></div>
|
172
|
+
</td>
|
173
|
+
</tr>
|
174
|
+
<tr id='section-Setting_up_your_Controller'>
|
175
|
+
<td class=docs>
|
176
|
+
<div class="pilwrap">
|
177
|
+
<a class="pilcrow" href="#section-Setting_up_your_Controller">¶</a>
|
178
|
+
</div>
|
179
|
+
<h3>Setting up your Controller</h3>
|
180
|
+
</td>
|
181
|
+
<td class=code>
|
182
|
+
<div class='highlight'><pre></pre></div>
|
183
|
+
</td>
|
184
|
+
</tr>
|
185
|
+
<tr id='section-12'>
|
186
|
+
<td class=docs>
|
187
|
+
<div class="pilwrap">
|
188
|
+
<a class="pilcrow" href="#section-12">¶</a>
|
189
|
+
</div>
|
190
|
+
<p>Now you just have to exchange the <code>render</code> method in your controller for the <code>render_for_api</code> method.</p>
|
191
|
+
</td>
|
192
|
+
<td class=code>
|
193
|
+
<div class='highlight'><pre><span class="k">class</span> <span class="nc">UsersController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
|
194
|
+
|
195
|
+
<span class="k">def</span> <span class="nf">index</span>
|
196
|
+
<span class="vi">@users</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">all</span></pre></div>
|
197
|
+
</td>
|
198
|
+
</tr>
|
199
|
+
<tr id='section-13'>
|
200
|
+
<td class=docs>
|
201
|
+
<div class="pilwrap">
|
202
|
+
<a class="pilcrow" href="#section-13">¶</a>
|
203
|
+
</div>
|
204
|
+
<p>Note that it’s wise to add a <code>root</code> param when rendering lists.</p>
|
205
|
+
</td>
|
206
|
+
<td class=code>
|
207
|
+
<div class='highlight'><pre> <span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="nb">format</span><span class="o">|</span>
|
208
|
+
<span class="nb">format</span><span class="o">.</span><span class="n">xml</span> <span class="p">{</span> <span class="n">render_for_api</span> <span class="ss">:name_only</span><span class="p">,</span> <span class="ss">:xml</span> <span class="o">=></span> <span class="vi">@users</span><span class="p">,</span> <span class="ss">:root</span> <span class="o">=></span> <span class="ss">:users</span> <span class="p">}</span>
|
209
|
+
<span class="nb">format</span><span class="o">.</span><span class="n">json</span> <span class="p">{</span> <span class="n">render_for_api</span> <span class="ss">:name_only</span><span class="p">,</span> <span class="ss">:json</span> <span class="o">=></span> <span class="vi">@users</span><span class="p">,</span> <span class="ss">:root</span> <span class="o">=></span> <span class="ss">:users</span> <span class="p">}</span>
|
210
|
+
<span class="k">end</span>
|
211
|
+
<span class="k">end</span>
|
212
|
+
|
213
|
+
<span class="k">def</span> <span class="nf">show</span>
|
214
|
+
<span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span><span class="p">)</span>
|
215
|
+
|
216
|
+
<span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="nb">format</span><span class="o">|</span>
|
217
|
+
<span class="nb">format</span><span class="o">.</span><span class="n">xml</span> <span class="p">{</span> <span class="n">render_for_api</span> <span class="ss">:name_only</span><span class="p">,</span> <span class="ss">:xml</span> <span class="o">=></span> <span class="vi">@user</span> <span class="p">}</span>
|
218
|
+
<span class="nb">format</span><span class="o">.</span><span class="n">json</span> <span class="p">{</span> <span class="n">render_for_api</span> <span class="ss">:name_only</span><span class="p">,</span> <span class="ss">:json</span> <span class="o">=></span> <span class="vi">@user</span> <span class="p">}</span>
|
219
|
+
<span class="k">end</span>
|
220
|
+
<span class="k">end</span>
|
221
|
+
|
222
|
+
<span class="k">end</span></pre></div>
|
223
|
+
</td>
|
224
|
+
</tr>
|
225
|
+
<tr id='section-That&rsquo;s_it!'>
|
226
|
+
<td class=docs>
|
227
|
+
<div class="pilwrap">
|
228
|
+
<a class="pilcrow" href="#section-That&rsquo;s_it!">¶</a>
|
229
|
+
</div>
|
230
|
+
<h3>That’s it!</h3>
|
231
|
+
</td>
|
232
|
+
<td class=code>
|
233
|
+
<div class='highlight'><pre></pre></div>
|
234
|
+
</td>
|
235
|
+
</tr>
|
236
|
+
<tr id='section-15'>
|
237
|
+
<td class=docs>
|
238
|
+
<div class="pilwrap">
|
239
|
+
<a class="pilcrow" href="#section-15">¶</a>
|
240
|
+
</div>
|
241
|
+
<p>Try it. The JSON response of #show should now look like this:</p>
|
242
|
+
|
243
|
+
<p>Other attributes of the model like <code>created_at</code> or <code>updated_at</code> won’t be included
|
244
|
+
because they were not listed by <code>api_accessible</code> in the model.</p>
|
245
|
+
</td>
|
246
|
+
<td class=code>
|
247
|
+
<div class='highlight'><pre><span class="p">{</span>
|
248
|
+
<span class="s2">"user"</span><span class="p">:</span> <span class="p">{</span>
|
249
|
+
<span class="s2">"first_name"</span><span class="p">:</span> <span class="s2">"John"</span><span class="p">,</span>
|
250
|
+
<span class="s2">"last_name"</span><span class="p">:</span> <span class="s2">"Doe"</span>
|
251
|
+
<span class="p">}</span>
|
252
|
+
<span class="p">}</span></pre></div>
|
253
|
+
</td>
|
254
|
+
</tr>
|
255
|
+
<tr id='section-16'>
|
256
|
+
<td class=docs>
|
257
|
+
<div class="pilwrap">
|
258
|
+
<a class="pilcrow" href="#section-16">¶</a>
|
259
|
+
</div>
|
260
|
+
<hr />
|
261
|
+
</td>
|
262
|
+
<td class=code>
|
263
|
+
<div class='highlight'><pre></pre></div>
|
264
|
+
</td>
|
265
|
+
</tr>
|
266
|
+
<tr id='section-But_wait!_&hellip;_there&rsquo;s_more'>
|
267
|
+
<td class=docs>
|
268
|
+
<div class="pilwrap">
|
269
|
+
<a class="pilcrow" href="#section-But_wait!_&hellip;_there&rsquo;s_more">¶</a>
|
270
|
+
</div>
|
271
|
+
<h2>But wait! … there’s more</h2>
|
272
|
+
|
273
|
+
<p>Often the pure rendering of database values is just not enough, so acts_as_api
|
274
|
+
provides you some tools to customize your API responses.</p>
|
275
|
+
</td>
|
276
|
+
<td class=code>
|
277
|
+
<div class='highlight'><pre></pre></div>
|
278
|
+
</td>
|
279
|
+
</tr>
|
280
|
+
<tr id='section-What_can_I_include_in_my_responses?'>
|
281
|
+
<td class=docs>
|
282
|
+
<div class="pilwrap">
|
283
|
+
<a class="pilcrow" href="#section-What_can_I_include_in_my_responses?">¶</a>
|
284
|
+
</div>
|
285
|
+
<h3>What can I include in my responses?</h3>
|
286
|
+
|
287
|
+
<p>You can do basically anything:</p>
|
288
|
+
|
289
|
+
<ul>
|
290
|
+
<li>Include attributes and all other kinds of methods of your model</li>
|
291
|
+
<li>Include child associations (if they also act_as_api this will be considered)</li>
|
292
|
+
<li>Include lambdas and Procs</li>
|
293
|
+
<li>Call methods of a parent association</li>
|
294
|
+
<li>Call scopes of your model or child associations</li>
|
295
|
+
<li>Rename attributes, methods, associations</li>
|
296
|
+
<li>Create your own hierarchies</li>
|
297
|
+
</ul>
|
298
|
+
|
299
|
+
|
300
|
+
<p>You can find advanced examples in the <a href="https://github.com/fabrik42/acts_as_api/wiki/">Github Wiki</a></p>
|
301
|
+
</td>
|
302
|
+
<td class=code>
|
303
|
+
<div class='highlight'><pre></pre></div>
|
304
|
+
</td>
|
305
|
+
</tr>
|
306
|
+
<tr id='section-19'>
|
307
|
+
<td class=docs>
|
308
|
+
<div class="pilwrap">
|
309
|
+
<a class="pilcrow" href="#section-19">¶</a>
|
310
|
+
</div>
|
311
|
+
<hr />
|
312
|
+
</td>
|
313
|
+
<td class=code>
|
314
|
+
<div class='highlight'><pre></pre></div>
|
315
|
+
</td>
|
316
|
+
</tr>
|
317
|
+
<tr id='section-Links'>
|
318
|
+
<td class=docs>
|
319
|
+
<div class="pilwrap">
|
320
|
+
<a class="pilcrow" href="#section-Links">¶</a>
|
321
|
+
</div>
|
322
|
+
<h2>Links</h2>
|
323
|
+
|
324
|
+
<ul>
|
325
|
+
<li>Check out the <a href="https://github.com/fabrik42/acts_as_api/">source code on Github</a></li>
|
326
|
+
<li>For more usage examples visit the <a href="https://github.com/fabrik42/acts_as_api/wiki/">wiki</a></li>
|
327
|
+
<li>Found a bug or do you have a feature request? <a href="https://github.com/fabrik42/acts_as_api/issues">issue tracker</a></li>
|
328
|
+
<li><a href="http://rdoc.info/github/fabrik42/acts_as_api">Docs</a></li>
|
329
|
+
</ul>
|
330
|
+
|
331
|
+
|
332
|
+
</td>
|
333
|
+
<td class=code>
|
334
|
+
<div class='highlight'><pre></pre></div>
|
335
|
+
</td>
|
336
|
+
</tr>
|
337
|
+
</table>
|
338
|
+
</div>
|
339
|
+
<a href="https://github.com/fabrik42/acts_as_api"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://assets1.github.com/img/7afbc8b248c68eb468279e8c17986ad46549fb71?repo=&url=http%3A%2F%2Fs3.amazonaws.com%2Fgithub%2Fribbons%2Fforkme_right_darkblue_121621.png&path=" alt="Fork me on GitHub"></a>
|
340
|
+
</body>
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# The built-in XML/JSON support of Rails is great but:
|
2
|
+
# You surely don’t want to expose your models always with all attributes.
|
3
|
+
#
|
4
|
+
# acts_as_api enriches the models and controllers of your app in a rails-like way so you can easily determine how your API responses should look like.
|
5
|
+
|
6
|
+
### Features
|
7
|
+
# * DRY templates for your api responses
|
8
|
+
# * Easy but very flexible syntax for defining the templates
|
9
|
+
# * XML, JSON and JSON-P support out of the box, easy to extend
|
10
|
+
# * Minimal dependecies (you can also use it without Rails)
|
11
|
+
# * Does not rely on ActiveRecord (can be used with other ORMs like Mongoid)
|
12
|
+
# * Supports multiple api rendering templates for a models. This is especially useful for API versioning or for example for private vs. public access points to a user’s profile.
|
13
|
+
|
14
|
+
# ***
|
15
|
+
|
16
|
+
### Rails 3.0.x Quickstart
|
17
|
+
|
18
|
+
# Add to gemfile
|
19
|
+
gem 'acts_as_api'
|
20
|
+
|
21
|
+
# Update your bundle
|
22
|
+
bundle install
|
23
|
+
|
24
|
+
#### Setting up your Model
|
25
|
+
|
26
|
+
# Given you have a model `User`.
|
27
|
+
# If you only want to expose the `first_name` and `last_name` attribute of a user via your api, you would do something like this:
|
28
|
+
|
29
|
+
# Within your model:
|
30
|
+
#
|
31
|
+
# First you activate acts_as_api for your model by calling `acts_as_api`.
|
32
|
+
#
|
33
|
+
# Then you define an api template to render the model with `api_accessible`.
|
34
|
+
class User < ActiveRecord::Base
|
35
|
+
|
36
|
+
acts_as_api
|
37
|
+
|
38
|
+
api_accessible :name_only do |template|
|
39
|
+
template.add :first_name
|
40
|
+
template.add :last_name
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
# An API template with the name `:name_only` was created.
|
46
|
+
#
|
47
|
+
# See below how to use it in the controller:
|
48
|
+
|
49
|
+
#### Setting up your Controller
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
# Now you just have to exchange the `render` method in your controller for the `render_for_api` method.
|
54
|
+
class UsersController < ApplicationController
|
55
|
+
|
56
|
+
def index
|
57
|
+
@users = User.all
|
58
|
+
# Note that it's wise to add a `root` param when rendering lists.
|
59
|
+
respond_to do |format|
|
60
|
+
format.xml { render_for_api :name_only, :xml => @users, :root => :users }
|
61
|
+
format.json { render_for_api :name_only, :json => @users, :root => :users }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def show
|
66
|
+
@user = User.find(params[:id])
|
67
|
+
|
68
|
+
respond_to do |format|
|
69
|
+
format.xml { render_for_api :name_only, :xml => @user }
|
70
|
+
format.json { render_for_api :name_only, :json => @user }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
#### That's it!
|
77
|
+
|
78
|
+
# Try it. The JSON response of #show should now look like this:
|
79
|
+
#
|
80
|
+
# Other attributes of the model like `created_at` or `updated_at` won’t be included
|
81
|
+
# because they were not listed by `api_accessible` in the model.
|
82
|
+
{
|
83
|
+
"user": {
|
84
|
+
"first_name": "John",
|
85
|
+
"last_name": "Doe"
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
# ***
|
90
|
+
|
91
|
+
### But wait! ... there's more
|
92
|
+
#
|
93
|
+
# Often the pure rendering of database values is just not enough, so acts_as_api
|
94
|
+
# provides you some tools to customize your API responses.
|
95
|
+
|
96
|
+
#### What can I include in my responses?
|
97
|
+
#
|
98
|
+
# You can do basically anything:
|
99
|
+
#
|
100
|
+
# * Include attributes and all other kinds of methods of your model
|
101
|
+
# * Include child associations (if they also act_as_api this will be considered)
|
102
|
+
# * Include lambdas and Procs
|
103
|
+
# * Call methods of a parent association
|
104
|
+
# * Call scopes of your model or child associations
|
105
|
+
# * Rename attributes, methods, associations
|
106
|
+
# * Create your own hierarchies
|
107
|
+
#
|
108
|
+
# You can find advanced examples in the [Github Wiki][wi]
|
109
|
+
#
|
110
|
+
# [wi]: https://github.com/fabrik42/acts_as_api/wiki/
|
111
|
+
|
112
|
+
# ***
|
113
|
+
|
114
|
+
### Links
|
115
|
+
# * Check out the [source code on Github][so]
|
116
|
+
# * For more usage examples visit the [wiki][wi]
|
117
|
+
# * Found a bug or do you have a feature request? [issue tracker][to]
|
118
|
+
# * [Docs][do]
|
119
|
+
#
|
120
|
+
# [so]: https://github.com/fabrik42/acts_as_api/
|
121
|
+
# [wi]: https://github.com/fabrik42/acts_as_api/wiki/
|
122
|
+
# [to]: https://github.com/fabrik42/acts_as_api/issues
|
123
|
+
# [do]: http://rdoc.info/github/fabrik42/acts_as_api
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|