apipie-rails 0.3.6 → 0.5.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +23 -7
- data/CHANGELOG.md +147 -2
- data/Gemfile +1 -0
- data/Gemfile.rails41 +2 -0
- data/Gemfile.rails42 +10 -1
- data/Gemfile.rails50 +9 -0
- data/Gemfile.rails51 +9 -0
- data/Gemfile.rails60 +14 -0
- data/PROPOSAL_FOR_RESPONSE_DESCRIPTIONS.md +244 -0
- data/README.rst +570 -17
- data/apipie-rails.gemspec +3 -3
- data/app/controllers/apipie/apipies_controller.rb +48 -17
- data/app/views/apipie/apipies/_method_detail.erb +21 -0
- data/app/views/apipie/apipies/_params.html.erb +4 -2
- data/app/views/apipie/apipies/index.html.erb +5 -1
- data/app/views/apipie/apipies/resource.html.erb +3 -0
- data/app/views/layouts/apipie/apipie.html.erb +1 -1
- data/config/locales/en.yml +1 -0
- data/config/locales/fr.yml +31 -0
- data/config/locales/it.yml +31 -0
- data/config/locales/ja.yml +31 -0
- data/lib/apipie/apipie_module.rb +22 -4
- data/lib/apipie/application.rb +55 -28
- data/lib/apipie/configuration.rb +19 -3
- data/lib/apipie/core_ext/route.rb +9 -0
- data/lib/apipie/dsl_definition.rb +151 -10
- data/lib/apipie/error_description.rb +9 -2
- data/lib/apipie/errors.rb +34 -0
- data/lib/apipie/extractor/collector.rb +4 -0
- data/lib/apipie/extractor/recorder.rb +13 -12
- data/lib/apipie/extractor/writer.rb +83 -55
- data/lib/apipie/extractor.rb +10 -4
- data/lib/apipie/method_description.rb +51 -4
- data/lib/apipie/param_description.rb +56 -2
- data/lib/apipie/resource_description.rb +10 -3
- data/lib/apipie/response_description.rb +131 -0
- data/lib/apipie/response_description_adapter.rb +200 -0
- data/lib/apipie/routes_formatter.rb +1 -1
- data/lib/apipie/rspec/response_validation_helper.rb +194 -0
- data/lib/apipie/static_dispatcher.rb +3 -2
- data/lib/apipie/swagger_generator.rb +708 -0
- data/lib/apipie/tag_list_description.rb +11 -0
- data/lib/apipie/validator.rb +69 -8
- data/lib/apipie/version.rb +1 -1
- data/lib/apipie-rails.rb +7 -0
- data/lib/tasks/apipie.rake +103 -8
- data/spec/controllers/apipies_controller_spec.rb +52 -12
- data/spec/controllers/concerns_controller_spec.rb +2 -2
- data/spec/controllers/extended_controller_spec.rb +14 -0
- data/spec/controllers/memes_controller_spec.rb +10 -0
- data/spec/controllers/users_controller_spec.rb +115 -75
- data/spec/dummy/app/controllers/application_controller.rb +5 -1
- data/spec/dummy/app/controllers/concerns/extending_concern.rb +12 -0
- data/spec/dummy/app/controllers/concerns/sample_controller.rb +5 -5
- data/spec/dummy/app/controllers/extended_controller.rb +14 -0
- data/spec/dummy/app/controllers/pets_controller.rb +408 -0
- data/spec/dummy/app/controllers/pets_using_auto_views_controller.rb +73 -0
- data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +95 -0
- data/spec/dummy/app/controllers/tagged_cats_controller.rb +32 -0
- data/spec/dummy/app/controllers/tagged_dogs_controller.rb +15 -0
- data/spec/dummy/app/controllers/twitter_example_controller.rb +5 -0
- data/spec/dummy/app/controllers/users_controller.rb +19 -11
- data/spec/dummy/components/test_engine/Gemfile +6 -0
- data/spec/dummy/components/test_engine/app/controllers/test_engine/application_controller.rb +4 -0
- data/spec/dummy/components/test_engine/app/controllers/test_engine/memes_controller.rb +37 -0
- data/spec/dummy/components/test_engine/config/routes.rb +3 -0
- data/spec/dummy/components/test_engine/db/.gitkeep +0 -0
- data/spec/dummy/components/test_engine/lib/test_engine.rb +7 -0
- data/spec/dummy/components/test_engine/test_engine.gemspec +11 -0
- data/spec/dummy/config/application.rb +5 -0
- data/spec/dummy/config/environments/development.rb +3 -0
- data/spec/dummy/config/environments/production.rb +3 -0
- data/spec/dummy/config/environments/test.rb +3 -0
- data/spec/dummy/config/initializers/apipie.rb +3 -1
- data/spec/dummy/config/routes.rb +24 -1
- data/spec/lib/extractor/writer_spec.rb +32 -4
- data/spec/lib/file_handler_spec.rb +18 -0
- data/spec/lib/method_description_spec.rb +34 -0
- data/spec/lib/swagger/openapi_2_0_schema.json +1607 -0
- data/spec/lib/swagger/rake_swagger_spec.rb +139 -0
- data/spec/lib/swagger/response_validation_spec.rb +104 -0
- data/spec/lib/swagger/swagger_dsl_spec.rb +658 -0
- data/spec/lib/validator_spec.rb +58 -0
- data/spec/lib/validators/array_validator_spec.rb +28 -8
- data/spec/spec_helper.rb +68 -0
- metadata +75 -23
- data/Gemfile +0 -7
- data/Gemfile.rails32 +0 -6
- data/Gemfile.rails40 +0 -5
- data/lib/apipie/client/generator.rb +0 -135
@@ -0,0 +1,408 @@
|
|
1
|
+
#
|
2
|
+
# The PetsController defined here provides examples for different ways
|
3
|
+
# in which the 'returns' DSL directive can be used with the existing
|
4
|
+
# Apipie DSL elements 'param' and 'param_group'
|
5
|
+
#
|
6
|
+
|
7
|
+
class PetsController < ApplicationController
|
8
|
+
resource_description do
|
9
|
+
description 'A controller to test "returns"'
|
10
|
+
short 'Pets'
|
11
|
+
path '/pets'
|
12
|
+
|
13
|
+
param :common_param, Integer, :desc => "A param that can optionally be passed to all Pet methods", :required => false
|
14
|
+
|
15
|
+
returns :code => 404 do
|
16
|
+
property :error_message, String, "description of the error"
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
#-----------------------------------------------------------
|
22
|
+
# simple 'returns' example: a method that returns a pet record
|
23
|
+
#-----------------------------------------------------------
|
24
|
+
api :GET, "/pets/:id/as_properties", "Get a pet record"
|
25
|
+
returns :code => 200 do
|
26
|
+
property :pet_name, String, :desc => "Name of pet", :required => false
|
27
|
+
property :animal_type, ['dog','cat','iguana','kangaroo'], :desc => "Type of pet" # required by default, because this is a 'property'
|
28
|
+
end
|
29
|
+
returns :code => 404 do
|
30
|
+
property :another_error_message, String, :desc => "Overriding the response description from the Pets resource"
|
31
|
+
end
|
32
|
+
def show_as_properties
|
33
|
+
render :plain => "showing pet properties"
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
#-----------------------------------------------------------
|
38
|
+
# same example as above, but this time the properties are defined
|
39
|
+
# in a param group
|
40
|
+
#-----------------------------------------------------------
|
41
|
+
def_param_group :pet do
|
42
|
+
property :pet_name, String, :desc => "Name of pet", :required => false
|
43
|
+
property :animal_type, ['dog','cat','iguana','kangaroo'], :desc => "Type of pet" # required by default, because this is a 'property'
|
44
|
+
end
|
45
|
+
|
46
|
+
api :GET, "/pets/:id/as_param_group_of_properties", "Get a pet record"
|
47
|
+
returns :pet, :code => 200, :desc => "The pet"
|
48
|
+
def show_as_param_group_of_properties
|
49
|
+
render :plain => "showing pet properties defined as param groups"
|
50
|
+
end
|
51
|
+
|
52
|
+
#-----------------------------------------------------------
|
53
|
+
# Method returning an array of the :pet param_group
|
54
|
+
#-----------------------------------------------------------
|
55
|
+
api :GET, "/pets", "Get all pets"
|
56
|
+
returns :array_of => :pet, :desc => "list of pets"
|
57
|
+
def index
|
58
|
+
render :plain => "all pets"
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
#-----------------------------------------------------------
|
63
|
+
# mixing request/response and response-only parameters
|
64
|
+
#
|
65
|
+
# the param_group :pet_with_id has several parameters which are
|
66
|
+
# not expectd in the request, but would show up in the response
|
67
|
+
# and vice versa
|
68
|
+
#-----------------------------------------------------------
|
69
|
+
def_param_group :pet_with_id do
|
70
|
+
param :pet_id, Integer, :desc => "id of pet", :required => true
|
71
|
+
param :pet_name, String, :desc => "Name of pet", :required => false, :only_in => :response
|
72
|
+
param :partial_match_allowed, [true, false], :desc => "Partial match allowed?", :required => false, :only_in => :request
|
73
|
+
property :animal_type, ['dog','cat','iguana','kangaroo'], :desc => "Type of pet" # this is implicitly :only_in => :response
|
74
|
+
end
|
75
|
+
|
76
|
+
api :GET, "/pets/pet_by_id", "Get a pet record with the pet id in the body of the request"
|
77
|
+
param_group :pet_with_id # only the pet_id is expected to be in here
|
78
|
+
returns :param_group => :pet_with_id, :code => 200
|
79
|
+
def show_pet_by_id
|
80
|
+
render :plain => "returning a record with 3 fields"
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
#-----------------------------------------------------------
|
86
|
+
# example with multiple param groups
|
87
|
+
#-----------------------------------------------------------
|
88
|
+
def_param_group :owner do # a param group that can be used to describe inputs as well as outputs
|
89
|
+
param :owner_name, String
|
90
|
+
end
|
91
|
+
def_param_group :user_response do # a param group that can be used to describe outputs only
|
92
|
+
property :vote, [true,false]
|
93
|
+
end
|
94
|
+
|
95
|
+
api :GET, "/pets/by_owner_name/did_vote", "did any of the pets owned by the given user vote?"
|
96
|
+
param_group :owner, :desc => "look up the user by name"
|
97
|
+
returns :code => 200 do
|
98
|
+
param_group :owner
|
99
|
+
param_group :user_response
|
100
|
+
end
|
101
|
+
returns :code => 404 # no body
|
102
|
+
def get_vote_by_owner_name
|
103
|
+
render :plain => "no pets have voted"
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
#-----------------------------------------------------------
|
108
|
+
# a method returning multiple codes,
|
109
|
+
# some of which have a complex data structure
|
110
|
+
#-----------------------------------------------------------
|
111
|
+
def_param_group :pet_measurements do
|
112
|
+
property :pet_measurements, Hash do
|
113
|
+
property :weight, Integer, :desc => "Weight in pounds"
|
114
|
+
property :height, Integer, :desc => "Height in inches"
|
115
|
+
property :num_legs, Integer, :desc => "Number of legs", :required => false
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def_param_group :pet_history do
|
120
|
+
property :did_visit_vet, [true,false], :desc => "Did the pet visit the veterinarian"
|
121
|
+
property :avg_meals_per_day, Float, :desc => "Average number of meals per day"
|
122
|
+
end
|
123
|
+
|
124
|
+
api :GET, "/pets/:id/extra_info", "Get extra information about a pet"
|
125
|
+
returns :code => 201, :desc => "Found a pet" do
|
126
|
+
param_group :pet
|
127
|
+
end
|
128
|
+
returns :code => 202 do
|
129
|
+
param_group :pet
|
130
|
+
param_group :pet_measurements
|
131
|
+
end
|
132
|
+
returns :code => 203 do
|
133
|
+
param_group :pet
|
134
|
+
param_group :pet_measurements
|
135
|
+
property 'pet_history', Hash do # the pet_history param_group does not contain a wrapping object,
|
136
|
+
# so create one manually
|
137
|
+
param_group :pet_history
|
138
|
+
end
|
139
|
+
property 'additional_histories', :array_of => Hash do
|
140
|
+
param_group :pet_history
|
141
|
+
end
|
142
|
+
end
|
143
|
+
returns :code => 204 do
|
144
|
+
property :int_array, :array_of => Integer
|
145
|
+
property :enum_array, :array_of => ['v1','v2','v3']
|
146
|
+
end
|
147
|
+
returns :code => :unprocessable_entity, :desc => "Fleas were discovered on the pet" do
|
148
|
+
param_group :pet
|
149
|
+
property :num_fleas, Integer, :desc => "Number of fleas on this pet"
|
150
|
+
end
|
151
|
+
def show_extra_info
|
152
|
+
render :plain => "please disinfect your pet"
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
#=======================================================================
|
157
|
+
# Methods for testing response validation
|
158
|
+
#=======================================================================
|
159
|
+
|
160
|
+
|
161
|
+
#-----------------------------------------------------------
|
162
|
+
# A method which returns the response as described
|
163
|
+
#-----------------------------------------------------------
|
164
|
+
api!
|
165
|
+
returns :code => 200 do
|
166
|
+
property :a_number, Integer
|
167
|
+
property :an_optional_number, Integer, :required=>false
|
168
|
+
end
|
169
|
+
def return_and_validate_expected_response
|
170
|
+
result = {
|
171
|
+
a_number: 3
|
172
|
+
}
|
173
|
+
render :json => result
|
174
|
+
end
|
175
|
+
|
176
|
+
#-----------------------------------------------------------
|
177
|
+
# A method which returns a null value in the response
|
178
|
+
#-----------------------------------------------------------
|
179
|
+
api!
|
180
|
+
returns :code => 200 do
|
181
|
+
property :a_number, Integer
|
182
|
+
property :an_optional_number, Integer, :required=>false
|
183
|
+
end
|
184
|
+
def return_and_validate_expected_response_with_null
|
185
|
+
result = {
|
186
|
+
a_number: nil
|
187
|
+
}
|
188
|
+
render :json => result
|
189
|
+
end
|
190
|
+
|
191
|
+
#-----------------------------------------------------------
|
192
|
+
# A method which returns a null value in the response instead of an object
|
193
|
+
#-----------------------------------------------------------
|
194
|
+
api!
|
195
|
+
returns :code => 200 do
|
196
|
+
property :an_object, Hash do
|
197
|
+
property :an_optional_number, Integer, :required=>false
|
198
|
+
end
|
199
|
+
end
|
200
|
+
def return_and_validate_expected_response_with_null_object
|
201
|
+
result = {
|
202
|
+
an_object: nil
|
203
|
+
}
|
204
|
+
render :json => result
|
205
|
+
end
|
206
|
+
|
207
|
+
#-----------------------------------------------------------
|
208
|
+
# A method which returns an array response as described
|
209
|
+
#-----------------------------------------------------------
|
210
|
+
def_param_group :two_numbers do
|
211
|
+
property :a_number, Integer
|
212
|
+
property :an_optional_number, Integer, :required=>false
|
213
|
+
end
|
214
|
+
|
215
|
+
api!
|
216
|
+
returns :code => 200, :array_of => :two_numbers
|
217
|
+
def return_and_validate_expected_array_response
|
218
|
+
result = [{
|
219
|
+
a_number: 3
|
220
|
+
}]
|
221
|
+
render :json => result
|
222
|
+
end
|
223
|
+
|
224
|
+
#-----------------------------------------------------------
|
225
|
+
# A method which returns an array response when it is expected to return an object
|
226
|
+
# (note that response code is set here to 201)
|
227
|
+
#-----------------------------------------------------------
|
228
|
+
api!
|
229
|
+
returns :two_numbers, :code => 201
|
230
|
+
def return_and_validate_unexpected_array_response
|
231
|
+
result = [{
|
232
|
+
a_number: 3
|
233
|
+
}]
|
234
|
+
render :status => 201, :json => result
|
235
|
+
end
|
236
|
+
|
237
|
+
#-----------------------------------------------------------
|
238
|
+
# A method which has a response that does not match the output type
|
239
|
+
#-----------------------------------------------------------
|
240
|
+
api!
|
241
|
+
returns :code => 200 do
|
242
|
+
property :a_number, String
|
243
|
+
end
|
244
|
+
def return_and_validate_type_mismatch
|
245
|
+
result = {
|
246
|
+
a_number: 3
|
247
|
+
}
|
248
|
+
render :json => result
|
249
|
+
end
|
250
|
+
|
251
|
+
#-----------------------------------------------------------
|
252
|
+
# A method with no documentation
|
253
|
+
#-----------------------------------------------------------
|
254
|
+
def undocumented_method
|
255
|
+
render :json => {:result => "ok"}
|
256
|
+
end
|
257
|
+
|
258
|
+
#-----------------------------------------------------------
|
259
|
+
# A method which has a response with a missing field
|
260
|
+
#-----------------------------------------------------------
|
261
|
+
api!
|
262
|
+
returns :code => 200 do
|
263
|
+
property :a_number, Integer
|
264
|
+
property :another_number, Integer
|
265
|
+
end
|
266
|
+
def return_and_validate_missing_field
|
267
|
+
result = {
|
268
|
+
a_number: 3
|
269
|
+
}
|
270
|
+
render :json => result
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
#-----------------------------------------------------------
|
275
|
+
# A method which has a response with an extra property
|
276
|
+
# (should raise a validation error by default)
|
277
|
+
#-----------------------------------------------------------
|
278
|
+
api!
|
279
|
+
returns :code => 200 do
|
280
|
+
property :a_number, Integer
|
281
|
+
end
|
282
|
+
def return_and_validate_extra_property
|
283
|
+
result = {
|
284
|
+
a_number: 3,
|
285
|
+
another_number: 4
|
286
|
+
}
|
287
|
+
render :json => result
|
288
|
+
end
|
289
|
+
|
290
|
+
|
291
|
+
#-----------------------------------------------------------
|
292
|
+
# A method which has a response with an extra property, but 'additional_properties' is specified
|
293
|
+
# (should not raise a validation error by default)
|
294
|
+
#-----------------------------------------------------------
|
295
|
+
api!
|
296
|
+
returns :code => 200, :additional_properties => true do
|
297
|
+
property :a_number, Integer
|
298
|
+
end
|
299
|
+
def return_and_validate_allowed_extra_property
|
300
|
+
result = {
|
301
|
+
a_number: 3,
|
302
|
+
another_number: 4
|
303
|
+
}
|
304
|
+
render :json => result
|
305
|
+
end
|
306
|
+
|
307
|
+
#-----------------------------------------------------------
|
308
|
+
# Sub-object in response has an extra property. 'additional_properties' is specified on the response, but not on the object
|
309
|
+
# (should raise a validation error by default)
|
310
|
+
#-----------------------------------------------------------
|
311
|
+
api!
|
312
|
+
returns :code => 200, :additional_properties => true do
|
313
|
+
property :an_object, Hash do
|
314
|
+
property :a_number, Integer
|
315
|
+
end
|
316
|
+
end
|
317
|
+
def sub_object_invalid_extra_property
|
318
|
+
result = {
|
319
|
+
an_object: {
|
320
|
+
a_number: 2,
|
321
|
+
an_extra_number: 3
|
322
|
+
}
|
323
|
+
}
|
324
|
+
render :json => result
|
325
|
+
end
|
326
|
+
|
327
|
+
|
328
|
+
#-----------------------------------------------------------
|
329
|
+
# Sub-object in response has an extra property. 'additional_properties' is specified on the object, but not on the response
|
330
|
+
# (should not raise a validation error by default)
|
331
|
+
#-----------------------------------------------------------
|
332
|
+
api!
|
333
|
+
returns :code => 200, :additional_properties => false do
|
334
|
+
property :an_object, Hash, :additional_properties => true do
|
335
|
+
property :a_number, Integer
|
336
|
+
end
|
337
|
+
end
|
338
|
+
def sub_object_allowed_extra_property
|
339
|
+
result = {
|
340
|
+
an_object: {
|
341
|
+
a_number: 2,
|
342
|
+
an_extra_number: 3
|
343
|
+
}
|
344
|
+
}
|
345
|
+
render :json => result
|
346
|
+
end
|
347
|
+
|
348
|
+
#=======================================================================
|
349
|
+
# Methods for testing array field responses
|
350
|
+
#=======================================================================
|
351
|
+
|
352
|
+
#-----------------------------------------------------------
|
353
|
+
# A method which returns the response as described (one field is an array of objects)
|
354
|
+
#-----------------------------------------------------------
|
355
|
+
api!
|
356
|
+
returns :code => 200 do
|
357
|
+
property :a_number, Integer
|
358
|
+
property :array_of_objects, :array_of => Hash do
|
359
|
+
property :number1, Integer, :required=>true
|
360
|
+
property :number2, Integer, :required=>true
|
361
|
+
end
|
362
|
+
end
|
363
|
+
def returns_response_with_valid_array
|
364
|
+
result = {
|
365
|
+
a_number: 3,
|
366
|
+
array_of_objects: [
|
367
|
+
{number1: 1, number2: 2},
|
368
|
+
{number1: 10, number2: 20}
|
369
|
+
]
|
370
|
+
}
|
371
|
+
render :json => result
|
372
|
+
end
|
373
|
+
|
374
|
+
|
375
|
+
#-----------------------------------------------------------
|
376
|
+
# A method which returns an incorrect response (wrong field type inside array)
|
377
|
+
#-----------------------------------------------------------
|
378
|
+
api!
|
379
|
+
returns :code => 200 do
|
380
|
+
property :a_number, Integer
|
381
|
+
property :array_of_objects, :array_of => Hash do
|
382
|
+
property :number1, Integer, :required=>true
|
383
|
+
property :number2, Integer, :required=>true
|
384
|
+
end
|
385
|
+
end
|
386
|
+
def returns_response_with_invalid_array
|
387
|
+
result = {
|
388
|
+
a_number: 3,
|
389
|
+
array_of_objects: [
|
390
|
+
{number1: 1, number2: 2},
|
391
|
+
{number1: 10, number2: "this should have been a number"}
|
392
|
+
]
|
393
|
+
}
|
394
|
+
render :json => result
|
395
|
+
end
|
396
|
+
|
397
|
+
|
398
|
+
#-----------------------------------------------------------
|
399
|
+
# A method with simple tags.
|
400
|
+
#-----------------------------------------------------------
|
401
|
+
api!
|
402
|
+
tags(%w[Dogs Cats LivingBeings])
|
403
|
+
def show_plain_response_with_tags
|
404
|
+
render :plain => "showing pet properties"
|
405
|
+
end
|
406
|
+
|
407
|
+
end
|
408
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
#
|
2
|
+
# demonstration of how the 'describe_own_properties' method can be used
|
3
|
+
# to integrate Apipie response descriptions with view generation packages
|
4
|
+
# such as, for example, Grape::Entity
|
5
|
+
#
|
6
|
+
|
7
|
+
# Consider a hypothetical SelfDocumentingView class (a stand-in for Grape::Entity
|
8
|
+
# for demonstrational purposes). This is an abstract class, supporting the implementation
|
9
|
+
# of actual views as subclasses of SelfDocumentingView.
|
10
|
+
#
|
11
|
+
# A user of SelfDocumentingView would implement a subclass for each desired view. Each such
|
12
|
+
# view definition would include, for each field that should be returned in the JSON response,
|
13
|
+
# an instance method called v_<name>__<type>.
|
14
|
+
#
|
15
|
+
# SelfDocumentingView would then scan the subclass for such fields and:
|
16
|
+
# 1. Given an id: fetch the matching model and generated a view from it using the field definitions
|
17
|
+
# 2. Describe the structure of such views to Apipie using self.describe_own_properties
|
18
|
+
#
|
19
|
+
# (see the controller implementation below for how such a class might be used)
|
20
|
+
|
21
|
+
class SelfDocumentingView
|
22
|
+
# self.describe_own_properties (a class method) generates the meta-data
|
23
|
+
# (i.e., the type description) for the subclass.
|
24
|
+
def self.describe_own_properties
|
25
|
+
(self.instance_methods - self.class.instance_methods).map{|m|
|
26
|
+
if matchdata = /^v_(\w+)__(\w+)$/.match(m)
|
27
|
+
Apipie::prop(matchdata[1], matchdata[2])
|
28
|
+
end
|
29
|
+
}.compact
|
30
|
+
end
|
31
|
+
|
32
|
+
# to_json (an instance method) generates the actual view
|
33
|
+
def to_json
|
34
|
+
{ note: "in an actual implementation of SelfDocumentingView, this method
|
35
|
+
would call each v_<name>__<type> method and include its output
|
36
|
+
in the response as a (<name>: <value>) pair"
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(id)
|
41
|
+
load_from_model(id)
|
42
|
+
end
|
43
|
+
|
44
|
+
def load_from_model(id)
|
45
|
+
# in a real implementation of SelfDocumentingView, this
|
46
|
+
# method would load the fields to be displayed from the model
|
47
|
+
# instance identified by 'id'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
#
|
53
|
+
# ViewOfPet extends SelfDocumentingView to include specific fields
|
54
|
+
#
|
55
|
+
class ViewOfPet < SelfDocumentingView
|
56
|
+
attr_accessor :v_pet_name__string
|
57
|
+
attr_accessor :v_animal_type__string
|
58
|
+
attr_accessor :v_age__number
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
class PetsUsingAutoViewsController < ApplicationController
|
63
|
+
#-----------------------------------------------------------
|
64
|
+
# Method returning an array of AutomatedViewOfPet (where
|
65
|
+
# AutomatedViewOfPet is an auto-generated self-describing class)
|
66
|
+
# -----------------------------------------------------------
|
67
|
+
api :GET, "/pet_described_using_automated_view/:id", "Get the measurements of a single pet"
|
68
|
+
param :id, String
|
69
|
+
returns ViewOfPet, :desc => "like Pet, but different"
|
70
|
+
def pet_described_using_automated_view
|
71
|
+
render :plain => ViewOfPet.new(params.id).to_json
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
#----------------------------------------------------------------------------------------------------
|
2
|
+
# A "self-describing class" is a class that respond_to? :describe_own_properties
|
3
|
+
# and returns an array of Property Descriptions.
|
4
|
+
# (The simple way to create Property Description objects is using the Apipie::prop helper function,
|
5
|
+
# which is a factory for Apipie::ResponseDescriptionAdapter::PropDesc instances)
|
6
|
+
#
|
7
|
+
#----------------------------------------------------------------------------------------------------
|
8
|
+
|
9
|
+
|
10
|
+
# in this example, Pet is a self-describing class with only two properties.
|
11
|
+
# In a real implementation, the Pet class would actually do something with these properties.
|
12
|
+
# Here, the class is defined to only include the describe_own_properties method.
|
13
|
+
class Pet
|
14
|
+
def self.describe_own_properties
|
15
|
+
[
|
16
|
+
Apipie::prop(:pet_name, 'string', {:description => 'Name of pet', :required => false}),
|
17
|
+
Apipie::prop(:animal_type, 'string', {:description => 'Type of pet', :values => ["dog", "cat", "iguana", "kangaroo"]}),
|
18
|
+
Apipie::additional_properties(false)
|
19
|
+
]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# PetWithMeasurements is a self-describing class with an embedded object
|
25
|
+
#
|
26
|
+
class PetWithMeasurements
|
27
|
+
def self.describe_own_properties
|
28
|
+
[
|
29
|
+
Apipie::prop(:pet_name, 'string', {:description => 'Name of pet', :required => false}),
|
30
|
+
Apipie::prop('animal_type', 'string', {:description => 'Type of pet', :values => ["dog", "cat", "iguana", "kangaroo"]}),
|
31
|
+
Apipie::prop(:pet_measurements, 'object', {}, [
|
32
|
+
Apipie::prop(:weight, 'number', {:description => "Weight in pounds" }),
|
33
|
+
Apipie::prop(:height, 'number', {:description => "Height in inches" }),
|
34
|
+
Apipie::prop(:num_legs, 'number', {:description => "Number of legs", :required => false }),
|
35
|
+
Apipie::additional_properties(false)
|
36
|
+
])
|
37
|
+
]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# PetWithManyMeasurements is a self-describing class with an embedded object
|
43
|
+
#
|
44
|
+
class PetWithManyMeasurements
|
45
|
+
def self.describe_own_properties
|
46
|
+
[
|
47
|
+
Apipie::prop(:pet_name, 'string', {:description => 'Name of pet', :required => false}),
|
48
|
+
Apipie::prop(:many_pet_measurements, 'object', {is_array: true}, [
|
49
|
+
Apipie::prop(:weight, 'number', {:description => "Weight in pounds" }),
|
50
|
+
Apipie::prop(:height, 'number', {:description => "Height in inches" }),
|
51
|
+
])
|
52
|
+
]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
class PetsUsingSelfDescribingClassesController < ApplicationController
|
59
|
+
resource_description do
|
60
|
+
description 'A controller to test "returns" using self-describing classes'
|
61
|
+
short 'Pets'
|
62
|
+
path '/pets2'
|
63
|
+
end
|
64
|
+
|
65
|
+
#-----------------------------------------------------------
|
66
|
+
# Method returning an array of Pet (a self-describing class)
|
67
|
+
# -----------------------------------------------------------
|
68
|
+
api :GET, "/pets_described_as_class", "Get all pets"
|
69
|
+
returns :array_of => Pet, :desc => "list of pets"
|
70
|
+
def pets_described_as_class
|
71
|
+
render :plain => "all pets"
|
72
|
+
end
|
73
|
+
|
74
|
+
#-----------------------------------------------------------
|
75
|
+
# Method returning an array of PetWithMeasurements (a self-describing class)
|
76
|
+
# -----------------------------------------------------------
|
77
|
+
api :GET, "/pets_with_measurements_described_as_class/:id", "Get the measurements of a single pet"
|
78
|
+
param :id, String
|
79
|
+
returns PetWithMeasurements, :desc => "measurements of the pet"
|
80
|
+
def pets_with_measurements_described_as_class
|
81
|
+
render :plain => "all pets"
|
82
|
+
end
|
83
|
+
|
84
|
+
#-----------------------------------------------------------
|
85
|
+
# Method returning an array of PetWithManyMeasurements (a self-describing class with array field)
|
86
|
+
# -----------------------------------------------------------
|
87
|
+
api :GET, "/pets_with_many_measurements_as_class/:id", "Get the measurements of a single pet"
|
88
|
+
param :id, String
|
89
|
+
returns PetWithManyMeasurements, :desc => "measurements of the pet"
|
90
|
+
def pets_with_many_measurements_as_class
|
91
|
+
render :plain => "all pets"
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#
|
2
|
+
# The TagsController defined here provides an example of a resource_description
|
3
|
+
# defining a set of tags for the contained methods to include.
|
4
|
+
#
|
5
|
+
|
6
|
+
class TaggedCatsController < ActionController::Base
|
7
|
+
resource_description do
|
8
|
+
description 'A controller to test "returns"'
|
9
|
+
short 'Pets'
|
10
|
+
path '/pets'
|
11
|
+
|
12
|
+
tags(%w[Dogs Pets])
|
13
|
+
end
|
14
|
+
|
15
|
+
#-----------------------------------------------------------
|
16
|
+
# simple 'returns' example: a method that returns a cat record
|
17
|
+
#-----------------------------------------------------------
|
18
|
+
api :GET, "/pets/:id/as_properties", "Get a cat record"
|
19
|
+
tags(%w[Animals])
|
20
|
+
def show_as_properties
|
21
|
+
render :plain => "showing pet properties"
|
22
|
+
end
|
23
|
+
|
24
|
+
#-----------------------------------------------------------
|
25
|
+
# simple 'returns' example: a method that returns another cat record
|
26
|
+
#-----------------------------------------------------------
|
27
|
+
api :GET, "/pets/:id/as_same_properties", "Get a cat record"
|
28
|
+
tags("Puma", "Animals")
|
29
|
+
def show_as_same_properties
|
30
|
+
render :plain => "showing pet properties"
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#
|
2
|
+
# The TagsController defined here provides an example of a
|
3
|
+
# tags call without a resource description.
|
4
|
+
#
|
5
|
+
|
6
|
+
class TaggedDogsController < ActionController::Base
|
7
|
+
#-----------------------------------------------------------
|
8
|
+
# simple 'returns' example: a method that returns a cat record
|
9
|
+
#-----------------------------------------------------------
|
10
|
+
api :GET, "/pets/:id/as_properties", "Get a dog record"
|
11
|
+
tags(%w[Dogs Wolves])
|
12
|
+
def show_as_properties
|
13
|
+
render :plain => "showing pet properties"
|
14
|
+
end
|
15
|
+
end
|
@@ -299,4 +299,9 @@ class TwitterExampleController < ApplicationController
|
|
299
299
|
render :text => 'twitter example'
|
300
300
|
end
|
301
301
|
|
302
|
+
api :GET, '/twitter_example/{id}/followers', 'Find the followers for the given screen name'
|
303
|
+
tags %w[following index search]
|
304
|
+
def followers
|
305
|
+
render :text => 'followers'
|
306
|
+
end
|
302
307
|
end
|