apipie-rails 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/.autotest +3 -0
  2. data/.gitignore +6 -0
  3. data/.rspec +2 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +5 -0
  6. data/APACHE-LICENSE-2.0 +202 -0
  7. data/Gemfile +3 -0
  8. data/Gemfile.lock +115 -0
  9. data/MIT-LICENSE +20 -0
  10. data/NOTICE +4 -0
  11. data/README.rdoc +365 -0
  12. data/Rakefile +13 -0
  13. data/apipie-rails.gemspec +27 -0
  14. data/app/controllers/apipie/apipies_controller.rb +60 -0
  15. data/app/public/apipie/javascripts/apipie.js +6 -0
  16. data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +138 -0
  17. data/app/public/apipie/javascripts/bundled/bootstrap.js +1726 -0
  18. data/app/public/apipie/javascripts/bundled/jquery-1.7.2.js +9404 -0
  19. data/app/public/apipie/javascripts/bundled/prettify.js +28 -0
  20. data/app/public/apipie/stylesheets/application.css +7 -0
  21. data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +12 -0
  22. data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +689 -0
  23. data/app/public/apipie/stylesheets/bundled/prettify.css +30 -0
  24. data/app/views/apipie/apipies/_params.html.erb +22 -0
  25. data/app/views/apipie/apipies/_params_plain.html.erb +16 -0
  26. data/app/views/apipie/apipies/index.html.erb +36 -0
  27. data/app/views/apipie/apipies/method.html.erb +63 -0
  28. data/app/views/apipie/apipies/plain.html.erb +70 -0
  29. data/app/views/apipie/apipies/resource.html.erb +82 -0
  30. data/app/views/apipie/apipies/static.html.erb +101 -0
  31. data/app/views/layouts/apipie/apipie.html.erb +37 -0
  32. data/lib/apipie-rails.rb +12 -0
  33. data/lib/apipie/apipie_module.rb +105 -0
  34. data/lib/apipie/application.rb +225 -0
  35. data/lib/apipie/client/generator.rb +105 -0
  36. data/lib/apipie/client/template/Gemfile.tt +5 -0
  37. data/lib/apipie/client/template/README.tt +3 -0
  38. data/lib/apipie/client/template/base.rb.tt +33 -0
  39. data/lib/apipie/client/template/bin.rb.tt +110 -0
  40. data/lib/apipie/client/template/cli.rb.tt +25 -0
  41. data/lib/apipie/client/template/cli_command.rb.tt +129 -0
  42. data/lib/apipie/client/template/client.rb.tt +10 -0
  43. data/lib/apipie/client/template/resource.rb.tt +17 -0
  44. data/lib/apipie/dsl_definition.rb +139 -0
  45. data/lib/apipie/error_description.rb +21 -0
  46. data/lib/apipie/extractor.rb +143 -0
  47. data/lib/apipie/extractor/collector.rb +113 -0
  48. data/lib/apipie/extractor/recorder.rb +122 -0
  49. data/lib/apipie/extractor/writer.rb +356 -0
  50. data/lib/apipie/helpers.rb +24 -0
  51. data/lib/apipie/markup.rb +45 -0
  52. data/lib/apipie/method_description.rb +150 -0
  53. data/lib/apipie/param_description.rb +87 -0
  54. data/lib/apipie/railtie.rb +9 -0
  55. data/lib/apipie/resource_description.rb +83 -0
  56. data/lib/apipie/routing.rb +13 -0
  57. data/lib/apipie/static_dispatcher.rb +60 -0
  58. data/lib/apipie/validator.rb +292 -0
  59. data/lib/apipie/version.rb +3 -0
  60. data/lib/tasks/apipie.rake +156 -0
  61. data/rel-eng/packages/.readme +3 -0
  62. data/rel-eng/tito.props +5 -0
  63. data/rubygem-apipie-rails.spec +72 -0
  64. data/spec/controllers/apipies_controller_spec.rb +132 -0
  65. data/spec/controllers/users_controller_spec.rb +390 -0
  66. data/spec/dummy/Rakefile +7 -0
  67. data/spec/dummy/app/controllers/application_controller.rb +6 -0
  68. data/spec/dummy/app/controllers/twitter_example_controller.rb +302 -0
  69. data/spec/dummy/app/controllers/users_controller.rb +223 -0
  70. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  71. data/spec/dummy/config.ru +4 -0
  72. data/spec/dummy/config/application.rb +45 -0
  73. data/spec/dummy/config/boot.rb +10 -0
  74. data/spec/dummy/config/database.yml +21 -0
  75. data/spec/dummy/config/environment.rb +8 -0
  76. data/spec/dummy/config/environments/development.rb +25 -0
  77. data/spec/dummy/config/environments/production.rb +49 -0
  78. data/spec/dummy/config/environments/test.rb +35 -0
  79. data/spec/dummy/config/initializers/apipie.rb +64 -0
  80. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  81. data/spec/dummy/config/initializers/inflections.rb +10 -0
  82. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  83. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  84. data/spec/dummy/config/initializers/session_store.rb +8 -0
  85. data/spec/dummy/config/locales/en.yml +5 -0
  86. data/spec/dummy/config/routes.rb +21 -0
  87. data/spec/dummy/doc/apipie_examples.yml +28 -0
  88. data/spec/dummy/public/404.html +26 -0
  89. data/spec/dummy/public/422.html +26 -0
  90. data/spec/dummy/public/500.html +26 -0
  91. data/spec/dummy/public/favicon.ico +0 -0
  92. data/spec/dummy/public/javascripts/application.js +2 -0
  93. data/spec/dummy/public/javascripts/controls.js +965 -0
  94. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  95. data/spec/dummy/public/javascripts/effects.js +1123 -0
  96. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  97. data/spec/dummy/public/javascripts/rails.js +202 -0
  98. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  99. data/spec/dummy/script/rails +6 -0
  100. data/spec/spec_helper.rb +32 -0
  101. metadata +312 -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,6 @@
