restapi 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. data/.autotest +3 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/Gemfile +15 -0
  6. data/Gemfile.lock +118 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.rdoc +0 -0
  9. data/Rakefile +13 -0
  10. data/app/controllers/restapis_controller.rb +11 -0
  11. data/app/public/javascripts/backbone.js +1431 -0
  12. data/app/public/javascripts/bootstrap-collapse.js +138 -0
  13. data/app/public/javascripts/bootstrap.js +1726 -0
  14. data/app/public/javascripts/jquery-1.7.2.js +9404 -0
  15. data/app/public/javascripts/json2.js +487 -0
  16. data/app/public/javascripts/restapi/jst.js +108 -0
  17. data/app/public/javascripts/restapi/restapi.js +14 -0
  18. data/app/public/javascripts/restapi/routers/documentation_router.js +47 -0
  19. data/app/public/javascripts/underscore.js +999 -0
  20. data/app/public/stylesheets/bootstrap-responsive.css +686 -0
  21. data/app/public/stylesheets/bootstrap-responsive.min.css +12 -0
  22. data/app/public/stylesheets/bootstrap.css +3990 -0
  23. data/app/public/stylesheets/bootstrap.min.css +689 -0
  24. data/app/public/stylesheets/restapi/application.css +7 -0
  25. data/app/views/restapis/index.html.erb +47 -0
  26. data/lib/restapi.rb +22 -0
  27. data/lib/restapi/application.rb +155 -0
  28. data/lib/restapi/dsl_definition.rb +119 -0
  29. data/lib/restapi/error_description.rb +15 -0
  30. data/lib/restapi/method_description.rb +38 -0
  31. data/lib/restapi/param_description.rb +48 -0
  32. data/lib/restapi/railtie.rb +9 -0
  33. data/lib/restapi/resource_description.rb +75 -0
  34. data/lib/restapi/restapi_module.rb +54 -0
  35. data/lib/restapi/routing.rb +15 -0
  36. data/lib/restapi/validator.rb +193 -0
  37. data/lib/restapi/version.rb +4 -0
  38. data/restapi.gemspec +23 -0
  39. data/spec/controllers/restapis_controller_spec.rb +13 -0
  40. data/spec/controllers/users_controller_spec.rb +175 -0
  41. data/spec/dummy/Rakefile +7 -0
  42. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  43. data/spec/dummy/app/controllers/dogs_controller.rb +22 -0
  44. data/spec/dummy/app/controllers/twitter_example_controller.rb +306 -0
  45. data/spec/dummy/app/controllers/users_controller.rb +211 -0
  46. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  47. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  48. data/spec/dummy/app/views/users/doc.html.erb +24 -0
  49. data/spec/dummy/config.ru +4 -0
  50. data/spec/dummy/config/application.rb +45 -0
  51. data/spec/dummy/config/boot.rb +10 -0
  52. data/spec/dummy/config/database.yml +22 -0
  53. data/spec/dummy/config/environment.rb +5 -0
  54. data/spec/dummy/config/environments/development.rb +25 -0
  55. data/spec/dummy/config/environments/production.rb +49 -0
  56. data/spec/dummy/config/environments/test.rb +35 -0
  57. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  58. data/spec/dummy/config/initializers/inflections.rb +10 -0
  59. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  60. data/spec/dummy/config/initializers/restapi.rb +30 -0
  61. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  62. data/spec/dummy/config/initializers/session_store.rb +8 -0
  63. data/spec/dummy/config/locales/en.yml +5 -0
  64. data/spec/dummy/config/routes.rb +69 -0
  65. data/spec/dummy/public/404.html +26 -0
  66. data/spec/dummy/public/422.html +26 -0
  67. data/spec/dummy/public/500.html +26 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/dummy/public/javascripts/application.js +2 -0
  70. data/spec/dummy/public/javascripts/controls.js +965 -0
  71. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  72. data/spec/dummy/public/javascripts/effects.js +1123 -0
  73. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  74. data/spec/dummy/public/javascripts/rails.js +202 -0
  75. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  76. data/spec/dummy/script/rails +6 -0
  77. data/spec/spec_helper.rb +32 -0
  78. metadata +207 -0
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,22 @@
1
+ class DogsController < ApplicationController
2
+
3
+ api :desc => "Show dogs profile",
4
+ :path => "/dogs/:id",
5
+ :method => "GET"
6
+ error :code => 401, :desc => "Unauthorized"
7
+ error :code => 404, :desc => "Not Found"
8
+ desc "+Show dog+ This is *description* of dog show method."
9
+ def show
10
+ render :nothing => true
11
+ end
12
+
13
+ #############################################################################
14
+
15
+ api :desc => "List all dogs",
16
+ :path => "/dogs",
17
+ :method => "GET"
18
+ desc "List all dogs which are registered on our social site."
19
+ def index
20
+ render :text => "List of dogs"
21
+ end
22
+ end
@@ -0,0 +1,306 @@
1
+ class TwitterExampleController < ApplicationController
2
+
3
+ resource_description do
4
+ name 'Users'
5
+ short_description 'Users are at the center of everything Twitter: they follow, they favorite, and tweet & retweet.'
6
+ path '/users/'
7
+ end
8
+
9
+ api :desc => 'Return up to 100 users worth of extended information, specified by either ID, screen name, or combination of the two.',
10
+ :path => '/users/lookup',
11
+ :method => 'GET'
12
+ param :screen_name, String, :desc => 'A comma separated list of screen names, up to 100 are allowed in a single request. You are strongly encouraged to use a POST for larger (up to 100 screen names) requests.'
13
+ param :user_id, Integer, :desc => 'A comma separated list of user IDs, up to 100 are allowed in a single request. You are strongly encouraged to use a POST for larger requests.'
14
+ param :include_entities, String, :desc => 'When set to either <code>true</code>, <code>t</code> or <code>1</code>, each tweet will include a node called "entities,". This node offers a variety of metadata about the tweet in a discreet structure, including: user_mentions, urls, and hashtags. While entities are opt-in on timelines at present, they will be made a default component of output in the future. See Tweet Entities for more detail on entities.'
15
+
16
+ description <<-EOS
17
+ Return up to 100 users worth of extended information, specified by either ID, screen name, or combination of the two. The author's most recent status (if the authenticating user has permission) will be returned inline.
18
+
19
+ This method is crucial for consumers of the {Streaming API}[link:https://dev.twitter.com/pages/streaming_api]. It's also well suited for use in tandem with friends/ids[link:https://dev.twitter.com/doc/get/friends/ids] and followers/ids[link:https://dev.twitter.com/doc/get/followers/ids].
20
+
21
+ === Extended description
22
+ There are a few things to note when using this method.
23
+
24
+ * You must be following a protected user to be able to see their most recent status update. If you don't follow a protected user their status will be removed.
25
+ * The order of user IDs or screen names may not match the order of users in the returned array.
26
+ * If a requested user is unknown, suspended, or deleted, then that user will not be returned in the results list.
27
+ * You are strongly encouraged to use a POST for larger requests.
28
+ EOS
29
+ def lookup
30
+ render :text => "lookup"
31
+ end
32
+
33
+ api :desc => 'Access the profile image in various sizes for the user with the indicated screen_name.',
34
+ :path => '/users/profile_image/:screen_name',
35
+ :method => 'GET'
36
+ param :screen_name, String, :required => true, :desc => 'The screen name of the user for whom to return results for. Helpful for disambiguating when a valid screen name is also a user ID.'
37
+ param :size, ['bigger','normal','mini','original'], :desc => <<-EOS
38
+ Specifies the size of image to fetch. Not specifying a size will give the default, normal size of 48px by 48px. Valid options include:
39
+
40
+ * bigger - 73px by 73px
41
+ * normal - 48px by 48px
42
+ * mini - 24px by 24px
43
+ * original - undefined. This will be the size the image was originally uploaded in. The filesize of original images can be very big so use this parameter with caution.
44
+ EOS
45
+ description <<-EOS
46
+ Access the profile image in various sizes for the user with the indicated screen_name. If no size is provided the normal image is returned.
47
+
48
+ This resource does not return JSON or XML, but instead returns a 302 redirect to the actual image resource.
49
+
50
+ This method should only be used by application developers to lookup or check the profile image URL for a user. This method must not be used as the image source URL presented to users of your application.
51
+ EOS
52
+ def profile_image
53
+ render :text => "profile_image"
54
+ end
55
+
56
+ api :path => '/users/search', :method => 'GET',
57
+ :desc => 'Runs a search for users similar to Find People button on Twitter.com.'
58
+ param :q, String, :desc => 'The search query to run against people search.', :required => true
59
+ param :page, Integer, :desc => 'Specifies the page of results to retrieve.'
60
+ param :per_page, Integer, :desc => 'The number of people to retrieve. Maxiumum of 20 allowed per page.'
61
+ param :include_entities, String, :desc => 'When set to either true, t or 1, each tweet will include a node called "entities,". This node offers a variety of metadata about the tweet in a discreet structure, including: user_mentions, urls, and hashtags. While entities are opt-in on timelines at present, they will be made a default component of output in the future. See Tweet Entities for more detail on entities.'
62
+ description <<-EOS
63
+ Runs a search for users similar to Find People button on Twitter.com. The results returned by people search on Twitter.com are the same as those returned by this API request. Note that unlike GET search, this method does not support any operators.
64
+
65
+ Only the first 1000 matches are available.
66
+
67
+ === Extended description
68
+ This method has a feature-specific rate limit of 60 calls per hour that is applied in conjunction with the main REST API rate limit. Calls to this method will count against the feature-specific rate limit and the main REST API rate limit. If either limit is exhausted, the request will fail. You can monitor the status of the feature-specific rate limit by inspecting the HTTP response headers <tt>X-FeatureRateLimit-Limit</tt>, <tt>X-FeatureRateLimit-Remaining</tt>, and <tt>X-FeatureRateLimit-Reset</tt>. These headers correspond to the <tt>X-RateLimit</tt> headers provided by the main REST API limit.
69
+ [
70
+ {
71
+ "name": "Twitter API",
72
+ "profile_sidebar_border_color": "87bc44",
73
+ "profile_background_tile": false,
74
+ "profile_sidebar_fill_color": "e0ff92",
75
+ "location": "San Francisco, CA",
76
+ "profile_image_url": "http://a3.twimg.com/profile_images/689684365/api_normal.png",
77
+ "created_at": "Wed May 23 06:01:13 +0000 2007",
78
+ "profile_link_color": "0000ff",
79
+ "favourites_count": 2,
80
+ "url": "http://apiwiki.twitter.com",
81
+ "contributors_enabled": true,
82
+ "utc_offset": -28800,
83
+ "id": 6253282,
84
+ "profile_use_background_image": true,
85
+ "profile_text_color": "000000",
86
+ "protected": false,
87
+ "followers_count": 160753,
88
+ "lang": "en",
89
+ "verified": true,
90
+ "profile_background_color": "c1dfee",
91
+ "geo_enabled": true,
92
+ "notifications": null,
93
+ "description": "The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
94
+ "time_zone": "Pacific Time (US & Canada)",
95
+ "friends_count": 19,
96
+ "statuses_count": 1858,
97
+ "profile_background_image_url": "http://a3.twimg.com/profile_background_images/59931895/twitterapi-background-new.png",
98
+ "status": {
99
+ "coordinates": null,
100
+ "favorited": false,
101
+ "created_at": "Tue Jun 22 16:53:28 +0000 2010",
102
+ "truncated": false,
103
+ "text": "@Demonicpagan possible some part of your signature generation is incorrect & fails for real reasons.. follow up on the list if you suspect",
104
+ "contributors": null,
105
+ "id": 16783999399,
106
+ "geo": null,
107
+ "in_reply_to_user_id": 6339722,
108
+ "place": null,
109
+ "source": "<a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>",
110
+ "in_reply_to_screen_name": "Demonicpagan",
111
+ "in_reply_to_status_id": 16781827477
112
+ },
113
+ "screen_name": "twitterapi",
114
+ "following": null
115
+ },
116
+ ...
117
+ {
118
+ "name": "twitterAPI",
119
+ "profile_sidebar_border_color": "87bc44",
120
+ "profile_background_tile": false,
121
+ "profile_sidebar_fill_color": "e0ff92",
122
+ "location": null,
123
+ "profile_image_url": "http://s.twimg.com/a/1277162817/images/default_profile_6_normal.png",
124
+ "created_at": "Fri Jun 04 12:07:25 +0000 2010",
125
+ "profile_link_color": "0000ff",
126
+ "favourites_count": 0,
127
+ "url": null,
128
+ "contributors_enabled": false,
129
+ "utc_offset": null,
130
+ "id": 151851125,
131
+ "profile_use_background_image": true,
132
+ "profile_text_color": "000000",
133
+ "protected": false,
134
+ "followers_count": 0,
135
+ "lang": "ja",
136
+ "verified": false,
137
+ "profile_background_color": "9ae4e8",
138
+ "geo_enabled": false,
139
+ "notifications": false,
140
+ "description": null,
141
+ "time_zone": null,
142
+ "friends_count": 0,
143
+ "statuses_count": 0,
144
+ "profile_background_image_url": "http://s.twimg.com/a/1277162817/images/themes/theme1/bg.png",
145
+ "screen_name": "popoAPI",
146
+ "following": false
147
+ }
148
+ ]
149
+ EOS
150
+ def search
151
+ render :text => 'search'
152
+ end
153
+
154
+ api :path => '/users/show', :method => 'GET',
155
+ :desc => 'Returns extended information of a given user, specified by ID or screen name as per the required id parameter.'
156
+ param :user_id, Integer, :required => true, :desc => <<-EOS
157
+ The ID of the user for whom to return results for. Either an id or screen_name is required for this method.
158
+ _Note_: Specifies the ID of the user to befriend. Helpful for disambiguating when a valid user ID is also a valid screen name.
159
+ EOS
160
+ param :screen_name, String, :desc => 'The screen name of the user for whom to return results for. Either a id or screen_name is required for this method.'
161
+ description <<-EDOC
162
+ Returns extended information of a given user, specified by ID or screen name as per the required id parameter. The author's most recent status will be returned inline.
163
+
164
+ You must be following a protected user to be able to see their most recent status update. If you don't follow a protected user, or request this method without autenticating, the users status will be removed.
165
+
166
+ The URL pattern <tt>/version/users/show/:screen_name_or_user_id.format</tt> is still accepted but not recommended. As a sequence of numbers is a valid screen name we recommend using the +screen_name+ or +user_id+ parameter instead.
167
+ EDOC
168
+ def show
169
+ render :text => 'show'
170
+ end
171
+
172
+ api :path => '/users/contributors', :method => 'GET',
173
+ :description => 'Returns an array of users who can contribute to the specified account.'
174
+ param :user_id, Integer, :desc => 'The ID of the user for whom to return results for. Helpful for disambiguating when a valid user ID is also a valid screen name.'
175
+ param :screen_name, String, :desc => 'The screen name of the user for whom to return results for. Helpful for disambiguating when a valid screen name is also a user ID.'
176
+ param :include_entities, String
177
+ param :skip_status, ['t','true','1'],
178
+ :description => 'When set to either true, t or 1 statuses will not be included in the returned user objects.'
179
+
180
+ description <<-EDOC
181
+ === Example request
182
+ GET https://api.twitter.com/1/users/contributors.json?screen_name=twitterapi&include_entities=true&skip_status=true
183
+ [
184
+ {
185
+ "profile_sidebar_border_color": "C0DEED",
186
+ "profile_background_tile": false,
187
+ "name": "Matt Harris",
188
+ "profile_sidebar_fill_color": "DDEEF6",
189
+ "expanded_url": "http://themattharris.com",
190
+ "created_at": "Sat Feb 17 20:49:54 +0000 2007",
191
+ "location": "SFO/LHR/YVR/JAX/IAD/AUS",
192
+ "profile_image_url": "http://a1.twimg.com/profile_images/554181350/matt_normal.jpg",
193
+ "follow_request_sent": false,
194
+ "is_translator": false,
195
+ "profile_link_color": "0084B4",
196
+ "id_str": "777925",
197
+ "entities": {
198
+ "urls": [
199
+
200
+ ],
201
+ "hashtags": [
202
+
203
+ ],
204
+ "user_mentions": [
205
+ {
206
+ "name": "Cindy Li",
207
+ "id_str": "29733",
208
+ "id": 29733,
209
+ "indices": [
210
+ 45,
211
+ 53
212
+ ],
213
+ "screen_name": "cindyli"
214
+ }
215
+ ]
216
+ },
217
+ "default_profile": true,
218
+ "url": "http://t.co/292MnqA",
219
+ "contributors_enabled": false,
220
+ "favourites_count": 120,
221
+ "id": 777925,
222
+ "utc_offset": -28800,
223
+ "listed_count": 271,
224
+ "profile_use_background_image": true,
225
+ "followers_count": 6242,
226
+ "lang": "en",
227
+ "protected": false,
228
+ "profile_text_color": "333333",
229
+ "profile_background_color": "C0DEED",
230
+ "time_zone": "Pacific Time (US & Canada)",
231
+ "geo_enabled": true,
232
+ "description": "Developer Advocate at Twitter and married to @cindyli. NASA enthusiast, British expat and all around geek living in San Francisco.",
233
+ "notifications": false,
234
+ "verified": false,
235
+ "profile_background_image_url": "http://a0.twimg.com/images/themes/theme1/bg.png",
236
+ "statuses_count": 3835,
237
+ "display_url": "themattharris.com",
238
+ "friends_count": 360,
239
+ "default_profile_image": false,
240
+ "following": false,
241
+ "show_all_inline_media": false,
242
+ "screen_name": "themattharris"
243
+ },
244
+ ...
245
+ {
246
+ "profile_sidebar_border_color": "547980",
247
+ "profile_background_tile": true,
248
+ "name": "Ryan Sarver",
249
+ "profile_sidebar_fill_color": "F8FCF2",
250
+ "expanded_url": "http://sarver.org",
251
+ "created_at": "Mon Feb 26 18:05:55 +0000 2007",
252
+ "location": "San Francisco, CA",
253
+ "profile_image_url": "http://a2.twimg.com/profile_images/644997837/ryan_sarver_twitter_big_normal.jpg",
254
+ "follow_request_sent": false,
255
+ "is_translator": false,
256
+ "profile_link_color": "547980",
257
+ "id_str": "795649",
258
+ "entities": {
259
+ "urls": [
260
+
261
+ ],
262
+ "hashtags": [
263
+
264
+ ],
265
+ "user_mentions": [
266
+
267
+ ]
268
+ },
269
+ "default_profile": false,
270
+ "contributors_enabled": true,
271
+ "url": "http://t.co/Lzsetyk",
272
+ "favourites_count": 246,
273
+ "id": 795649,
274
+ "utc_offset": -28800,
275
+ "profile_use_background_image": true,
276
+ "listed_count": 1384,
277
+ "followers_count": 280756,
278
+ "lang": "en",
279
+ "protected": false,
280
+ "profile_text_color": "594F4F",
281
+ "profile_background_color": "45ADA8",
282
+ "time_zone": "Pacific Time (US & Canada)",
283
+ "geo_enabled": true,
284
+ "description": "platform/api at twitter",
285
+ "notifications": false,
286
+ "verified": false,
287
+ "friends_count": 1022,
288
+ "profile_background_image_url": "http://a0.twimg.com/profile_background_images/113854313/xa60e82408188860c483d73444d53e21.png",
289
+ "display_url": "sarver.org",
290
+ "default_profile_image": false,
291
+ "statuses_count": 7031,
292
+ "following": false,
293
+ "show_all_inline_media": true,
294
+ "screen_name": "rsarver"
295
+ }
296
+ ]
297
+ EDOC
298
+ def contributors
299
+ render :text => 'contributors'
300
+ end
301
+
302
+ def index
303
+ render :text => 'twitter example'
304
+ end
305
+
306
+ end
@@ -0,0 +1,211 @@
1
+ class UsersController < ApplicationController
2
+
3
+ resource_description do
4
+ name 'Members'
5
+ short 'Site members'
6
+ path '/users'
7
+ version '1.0 - 3.4.2012'
8
+ param :id, Fixnum, :desc => "User ID", :required => true
9
+ description <<-EOS
10
+ == Long description
11
+
12
+ Example resource for rest api documentation
13
+
14
+ These can now be accessed in <tt>shared/header</tt> with:
15
+
16
+ Headline: <%= headline %>
17
+ First name: <%= person.first_name %>
18
+
19
+ If you need to find out whether a certain local variable has been assigned a value in a particular render call,
20
+ you need to use the following pattern:
21
+
22
+ <% if local_assigns.has_key? :headline %>
23
+ Headline: <%= headline %>
24
+ <% end %>
25
+
26
+ Testing using <tt>defined? headline</tt> will not work. This is an implementation restriction.
27
+
28
+ === Template caching
29
+
30
+ By default, Rails will compile each template to a method in order to render it. When you alter a template,
31
+ Rails will check the file's modification time and recompile it in development mode.
32
+ EOS
33
+ end
34
+
35
+ api :short => "Show user profile",
36
+ :path => "/users/:id",
37
+ :method => "GET"
38
+ error :code => 401, :desc => "Unauthorized"
39
+ error :code => 404, :desc => "Not Found"
40
+ param :id, Fixnum, :desc => "user id", :required => true
41
+ param :id, Integer, :desc => "user id", :required => true
42
+ param :session, String, :desc => "user is logged in", :required => true
43
+ param :float_param, Float, :desc => "float param"
44
+ param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
45
+ param :array_param, [100, "one", "two", 1, 2], :desc => "array validator"
46
+ param :proc_param, lambda { |val|
47
+ val == "param value" ? true : "The only good value is 'param value'."
48
+ }, :desc => "proc validator"
49
+ description <<-eos
50
+ = Action View Base
51
+
52
+ Action View templates can be written in several ways. If the template file has a <tt>.erb</tt> extension then it uses a mixture of ERb
53
+ (included in Ruby) and HTML. If the template file has a <tt>.builder</tt> extension then Jim Weirich's Builder::XmlMarkup library is used.
54
+
55
+ == ERB
56
+
57
+ You trigger ERB by using embeddings such as <% %>, <% -%>, and <%= %>. The <%= %> tag set is used when you want output. Consider the
58
+ following loop for names:
59
+
60
+ <b>Names of all the people</b>
61
+ <% @people.each do |person| %>
62
+ Name: <%= person.name %><br/>
63
+ <% end %>
64
+
65
+ The loop is setup in regular embedding tags <% %> and the name is written using the output embedding tag <%= %>. Note that this
66
+ is not just a usage suggestion. Regular output functions like print or puts won't work with ERB templates. So this would be wrong:
67
+
68
+ <%# WRONG %>
69
+ Hi, Mr. <% puts "Frodo" %>
70
+
71
+ If you absolutely must write from within a function use +concat+.
72
+
73
+ <%- and -%> suppress leading and trailing whitespace, including the trailing newline, and can be used interchangeably with <% and %>.
74
+
75
+ === Using sub templates
76
+
77
+ Using sub templates allows you to sidestep tedious replication and extract common display structures in shared templates. The
78
+ classic example is the use of a header and footer (even though the Action Pack-way would be to use Layouts):
79
+
80
+ <%= render "shared/header" %>
81
+ Something really specific and terrific
82
+ <%= render "shared/footer" %>
83
+
84
+ As you see, we use the output embeddings for the render methods. The render call itself will just return a string holding the
85
+ result of the rendering. The output embedding writes it to the current template.
86
+
87
+ But you don't have to restrict yourself to static includes. Templates can share variables amongst themselves by using instance
88
+ variables defined using the regular embedding tags. Like this:
89
+
90
+ <% @page_title = "A Wonderful Hello" %>
91
+ <%= render "shared/header" %>
92
+
93
+ Now the header can pick up on the <tt>@page_title</tt> variable and use it for outputting a title tag:
94
+
95
+ <title><%= @page_title %></title>
96
+
97
+ === Passing local variables to sub templates
98
+
99
+ You can pass local variables to sub templates by using a hash with the variable names as keys and the objects as values:
100
+
101
+ <%= render "shared/header", { :headline => "Welcome", :person => person } %>
102
+
103
+ These can now be accessed in <tt>shared/header</tt> with:
104
+
105
+ Headline: <%= headline %>
106
+ First name: <%= person.first_name %>
107
+
108
+ If you need to find out whether a certain local variable has been assigned a value in a particular render call,
109
+ you need to use the following pattern:
110
+
111
+ <% if local_assigns.has_key? :headline %>
112
+ Headline: <%= headline %>
113
+ <% end %>
114
+
115
+ Testing using <tt>defined? headline</tt> will not work. This is an implementation restriction.
116
+
117
+ === Template caching
118
+
119
+ By default, Rails will compile each template to a method in order to render it. When you alter a template,
120
+ Rails will check the file's modification time and recompile it in development mode.
121
+
122
+ == Builder
123
+
124
+ Builder templates are a more programmatic alternative to ERB. They are especially useful for generating XML content. An XmlMarkup object
125
+ named +xml+ is automatically made available to templates with a <tt>.builder</tt> extension.
126
+
127
+ Here are some basic examples:
128
+
129
+ xml.em("emphasized") # => <em>emphasized</em>
130
+ xml.em { xml.b("emph & bold") } # => <em><b>emph &amp; bold</b></em>
131
+ xml.a("A Link", "href" => "http://onestepback.org") # => <a href="http://onestepback.org">A Link</a>
132
+ xml.target("name" => "compile", "option" => "fast") # => <target option="fast" name="compile"\>
133
+ # NOTE: order of attributes is not specified.
134
+
135
+ Any method with a block will be treated as an XML markup tag with nested markup in the block. For example, the following:
136
+
137
+ xml.div do
138
+ xml.h1(@person.name)
139
+ xml.p(@person.bio)
140
+ end
141
+
142
+ would produce something like:
143
+
144
+ <div>
145
+ <h1>David Heinemeier Hansson</h1>
146
+ <p>A product of Danish Design during the Winter of '79...</p>
147
+ </div>
148
+
149
+ A full-length RSS example actually used on Basecamp:
150
+
151
+ xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
152
+ xml.channel do
153
+ xml.title(@feed_title)
154
+ xml.link(@url)
155
+ xml.description "Basecamp: Recent items"
156
+ xml.language "en-us"
157
+ xml.ttl "40"
158
+
159
+ @recent_items.each do |item|
160
+ xml.item do
161
+ xml.title(item_title(item))
162
+ xml.description(item_description(item)) if item_description(item)
163
+ xml.pubDate(item_pubDate(item))
164
+ xml.guid(@person.firm.account.url + @recent_items.url(item))
165
+ xml.link(@person.firm.account.url + @recent_items.url(item))
166
+
167
+ xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ More builder documentation can be found at http://builder.rubyforge.org.
174
+ eos
175
+ def show
176
+ unless params[:session] == "secret_hash"
177
+ render :text => "Not authorized", :status => 401
178
+ return
179
+ end
180
+
181
+ unless params[:id].to_i == 5
182
+ render :text => "Not Found", :status => 404 and return
183
+ end
184
+
185
+ render :text => "OK"
186
+ end
187
+
188
+ api :desc => "Create user", :path => "/users", :method => "POST"
189
+ param :user, Hash, :desc => "User info" do
190
+ param :username, String, :desc => "Username for login", :required => true
191
+ param :password, String, :desc => "Password for login", :required => true
192
+ param :membership, ["standard","premium"], :desc => "User membership"
193
+ end
194
+ def create
195
+ render :text => "OK"
196
+ end
197
+
198
+ api :desc => "List users",
199
+ :path => "/users",
200
+ :method => "GET"
201
+ error :code => 401, :desc => "Unauthorized"
202
+ error :code => 404, :desc => "Not Found"
203
+ desc "List all users."
204
+ def index
205
+ render :text => "List of users"
206
+ end
207
+
208
+ def doc
209
+ @api = Restapi['users', :show]
210
+ end
211
+ end