1
+ class ApplicationController < ActionController::Base
2
+
3
+ resource_description do
4
+ param :oauth, String, :desc => "Authorization", :required => false
5
+ end
6
+ end
@@ -0,0 +1,302 @@
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 '/twitter_example/'
7
+ description "Long description of this resource."
8
+ end
9
+
10
+ api :GET, '/twitter_example/lookup', 'Return up to 100 users worth of extended information, specified by either ID, screen name, or combination of the two.'
11
+ 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.'
12
+ 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.'
13
+ 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.'
14
+
15
+ description <<-EOS
16
+ 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.
17
+
18
+ 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].
19
+
20
+ === Extended description
21
+ There are a few things to note when using this method.
22
+
23
+ * 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.
24
+ * The order of user IDs or screen names may not match the order of users in the returned array.
25
+ * If a requested user is unknown, suspended, or deleted, then that user will not be returned in the results list.
26
+ * You are strongly encouraged to use a POST for larger requests.
27
+ EOS
28
+ def lookup
29
+ render :text => "lookup"
30
+ end
31
+
32
+ api :GET, '/twitter_example/profile_image/:screen_name', 'Access the profile image in various sizes for the user with the indicated screen_name.'
33
+ 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.'
34
+ param :size, ['bigger','normal','mini','original'], :desc => <<-EOS
35
+ Specifies the size of image to fetch. Not specifying a size will give the default, normal size of 48px by 48px. Valid options include:
36
+
37
+ * bigger - 73px by 73px
38
+ * normal - 48px by 48px
39
+ * mini - 24px by 24px
40
+ * 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.
41
+ EOS
42
+ description <<-EOS
43
+ 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.
44
+
45
+ This resource does not return JSON or XML, but instead returns a 302 redirect to the actual image resource.
46
+
47
+ 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.
48
+ EOS
49
+ def profile_image
50
+ render :text => "profile_image of '#{params[:screen_name]}'"
51
+ end
52
+
53
+ api :GET, '/twitter_example/search', 'Runs a search for users similar to Find People button on Twitter.com.'
54
+ param :q, String, :desc => 'The search query to run against people search.', :required => true
55
+ param :page, Integer, :desc => 'Specifies the page of results to retrieve.'
56
+ param :per_page, Integer, :desc => 'The number of people to retrieve. Maxiumum of 20 allowed per page.'
57
+ 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.'
58
+ description <<-EOS
59
+ 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.
60
+
61
+ Only the first 1000 matches are available.
62
+
63
+ === Extended description
64
+ 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.
65
+ EOS
66
+
67
+ example <<-EDOC
68
+ [
69
+ {
70
+ "name": "Twitter API",
71
+ "profile_sidebar_border_color": "87bc44",
72
+ "profile_background_tile": false,
73
+ "profile_sidebar_fill_color": "e0ff92",
74
+ "location": "San Francisco, CA",
75
+ "profile_image_url": "http://a3.twimg.com/profile_images/689684365/api_normal.png",
76
+ "created_at": "Wed May 23 06:01:13 +0000 2007",
77
+ "profile_link_color": "0000ff",
78
+ "favourites_count": 2,
79
+ "url": "http://apiwiki.twitter.com",
80
+ "contributors_enabled": true,
81
+ "utc_offset": -28800,
82
+ "id": 6253282,
83
+ "profile_use_background_image": true,
84
+ "profile_text_color": "000000",
85
+ "protected": false,
86
+ "followers_count": 160753,
87
+ "lang": "en",
88
+ "verified": true,
89
+ "profile_background_color": "c1dfee",
90
+ "geo_enabled": true,
91
+ "notifications": null,
92
+ "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.",
93
+ "time_zone": "Pacific Time (US & Canada)",
94
+ "friends_count": 19,
95
+ "statuses_count": 1858,
96
+ "profile_background_image_url": "http://a3.twimg.com/profile_background_images/59931895/twitterapi-background-new.png",
97
+ "status": {
98
+ "coordinates": null,
99
+ "favorited": false,
100
+ "created_at": "Tue Jun 22 16:53:28 +0000 2010",
101
+ "truncated": false,
102
+ "text": "@Demonicpagan possible some part of your signature generation is incorrect & fails for real reasons.. follow up on the list if you suspect",
103
+ "contributors": null,
104
+ "id": 16783999399,
105
+ "geo": null,
106
+ "in_reply_to_user_id": 6339722,
107
+ "place": null,
108
+ "source": "<a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>",
109
+ "in_reply_to_screen_name": "Demonicpagan",
110
+ "in_reply_to_status_id": 16781827477
111
+ },
112
+ "screen_name": "twitterapi",
113
+ "following": null
114
+ },
115
+ ...
116
+ {
117
+ "name": "twitterAPI",
118
+ "profile_sidebar_border_color": "87bc44",
119
+ "profile_background_tile": false,
120
+ "profile_sidebar_fill_color": "e0ff92",
121
+ "location": null,
122
+ "profile_image_url": "http://s.twimg.com/a/1277162817/images/default_profile_6_normal.png",
123
+ "created_at": "Fri Jun 04 12:07:25 +0000 2010",
124
+ "profile_link_color": "0000ff",
125
+ "favourites_count": 0,
126
+ "url": null,
127
+ "contributors_enabled": false,
128
+ "utc_offset": null,
129
+ "id": 151851125,
130
+ "profile_use_background_image": true,
131
+ "profile_text_color": "000000",
132
+ "protected": false,
133
+ "followers_count": 0,
134
+ "lang": "ja",
135
+ "verified": false,
136
+ "profile_background_color": "9ae4e8",
137
+ "geo_enabled": false,
138
+ "notifications": false,
139
+ "description": null,
140
+ "time_zone": null,
141
+ "friends_count": 0,
142
+ "statuses_count": 0,
143
+ "profile_background_image_url": "http://s.twimg.com/a/1277162817/images/themes/theme1/bg.png",
144
+ "screen_name": "popoAPI",
145
+ "following": false
146
+ }
147
+ ]
148
+ EDOC
149
+ def search
150
+ render :text => 'search'
151
+ end
152
+
153
+ api :GET, '/twitter_example/:id', 'Returns extended information of a given user, specified by ID or screen name as per the required id parameter.'
154
+ param :id, :number, :required => true, :desc => <<-EOS
155
+ The ID of the user for whom to return results for. Either an id or screen_name is required for this method.
156
+ EOS
157
+ param :screen_name, String, :desc => 'The screen name of the user for...'
158
+ description <<-EDOC
159
+ 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.
160
+ EDOC
161
+ def show
162
+ render :text => "show #{params}"
163
+ end
164
+
165
+ api :GET, '/twitter_example/contributors', 'Returns an array of users who can contribute to the specified account.'
166
+ 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.'
167
+ 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.'
168
+ param :include_entities, String
169
+ param :skip_status, ['t','true','1'],
170
+ :description => 'When set to either true, t or 1 statuses will not be included in the returned user objects.'
171
+
172
+ description "Look at examples."
173
+
174
+ example <<-EDOC
175
+ GET https://api.twitter.com/1/twitter_example/contributors.json?screen_name=twitterapi&include_entities=true&skip_status=true
176
+ [
177
+ {
178
+ "profile_sidebar_border_color": "C0DEED",
179
+ "profile_background_tile": false,
180
+ "name": "Matt Harris",
181
+ "profile_sidebar_fill_color": "DDEEF6",
182
+ "expanded_url": "http://themattharris.com",
183
+ "created_at": "Sat Feb 17 20:49:54 +0000 2007",
184
+ "location": "SFO/LHR/YVR/JAX/IAD/AUS",
185
+ "profile_image_url": "http://a1.twimg.com/profile_images/554181350/matt_normal.jpg",
186
+ "follow_request_sent": false,
187
+ "is_translator": false,
188
+ "profile_link_color": "0084B4",
189
+ "id_str": "777925",
190
+ "entities": {
191
+ "urls": [
192
+
193
+ ],
194
+ "hashtags": [
195
+
196
+ ],
197
+ "user_mentions": [
198
+ {
199
+ "name": "Cindy Li",
200
+ "id_str": "29733",
201
+ "id": 29733,
202
+ "indices": [
203
+ 45,
204
+ 53
205
+ ],
206
+ "screen_name": "cindyli"
207
+ }
208
+ ]
209
+ },
210
+ "default_profile": true,
211
+ "url": "http://t.co/292MnqA",
212
+ "contributors_enabled": false,
213
+ "favourites_count": 120,
214
+ "id": 777925,
215
+ "utc_offset": -28800,
216
+ "listed_count": 271,
217
+ "profile_use_background_image": true,
218
+ "followers_count": 6242,
219
+ "lang": "en",
220
+ "protected": false,
221
+ "profile_text_color": "333333",
222
+ "profile_background_color": "C0DEED",
223
+ "time_zone": "Pacific Time (US & Canada)",
224
+ "geo_enabled": true,
225
+ "description": "Developer Advocate at Twitter and married to @cindyli. NASA enthusiast, British expat and all around geek living in San Francisco.",
226
+ "notifications": false,
227
+ "verified": false,
228
+ "profile_background_image_url": "http://a0.twimg.com/images/themes/theme1/bg.png",
229
+ "statuses_count": 3835,
230
+ "display_url": "themattharris.com",
231
+ "friends_count": 360,
232
+ "default_profile_image": false,
233
+ "following": false,
234
+ "show_all_inline_media": false,
235
+ "screen_name": "themattharris"
236
+ },
237
+ ...
238
+ ]
239
+ EDOC
240
+ example <<-EDOC
241
+ Another example...
242
+ {
243
+ "profile_sidebar_border_color": "547980",
244
+ "profile_background_tile": true,
245
+ "name": "Ryan Sarver",
246
+ "profile_sidebar_fill_color": "F8FCF2",
247
+ "expanded_url": "http://sarver.org",
248
+ "created_at": "Mon Feb 26 18:05:55 +0000 2007",
249
+ "location": "San Francisco, CA",
250
+ "profile_image_url": "http://a2.twimg.com/profile_images/644997837/ryan_sarver_twitter_big_normal.jpg",
251
+ "follow_request_sent": false,
252
+ "is_translator": false,
253
+ "profile_link_color": "547980",
254
+ "id_str": "795649",
255
+ "entities": {
256
+ "urls": [
257
+
258
+ ],
259
+ "hashtags": [
260
+
261
+ ],
262
+ "user_mentions": [
263
+
264
+ ]
265
+ },
266
+ "default_profile": false,
267
+ "contributors_enabled": true,
268
+ "url": "http://t.co/Lzsetyk",
269
+ "favourites_count": 246,
270
+ "id": 795649,
271
+ "utc_offset": -28800,
272
+ "profile_use_background_image": true,
273
+ "listed_count": 1384,
274
+ "followers_count": 280756,
275
+ "lang": "en",
276
+ "protected": false,
277
+ "profile_text_color": "594F4F",
278
+ "profile_background_color": "45ADA8",
279
+ "time_zone": "Pacific Time (US & Canada)",
280
+ "geo_enabled": true,
281
+ "description": "platform/api at twitter",
282
+ "notifications": false,
283
+ "verified": false,
284
+ "friends_count": 1022,
285
+ "profile_background_image_url": "http://a0.twimg.com/profile_background_images/113854313/xa60e82408188860c483d73444d53e21.png",
286
+ "display_url": "sarver.org",
287
+ "default_profile_image": false,
288
+ "statuses_count": 7031,
289
+ "following": false,
290
+ "show_all_inline_media": true,
291
+ "screen_name": "rsarver"
292
+ }
293
+ EDOC
294
+ def contributors
295
+ render :text => 'contributors'
296
+ end
297
+
298
+ def index
299
+ render :text => 'twitter example'
300
+ end
301
+
302
+ end
@@ -0,0 +1,223 @@
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 => false
9
+ param :resource_param, Hash, :desc => 'Param description for all methods' do
10
+ param :ausername, String, :desc => "Username for login", :required => true
11
+ param :apassword, String, :desc => "Password for login", :required => true
12
+ end
13
+ description <<-EOS
14
+ == Long description
15
+
16
+ Example resource for rest api documentation
17
+
18
+ These can now be accessed in <tt>shared/header</tt> with:
19
+
20
+ Headline: <%= headline %>
21
+ First name: <%= person.first_name %>
22
+
23
+ If you need to find out whether a certain local variable has been assigned a value in a particular render call,
24
+ you need to use the following pattern:
25
+
26
+ <% if local_assigns.has_key? :headline %>
27
+ Headline: <%= headline %>
28
+ <% end %>
29
+
30
+ Testing using <tt>defined? headline</tt> will not work. This is an implementation restriction.
31
+
32
+ === Template caching
33
+
34
+ By default, Rails will compile each template to a method in order to render it. When you alter a template,
35
+ Rails will check the file's modification time and recompile it in development mode.
36
+ EOS
37
+ end
38
+
39
+ description <<-eos
40
+ = Action View Base
41
+
42
+ 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
43
+ (included in Ruby) and HTML. If the template file has a <tt>.builder</tt> extension then Jim Weirich's Builder::XmlMarkup library is used.
44
+
45
+ == ERB
46
+
47
+ You trigger ERB by using embeddings such as <% %>, <% -%>, and <%= %>. The <%= %> tag set is used when you want output. Consider the
48
+ following loop for names:
49
+
50
+ <b>Names of all the people</b>
51
+ <% @people.each do |person| %>
52
+ Name: <%= person.name %><br/>
53
+ <% end %>
54
+
55
+ The loop is setup in regular embedding tags <% %> and the name is written using the output embedding tag <%= %>. Note that this
56
+ is not just a usage suggestion. Regular output functions like print or puts won't work with ERB templates. So this would be wrong:
57
+
58
+ <%# WRONG %>
59
+ Hi, Mr. <% puts "Frodo" %>
60
+
61
+ If you absolutely must write from within a function use +concat+.
62
+
63
+ <%- and -%> suppress leading and trailing whitespace, including the trailing newline, and can be used interchangeably with <% and %>.
64
+
65
+ === Using sub templates
66
+
67
+ Using sub templates allows you to sidestep tedious replication and extract common display structures in shared templates. The
68
+ classic example is the use of a header and footer (even though the Action Pack-way would be to use Layouts):
69
+
70
+ <%= render "shared/header" %>
71
+ Something really specific and terrific
72
+ <%= render "shared/footer" %>
73
+
74
+ As you see, we use the output embeddings for the render methods. The render call itself will just return a string holding the
75
+ result of the rendering. The output embedding writes it to the current template.
76
+
77
+ But you don't have to restrict yourself to static includes. Templates can share variables amongst themselves by using instance
78
+ variables defined using the regular embedding tags. Like this:
79
+
80
+ <% @page_title = "A Wonderful Hello" %>
81
+ <%= render "shared/header" %>
82
+
83
+ Now the header can pick up on the <tt>@page_title</tt> variable and use it for outputting a title tag:
84
+
85
+ <title><%= @page_title %></title>
86
+
87
+ === Passing local variables to sub templates
88
+
89
+ You can pass local variables to sub templates by using a hash with the variable names as keys and the objects as values:
90
+
91
+ <%= render "shared/header", { :headline => "Welcome", :person => person } %>
92
+
93
+ These can now be accessed in <tt>shared/header</tt> with:
94
+
95
+ Headline: <%= headline %>
96
+ First name: <%= person.first_name %>
97
+
98
+ If you need to find out whether a certain local variable has been assigned a value in a particular render call,
99
+ you need to use the following pattern:
100
+
101
+ <% if local_assigns.has_key? :headline %>
102
+ Headline: <%= headline %>
103
+ <% end %>
104
+
105
+ Testing using <tt>defined? headline</tt> will not work. This is an implementation restriction.
106
+
107
+ === Template caching
108
+
109
+ By default, Rails will compile each template to a method in order to render it. When you alter a template,
110
+ Rails will check the file's modification time and recompile it in development mode.
111
+
112
+ == Builder
113
+
114
+ Builder templates are a more programmatic alternative to ERB. They are especially useful for generating XML content. An XmlMarkup object
115
+ named +xml+ is automatically made available to templates with a <tt>.builder</tt> extension.
116
+
117
+ Here are some basic examples:
118
+
119
+ xml.em("emphasized") # => <em>emphasized</em>
120
+ xml.em { xml.b("emph & bold") } # => <em><b>emph &amp; bold</b></em>
121
+ xml.a("A Link", "href" => "http://onestepback.org") # => <a href="http://onestepback.org">A Link</a>
122
+ xml.target("name" => "compile", "option" => "fast") # => <target option="fast" name="compile"\>
123
+ # NOTE: order of attributes is not specified.
124
+
125
+ Any method with a block will be treated as an XML markup tag with nested markup in the block. For example, the following:
126
+
127
+ xml.div do
128
+ xml.h1(@person.name)
129
+ xml.p(@person.bio)
130
+ end
131
+
132
+ would produce something like:
133
+
134
+ <div>
135
+ <h1>David Heinemeier Hansson</h1>
136
+ <p>A product of Danish Design during the Winter of '79...</p>
137
+ </div>
138
+
139
+ A full-length RSS example actually used on Basecamp:
140
+
141
+ xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
142
+ xml.channel do
143
+ xml.title(@feed_title)
144
+ xml.link(@url)
145
+ xml.description "Basecamp: Recent items"
146
+ xml.language "en-us"
147
+ xml.ttl "40"
148
+
149
+ @recent_items.each do |item|
150
+ xml.item do
151
+ xml.title(item_title(item))
152
+ xml.description(item_description(item)) if item_description(item)
153
+ xml.pubDate(item_pubDate(item))
154
+ xml.guid(@person.firm.account.url + @recent_items.url(item))
155
+ xml.link(@person.firm.account.url + @recent_items.url(item))
156
+
157
+ xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ More builder documentation can be found at http://builder.rubyforge.org.
164
+ eos
165
+ api :GET, "/users/:id", "Show user profile"
166
+ error 401, "Unauthorized"
167
+ error :code => 404, :description => "Not Found"
168
+ param :id, Integer, :desc => "user id", :required => true
169
+ param :session, String, :desc => "user is logged in", :required => true
170
+ param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
171
+ param :array_param, ["100", "one", "two", "1", "2"], :desc => "array validator"
172
+ param :boolean_param, [true, false], :desc => "array validator with boolean"
173
+ param :proc_param, lambda { |val|
174
+ val == "param value" ? true : "The only good value is 'param value'."
175
+ }, :desc => "proc validator"
176
+ def show
177
+ unless params[:session] == "secret_hash"
178
+ render :text => "Not authorized", :status => 401
179
+ return
180
+ end
181
+
182
+ unless params[:id].to_i == 5
183
+ render :text => "Not Found", :status => 404 and return
184
+ end
185
+
186
+ render :text => "OK"
187
+ end
188
+
189
+ api :POST, "/users", "Create user"
190
+ param :user, Hash, :desc => "User info", :required => true do
191
+ param :name, String, :desc => "Username for login", :required => true
192
+ param :pass, String, :desc => "Password for login", :required => true
193
+ param :membership, ["standard","premium"], :desc => "User membership"
194
+ end
195
+ param :facts, Hash, :desc => "Additional optional facts about the user", :allow_nil => true
196
+ def create
197
+ render :text => "OK #{params.inspect}"
198
+ end
199
+
200
+ api :GET, "/users", "List users"
201
+ error :code => 401, :desc => "Unauthorized"
202
+ error :code => 404, :desc => "Not Found"
203
+ desc "List all users."
204
+ param :oauth, nil,
205
+ :desc => "Hide this global param (eg dont need auth here)"
206
+ def index
207
+ render :text => "List of users"
208
+ end
209
+
210
+ api :GET, '/company_users', 'Get company users'
211
+ api :GET, '/company/:id/users', 'Get users working in given company'
212
+ param :id, Integer, :desc => "Company ID"
213
+ def two_urls
214
+ render :text => 'List of users'
215
+ end
216
+
217
+ api :GET, '/users/see_another', 'Boring method'
218
+ see 'users#create'
219
+ desc 'This method is boring, look at users#create'
220
+ def see_another
221
+ render :text => 'This is very similar to create action'
222
+ end
223
+ end