apipie-rails 0.2.6 → 0.3.0
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.
- data/CHANGELOG.md +33 -1
- data/README.rst +210 -137
- data/app/views/apipie/apipies/_disqus.html.erb +1 -1
- data/app/views/apipie/apipies/_params.html.erb +1 -1
- data/app/views/apipie/apipies/_params_plain.html.erb +1 -1
- data/lib/apipie/application.rb +44 -1
- data/lib/apipie/configuration.rb +22 -3
- data/lib/apipie/dsl_definition.rb +90 -47
- data/lib/apipie/errors.rb +6 -0
- data/lib/apipie/extractor.rb +1 -1
- data/lib/apipie/extractor/recorder.rb +35 -0
- data/lib/apipie/extractor/writer.rb +31 -9
- data/lib/apipie/method_description.rb +9 -3
- data/lib/apipie/routes_formatter.rb +33 -0
- data/lib/apipie/validator.rb +8 -0
- data/lib/apipie/version.rb +1 -1
- data/lib/tasks/apipie.rake +5 -3
- data/spec/controllers/users_controller_spec.rb +261 -193
- data/spec/dummy/app/controllers/application_controller.rb +8 -0
- data/spec/dummy/app/controllers/files_controller.rb +5 -0
- data/spec/dummy/app/controllers/users_controller.rb +10 -0
- data/spec/dummy/config/initializers/apipie.rb +1 -2
- data/spec/dummy/config/routes.rb +8 -1
- data/spec/lib/extractor/extractor_spec.rb +9 -0
- data/spec/lib/extractor/middleware_spec.rb +23 -0
- metadata +7 -2
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,32 @@
|
|
2
2
|
Changelog
|
3
3
|
===========
|
4
4
|
|
5
|
+
v0.3.0
|
6
|
+
------
|
7
|
+
This should be a backward compatible release. However, the number of new
|
8
|
+
significant features deserves new minor version bump.
|
9
|
+
|
10
|
+
* Rubocop-blessed Ruby 1.9 syntax for generated DSL documentation
|
11
|
+
[#318](https://github.com/Apipie/apipie-rails/pull/318) [@burnettk][]
|
12
|
+
* load API paths from routes.rb
|
13
|
+
[#187](https://github.com/Apipie/apipie-rails/pull/187) [@mtparet][] [@iNecas][]
|
14
|
+
* ability to use before_filter instead of overriding the action method
|
15
|
+
for validation
|
16
|
+
[#306](https://github.com/Apipie/apipie-rails/pull/306) [@dprice-fiksu][]
|
17
|
+
* support multi-part data when recording from tests
|
18
|
+
[#310](https://github.com/Apipie/apipie-rails/pull/310) [@bradrf][]
|
19
|
+
* validate_keys option to raise exception when passing undocumented option
|
20
|
+
[#122](https://github.com/Apipie/apipie-rails/pull/122) [@dfharmon][]
|
21
|
+
* handle static page generation when the doc_base_url has multiple folders
|
22
|
+
[#300](https://github.com/Apipie/apipie-rails/pull/300) [@ryanische][]
|
23
|
+
* add ability to markup validator description
|
24
|
+
[#282](https://github.com/Apipie/apipie-rails/pull/282) [@exAspArk][]
|
25
|
+
* don't specify protocol in Disqus script tag src
|
26
|
+
[#285](https://github.com/Apipie/apipie-rails/pull/285) [@chrise86][]
|
27
|
+
* fix BooleanValidator to set expected_type as boolean
|
28
|
+
[#286](https://github.com/Apipie/apipie-rails/pull/286) [@dustin-rh][]
|
29
|
+
|
30
|
+
|
5
31
|
v0.2.6
|
6
32
|
------
|
7
33
|
|
@@ -217,5 +243,11 @@ v0.0.15
|
|
217
243
|
[@exAspArk]: https://github.com/exAspArk
|
218
244
|
[@mourad-ifeelgoods]: https://github.com/mourad-ifeelgoods
|
219
245
|
[@domcleal]: https://github.com/domcleal
|
220
|
-
[@
|
246
|
+
[@dustin-rh]: https://github.com/dustin-rh
|
221
247
|
[@RajRoR]: https://github.com/RajRoR
|
248
|
+
[@chrise86]: https://github.com/chrise86
|
249
|
+
[@ryanische]: https://github.com/ryanische
|
250
|
+
[@dfharmon]: https://github.com/dfharmon
|
251
|
+
[@bradrf]: https://github.com/bradrf
|
252
|
+
[@dprice-fiksu]: https://github.com/dprice-fiksu
|
253
|
+
[@burnettk]: https://github.com/burnettk
|
data/README.rst
CHANGED
@@ -7,20 +7,20 @@
|
|
7
7
|
.. image:: https://codeclimate.com/github/Apipie/apipie-rails.png
|
8
8
|
:target: https://codeclimate.com/github/Apipie/apipie-rails
|
9
9
|
|
10
|
-
Apipie-rails is a DSL and Rails engine for documenting
|
10
|
+
Apipie-rails is a DSL and Rails engine for documenting your RESTful
|
11
11
|
API. Instead of traditional use of ``#comments``, Apipie lets you
|
12
|
-
describe the code
|
12
|
+
describe the code, through the code. This brings advantages like:
|
13
13
|
|
14
|
-
*
|
15
|
-
*
|
16
|
-
*
|
17
|
-
*
|
14
|
+
* No need to learn yet another syntax, you already know Ruby, right?
|
15
|
+
* Possibility of reusing the docs for other purposes (such as validation)
|
16
|
+
* Easier to extend and maintain (no string parsing involved)
|
17
|
+
* Possibility of reusing other sources for documentation purposes (such as
|
18
18
|
routes etc.)
|
19
19
|
|
20
|
-
The documentation is available
|
21
|
-
``/apipie`` path. In development mode, you can see the changes as you
|
22
|
-
go. It's markup language agnostic and even provides an API for reusing
|
23
|
-
the documentation data in
|
20
|
+
The documentation is available from within your app (by default under the
|
21
|
+
``/apipie`` path.) In development mode, you can see the changes as you
|
22
|
+
go. It's markup language agnostic, and even provides an API for reusing
|
23
|
+
the documentation data in JSON.
|
24
24
|
|
25
25
|
Getting started
|
26
26
|
---------------
|
@@ -46,12 +46,12 @@ Now you can start documenting your resources and actions (see
|
|
46
46
|
|
47
47
|
|
48
48
|
Run your application and see the result at
|
49
|
-
``http://localhost:3000/apipie``. For
|
49
|
+
``http://localhost:3000/apipie``. For further processing, you can
|
50
50
|
use ``http://localhost:3000/apipie.json``.
|
51
51
|
|
52
|
-
For more comprehensive getting started guide, see
|
53
|
-
`this demo <https://github.com/iNecas/apipie-demo>`_,
|
54
|
-
features such as generating
|
52
|
+
For a more comprehensive getting started guide, see
|
53
|
+
`this demo <https://github.com/iNecas/apipie-demo>`_, which includes
|
54
|
+
features such as generating documentation from tests, recording examples etc.
|
55
55
|
|
56
56
|
Screenshots
|
57
57
|
-----------
|
@@ -88,7 +88,7 @@ Apipie-rails is released under the `MIT License <http://opensource.org/licenses/
|
|
88
88
|
Resource Description
|
89
89
|
--------------------
|
90
90
|
|
91
|
-
You can describe a resource on controller level. The description is introduced by calling
|
91
|
+
You can describe a resource on the controller level. The description is introduced by calling
|
92
92
|
``resource_description do ... end``.
|
93
93
|
|
94
94
|
Inheritance is supported, so you can specify common params for group of controllers in their parent
|
@@ -97,13 +97,13 @@ class.
|
|
97
97
|
The following keywords are available (all are optional):
|
98
98
|
|
99
99
|
resource_id
|
100
|
-
How
|
100
|
+
How the resource will be referenced in Apipie (paths, ``see`` command etc.); by default `controller_name.downcase` is used.
|
101
101
|
|
102
102
|
name
|
103
103
|
Human readable name of resource. By default ``class.name.humanize`` is used.
|
104
104
|
|
105
105
|
short (also short_description)
|
106
|
-
Short description of the resource (it's shown on both list of resources and resource details)
|
106
|
+
Short description of the resource (it's shown on both the list of resources, and resource details)
|
107
107
|
|
108
108
|
desc (also description and full_description)
|
109
109
|
Full description of the resource (shown only in resource details)
|
@@ -112,20 +112,20 @@ param
|
|
112
112
|
Common params for all methods defined in controller/child controllers.
|
113
113
|
|
114
114
|
api_base_url
|
115
|
-
What
|
115
|
+
What URL is the resource available under.
|
116
116
|
|
117
117
|
api_versions (also api_version)
|
118
118
|
What versions does the controller define the resource. (See `Versioning`_ for details.)
|
119
119
|
|
120
120
|
formats
|
121
|
-
|
121
|
+
Request / response formats.
|
122
122
|
|
123
123
|
error
|
124
|
-
Describe every possible error that can happen
|
124
|
+
Describe every possible error that can happen when calling all
|
125
125
|
methods defined in controller. HTTP response code and description can be provided.
|
126
126
|
|
127
127
|
app_info
|
128
|
-
In case of versioning, this sets app info description on per_version basis.
|
128
|
+
In case of versioning, this sets app info description on a per_version basis.
|
129
129
|
|
130
130
|
meta
|
131
131
|
Hash or array with custom metadata.
|
@@ -180,13 +180,22 @@ Method Description
|
|
180
180
|
Then describe methods available to your API.
|
181
181
|
|
182
182
|
api
|
183
|
-
|
183
|
+
Describe how this method is exposed, and provide a short description.
|
184
184
|
The first parameter is HTTP method (one of :GET/:POST/:PUT/:DELETE).
|
185
|
-
The second parameter is relative URL path which is mapped to this
|
186
|
-
method. The last parameter is methods short description.
|
187
|
-
You can use this +api+ method more than once
|
185
|
+
The second parameter is the relative URL path which is mapped to this
|
186
|
+
method. The last parameter is the methods short description.
|
187
|
+
You can use this +api+ method more than once per method. It could
|
188
188
|
be useful when there are more routes mapped to it.
|
189
189
|
|
190
|
+
When providing just one argument (description), or no argument at all,
|
191
|
+
the paths will be loaded from the routes.rb file.
|
192
|
+
|
193
|
+
api!
|
194
|
+
Provide a short description and additional option.
|
195
|
+
The last parameter is the methods short description.
|
196
|
+
The paths will be loaded from routes.rb file. See
|
197
|
+
`Rails Routes Integration`_ for more details.
|
198
|
+
|
190
199
|
api_versions (also api_version)
|
191
200
|
What version(s) does the action belong to. (See `Versioning`_ for details.)
|
192
201
|
|
@@ -197,19 +206,19 @@ formats
|
|
197
206
|
Method level request / response formats.
|
198
207
|
|
199
208
|
error
|
200
|
-
Describe each possible error that can happen
|
209
|
+
Describe each possible error that can happen while calling this
|
201
210
|
method. HTTP response code and description can be provided.
|
202
211
|
|
203
212
|
description
|
204
|
-
Full method description which will be converted
|
213
|
+
Full method description, which will be converted into HTML by the
|
205
214
|
chosen markup language processor.
|
206
215
|
|
207
216
|
example
|
208
|
-
Provide example of server response
|
217
|
+
Provide an example of the server response; whole communication or response type.
|
209
218
|
It will be formatted as code.
|
210
219
|
|
211
220
|
see
|
212
|
-
Provide reference to another method, this has to be string with
|
221
|
+
Provide reference to another method, this has to be a string with
|
213
222
|
controller_name#method_name.
|
214
223
|
|
215
224
|
meta
|
@@ -220,6 +229,12 @@ Example:
|
|
220
229
|
|
221
230
|
.. code:: ruby
|
222
231
|
|
232
|
+
# The simplest case: just load the paths from routes.rb
|
233
|
+
api!
|
234
|
+
def index
|
235
|
+
end
|
236
|
+
|
237
|
+
# More complex example
|
223
238
|
api :GET, "/users/:id", "Show user profile"
|
224
239
|
error :code => 401, :desc => "Unauthorized"
|
225
240
|
error :code => 404, :desc => "Not Found", :meta => {:anything => "you can think of"}
|
@@ -245,14 +260,14 @@ Example:
|
|
245
260
|
Parameter Description
|
246
261
|
---------------------
|
247
262
|
|
248
|
-
Use ``param`` to describe every possible parameter. You can use Hash validator
|
249
|
-
in
|
263
|
+
Use ``param`` to describe every possible parameter. You can use the Hash validator
|
264
|
+
in conjunction with a block given to the param method to describe nested parameters.
|
250
265
|
|
251
266
|
name
|
252
|
-
The first argument is parameter name as a symbol.
|
267
|
+
The first argument is the parameter name as a symbol.
|
253
268
|
|
254
269
|
validator
|
255
|
-
Second parameter is parameter validator, choose one from section `Validators`_
|
270
|
+
Second parameter is the parameter validator, choose one from section `Validators`_
|
256
271
|
|
257
272
|
desc
|
258
273
|
Parameter description.
|
@@ -261,10 +276,10 @@ required
|
|
261
276
|
Set this true/false to make it required/optional. Default is optional
|
262
277
|
|
263
278
|
allow_nil
|
264
|
-
|
279
|
+
Setting this to true means that ``nil`` can be passed.
|
265
280
|
|
266
281
|
as
|
267
|
-
|
282
|
+
Used by the processing functionality to change the name of a key params.
|
268
283
|
|
269
284
|
meta
|
270
285
|
Hash or array with custom metadata.
|
@@ -291,10 +306,9 @@ DRY with param_group
|
|
291
306
|
--------------------
|
292
307
|
|
293
308
|
Often, params occur together in more actions. Typically, most of the
|
294
|
-
params for ``create`` and ``update`` actions are
|
295
|
-
them.
|
309
|
+
params for ``create`` and ``update`` actions are shared between them.
|
296
310
|
|
297
|
-
|
311
|
+
These params can be extracted with ``def_param_group`` and
|
298
312
|
``param_group`` keywords.
|
299
313
|
|
300
314
|
The definition is looked up in the scope of the controller. If the
|
@@ -342,7 +356,7 @@ Example:
|
|
342
356
|
Action Aware params
|
343
357
|
-------------------
|
344
358
|
|
345
|
-
In CRUD operations, this pattern occurs quite often
|
359
|
+
In CRUD operations, this pattern occurs quite often - params that need
|
346
360
|
to be set are:
|
347
361
|
|
348
362
|
* for create action: ``required => true`` and ``allow_nil => false``
|
@@ -389,7 +403,7 @@ In this case, ``user[name]`` will be not be allowed nil for all
|
|
389
403
|
actions and required only for ``create`` and ``create_admin``. Params
|
390
404
|
with ``allow_nil`` set explicitly don't have this value changed.
|
391
405
|
|
392
|
-
Action awareness is
|
406
|
+
Action awareness is inherited from ancestors (in terms of
|
393
407
|
nested params).
|
394
408
|
|
395
409
|
Concerns
|
@@ -399,12 +413,12 @@ Sometimes, the actions are not defined in the controller class
|
|
399
413
|
directly but included from a module instead. You can load the Apipie
|
400
414
|
DSL into the module by extending it with ``Apipie::DSL::Concern``.
|
401
415
|
|
402
|
-
The module can be used in more controllers. Therefore there is a way
|
403
|
-
|
404
|
-
specific values.
|
416
|
+
The module can be used in more controllers. Therefore there is a way to
|
417
|
+
substitute parts of the documentation in the module with controller
|
418
|
+
specific values. These substitutions can be stated explicitly with
|
405
419
|
``apipie_concern_subst(:key => "value")`` (needs to be called before
|
406
420
|
the module is included to take effect). The substitutions are
|
407
|
-
performed in paths and descriptions of APIs and names and descriptions
|
421
|
+
performed in the paths and descriptions of APIs and names and descriptions
|
408
422
|
of params.
|
409
423
|
|
410
424
|
There are some default substitutions available:
|
@@ -476,13 +490,13 @@ Example
|
|
476
490
|
Configuration Reference
|
477
491
|
=========================
|
478
492
|
|
479
|
-
Create configuration file in e.g. ``/config/initializers/apipie.rb``.
|
480
|
-
You can set
|
493
|
+
Create a configuration file in e.g. ``/config/initializers/apipie.rb``.
|
494
|
+
You can set the application name, footer text, API and documentation base URL
|
481
495
|
and turn off validations. You can also choose your favorite markup language
|
482
|
-
|
496
|
+
for full descriptions.
|
483
497
|
|
484
498
|
app_name
|
485
|
-
Name of your application used in breadcrumbs navigation.
|
499
|
+
Name of your application; used in breadcrumbs navigation.
|
486
500
|
|
487
501
|
copyright
|
488
502
|
Copyright information (shown in page footer).
|
@@ -497,7 +511,12 @@ default_version
|
|
497
511
|
Default API version to be used (1.0 by default)
|
498
512
|
|
499
513
|
validate
|
500
|
-
Parameters validation is turned off when set to false.
|
514
|
+
Parameters validation is turned off when set to false. When set to
|
515
|
+
``:explicitly``, you must invoke parameter validation yourself by calling
|
516
|
+
controller method ``apipie_validations`` (typically in a before_filter).
|
517
|
+
When set to ``:implicitly`` (or just true), your controller's action
|
518
|
+
methods are wrapped with generated methods which call ``apipie_validations``,
|
519
|
+
and then call the action method. (``:implicitly`` by default)
|
501
520
|
|
502
521
|
validate_value
|
503
522
|
Check the value of params against specified validators (true by
|
@@ -506,21 +525,30 @@ validate_value
|
|
506
525
|
validate_presence
|
507
526
|
Check the params presence against the documentation.
|
508
527
|
|
528
|
+
validate_keys
|
529
|
+
Check the received params to ensure they are defined in the API. (false by default)
|
530
|
+
|
509
531
|
process_params
|
510
|
-
Process and extract parameter defined from the params of the request
|
532
|
+
Process and extract the parameter defined from the params of the request
|
511
533
|
to the api_params variable
|
512
534
|
|
513
535
|
app_info
|
514
536
|
Application long description.
|
515
537
|
|
516
538
|
reload_controllers
|
517
|
-
Set to enable/disable reloading controllers (and the documentation with it)
|
539
|
+
Set to enable/disable reloading controllers (and the documentation with it). Enabled by default in development.
|
518
540
|
|
519
541
|
api_controllers_matcher
|
520
542
|
For reloading to work properly you need to specify where your API controllers are. Can be an array if multiple paths are needed
|
521
543
|
|
522
544
|
api_routes
|
523
|
-
Set if your application uses custom API router, different from Rails
|
545
|
+
Set if your application uses a custom API router, different from the Rails
|
546
|
+
default
|
547
|
+
|
548
|
+
routes_formatter
|
549
|
+
An object providing the translation from the Rails routes to the
|
550
|
+
format usable in the documentation when using the `api!` keyword. By
|
551
|
+
default, the ``Apipie::RoutesFormatter`` is used.
|
524
552
|
|
525
553
|
markup
|
526
554
|
You can choose markup language for descriptions of your application,
|
@@ -553,11 +581,11 @@ show_all_examples
|
|
553
581
|
Set this to true to set show_in_doc=1 in all recorded examples
|
554
582
|
|
555
583
|
link_extension
|
556
|
-
The extension to use for API pages ('.html' by default).
|
584
|
+
The extension to use for API pages ('.html' by default). Link extensions
|
557
585
|
in static API docs cannot be changed from '.html'.
|
558
586
|
|
559
587
|
languages
|
560
|
-
List of languages API documentation should be translated into. Empty
|
588
|
+
List of languages the API documentation should be translated into. Empty by default.
|
561
589
|
|
562
590
|
default_locale
|
563
591
|
Locale used for generating documentation when no specific locale is set.
|
@@ -571,7 +599,7 @@ locale
|
|
571
599
|
config.locale = lambda { |loc| loc ? FastGettext.set_locale(loc) : FastGettext.locale }
|
572
600
|
|
573
601
|
translate
|
574
|
-
Pass proc to translate strings using localization library your project uses.
|
602
|
+
Pass proc to translate strings using the localization library your project uses.
|
575
603
|
For example see `Localization`_
|
576
604
|
|
577
605
|
Example:
|
@@ -605,21 +633,51 @@ checksum_path
|
|
605
633
|
update_checksum
|
606
634
|
If set to true, the checksum is recalculated with every documentation_reload call
|
607
635
|
|
636
|
+
========================
|
637
|
+
Rails Routes Integration
|
638
|
+
========================
|
639
|
+
|
640
|
+
Apipie is able to load the information about the paths based on the
|
641
|
+
routes defined in the Rails application, by using the `api!` keyword
|
642
|
+
in the DSL.
|
643
|
+
|
644
|
+
It should be usable out of box, however, one might want
|
645
|
+
to do some customization (such as omitting some implicit parameters in
|
646
|
+
the path etc.). For this kind of customizations one can create a new
|
647
|
+
formatter and pass as the ``Apipie.configuration.routes_formatter``
|
648
|
+
option, like this:
|
649
|
+
|
650
|
+
.. code:: ruby
|
651
|
+
|
652
|
+
class MyFormatter < Apipie::RailsFormatter
|
653
|
+
def format_path(route)
|
654
|
+
super.gsub(/\(.*?\)/, '').gsub('//','') # hide all implicit parameters
|
655
|
+
end
|
656
|
+
end
|
657
|
+
|
658
|
+
Apipie.configure do |config|
|
659
|
+
...
|
660
|
+
config.routes_formatter = MyFormatter.new
|
661
|
+
...
|
662
|
+
end
|
663
|
+
|
664
|
+
A similar way can be used to influence things like order, or a description
|
665
|
+
of the loaded APIs, even omitting some paths if needed.
|
666
|
+
|
608
667
|
============
|
609
668
|
Processing
|
610
669
|
============
|
611
670
|
|
612
|
-
The goal is to extract and pre
|
671
|
+
The goal is to extract and pre-process parameters of the request.
|
613
672
|
|
614
|
-
For example Rails, by default, transforms empty array to nil value
|
615
|
-
you want
|
673
|
+
For example Rails, by default, transforms an empty array to nil value. Perhaps
|
674
|
+
you want to transform it again into an empty array. Or you
|
616
675
|
want to support an enumeration type (comma separated values) and
|
617
|
-
you want automatically transform this string
|
676
|
+
you want to automatically transform this string into an array.
|
618
677
|
|
619
|
-
To use it, set
|
620
|
-
In your action, use api_params variable instead of params.
|
678
|
+
To use it, set the ``process_params`` configuration variable to true.
|
621
679
|
|
622
|
-
Also by using
|
680
|
+
Also by using ``as`` you can separate your API parameter
|
623
681
|
names from the names you are using inside your code.
|
624
682
|
|
625
683
|
To implement it, you just have to write a process_value
|
@@ -637,21 +695,32 @@ For an enumeration type:
|
|
637
695
|
Validators
|
638
696
|
============
|
639
697
|
|
640
|
-
Every parameter needs to have associated validator. For now there are some
|
641
|
-
basic validators. You can always provide your own to
|
698
|
+
Every parameter needs to have an associated validator. For now there are some
|
699
|
+
basic validators. You can always provide your own to achieve complex
|
642
700
|
results.
|
643
701
|
|
644
702
|
If validations are enabled (default state) the parameters of every
|
645
|
-
request are validated. If the value is wrong
|
646
|
-
is raised and can be rescued and processed. It contains
|
647
|
-
of parameter value expectations. Validations can be turned off
|
648
|
-
in configuration file.
|
703
|
+
request are validated. If the value is wrong an +ArgumentError+ exception
|
704
|
+
is raised and can be rescued and processed. It contains a description
|
705
|
+
of the parameter value expectations. Validations can be turned off
|
706
|
+
in the configuration file.
|
649
707
|
|
708
|
+
Parameter validation normally happens after before_filters, just before
|
709
|
+
your controller method is invoked. If you prefer to control when parameter
|
710
|
+
validation occurs, set the configuration parameter ``validate`` to ``:explicitly``.
|
711
|
+
You must then call the ``apipie_validations`` method yourself, e.g.:
|
712
|
+
|
713
|
+
.. code:: ruby
|
714
|
+
|
715
|
+
before_filter: :apipie_validations
|
716
|
+
|
717
|
+
This is useful if you have before_filters which use parameter values: just add them
|
718
|
+
after the ``apipie_validations`` before_filter.
|
650
719
|
|
651
720
|
TypeValidator
|
652
721
|
-------------
|
653
722
|
Check the parameter type. Only String, Hash and Array are supported
|
654
|
-
for the sake of simplicity. Read more to
|
723
|
+
for the sake of simplicity. Read more to find out how to add
|
655
724
|
your own validator.
|
656
725
|
|
657
726
|
.. code:: ruby
|
@@ -672,7 +741,7 @@ Check parameter value against given regular expression.
|
|
672
741
|
EnumValidator
|
673
742
|
--------------
|
674
743
|
|
675
|
-
Check if parameter value is included given array.
|
744
|
+
Check if parameter value is included in the given array.
|
676
745
|
|
677
746
|
.. code:: ruby
|
678
747
|
|
@@ -682,11 +751,11 @@ Check if parameter value is included given array.
|
|
682
751
|
ProcValidator
|
683
752
|
-------------
|
684
753
|
|
685
|
-
If you need more complex validation and you know you won't reuse it you
|
686
|
-
can use Proc/lambda validator. Provide your own Proc taking value
|
687
|
-
of parameter as the only argument. Return true if value
|
688
|
-
or return some text about what is wrong. _Don't use the keyword *return*
|
689
|
-
if you provide instance of Proc (with lambda it is ok), just use the last
|
754
|
+
If you need more complex validation and you know you won't reuse it, you
|
755
|
+
can use the Proc/lambda validator. Provide your own Proc, taking the value
|
756
|
+
of the parameter as the only argument. Return true if value passes validation
|
757
|
+
or return some text about what is wrong otherwise. _Don't use the keyword *return*
|
758
|
+
if you provide an instance of Proc (with lambda it is ok), just use the last
|
690
759
|
statement return property of ruby.
|
691
760
|
|
692
761
|
.. code:: ruby
|
@@ -699,7 +768,7 @@ statement return property of ruby.
|
|
699
768
|
HashValidator
|
700
769
|
-------------
|
701
770
|
|
702
|
-
You can describe hash parameters in depth if you provide a block with
|
771
|
+
You can describe hash parameters in depth if you provide a block with a
|
703
772
|
description of nested values.
|
704
773
|
|
705
774
|
.. code:: ruby
|
@@ -714,8 +783,8 @@ description of nested values.
|
|
714
783
|
NilValidator
|
715
784
|
------------
|
716
785
|
|
717
|
-
In fact there
|
718
|
-
override parameters described on resource level.
|
786
|
+
In fact there isn't any NilValidator, but setting it to nil can be used to
|
787
|
+
override parameters described on the resource level.
|
719
788
|
|
720
789
|
.. code:: ruby
|
721
790
|
|
@@ -733,10 +802,10 @@ Additional options
|
|
733
802
|
~~~~~~~~~~~~~~~~~
|
734
803
|
|
735
804
|
of
|
736
|
-
Specify the type of items.
|
805
|
+
Specify the type of items. If not given it accepts an array of any item type
|
737
806
|
|
738
807
|
in
|
739
|
-
|
808
|
+
Specify an array of valid item values.
|
740
809
|
|
741
810
|
Examples
|
742
811
|
~~~~~~~~
|
@@ -770,7 +839,7 @@ The retrieving of valid items can be deferred until needed using a lambda. It is
|
|
770
839
|
NestedValidator
|
771
840
|
-------------
|
772
841
|
|
773
|
-
You can describe nested parameters in depth if you provide a block with
|
842
|
+
You can describe nested parameters in depth if you provide a block with a
|
774
843
|
description of nested values.
|
775
844
|
|
776
845
|
.. code:: ruby
|
@@ -786,18 +855,18 @@ Adding custom validator
|
|
786
855
|
-----------------------
|
787
856
|
|
788
857
|
Only basic validators are included but it is really easy to add your own.
|
789
|
-
Create new initializer with subclass of Apipie::Validator::BaseValidator.
|
790
|
-
Two methods are required to implement - instance method
|
858
|
+
Create a new initializer with a subclass of Apipie::Validator::BaseValidator.
|
859
|
+
Two methods are required to implement this - instance method
|
791
860
|
<tt>validate(value)</tt> and class method
|
792
861
|
<tt>build(param_description, argument, options, block)</tt>.
|
793
862
|
|
794
|
-
When searching for validator +build+ method
|
795
|
-
Apipie::Validator::BaseValidator is called. The first one
|
863
|
+
When searching for the validator +build+ method, every subclass of
|
864
|
+
Apipie::Validator::BaseValidator is called. The first one that returns the
|
796
865
|
constructed validator object is used.
|
797
866
|
|
798
867
|
Example: Adding IntegerValidator
|
799
868
|
|
800
|
-
We want to check if parameter value is an integer like this:
|
869
|
+
We want to check if the parameter value is an integer like this:
|
801
870
|
|
802
871
|
.. code:: ruby
|
803
872
|
|
@@ -834,10 +903,10 @@ Parameters of the build method:
|
|
834
903
|
|
835
904
|
param_description
|
836
905
|
Instance of Apipie::ParamDescription contains all
|
837
|
-
given
|
906
|
+
given information about the validated parameter.
|
838
907
|
|
839
908
|
argument
|
840
|
-
Specified validator
|
909
|
+
Specified validator; in our example it is +Integer+
|
841
910
|
|
842
911
|
options
|
843
912
|
Hash with specified options, for us just ``{:desc => "Company ID"}``
|
@@ -852,7 +921,7 @@ block
|
|
852
921
|
|
853
922
|
Every resource/method can belong to one or more versions. The version is
|
854
923
|
specified with the `api_version` DSL keyword. When not specified,
|
855
|
-
the resource
|
924
|
+
the resource belongs to `config.default_version` ("1.0" by default)
|
856
925
|
|
857
926
|
.. code:: ruby
|
858
927
|
|
@@ -869,14 +938,14 @@ the resource belong to `config.default_version` ("1.0" by default)
|
|
869
938
|
api :GET, "/api/users/", "List: users", :deprecated => true
|
870
939
|
|
871
940
|
In the example above we say the whole controller/resource is defined
|
872
|
-
for versions "1" and "2", but we override this
|
873
|
-
`index` belongs only to version "1". Also inheritance works (therefore
|
874
|
-
we can specify the api_version for the parent controller and all
|
875
|
-
children will know about that). Routes can be flagged as deprecated
|
941
|
+
for versions "1" and "2", but we override this by explicitly saying
|
942
|
+
`index` belongs only to version "1". Also, inheritance works (therefore
|
943
|
+
we can specify the api_version for the parent controller, and all
|
944
|
+
children will know about that). Routes can be flagged as deprecated,
|
876
945
|
and an annotation will be added to them when viewing in the API
|
877
946
|
documentation.
|
878
947
|
|
879
|
-
From the Apipie API perspective, the resources belong to version.
|
948
|
+
From the Apipie API perspective, the resources belong to the version.
|
880
949
|
With versioning, there are paths like this provided by apipie:
|
881
950
|
|
882
951
|
.. code::
|
@@ -884,16 +953,16 @@ With versioning, there are paths like this provided by apipie:
|
|
884
953
|
/apipie/1/users/index
|
885
954
|
/apipie/2/users/index
|
886
955
|
|
887
|
-
When not specifying the version explicitly in the path (or in
|
956
|
+
When not specifying the version explicitly in the path (or in DSL),
|
888
957
|
default version (`Apipie.configuration.default_version`) is used
|
889
|
-
instead ("1.0" by default). Therefore,
|
958
|
+
instead ("1.0" by default). Therefore, an application that doesn't
|
890
959
|
need versioning should work as before.
|
891
960
|
|
892
|
-
The static page generator takes version parameter (or uses default).
|
961
|
+
The static page generator takes a version parameter (or uses default).
|
893
962
|
|
894
|
-
You can specify the versions for the examples, with `versions`
|
963
|
+
You can specify the versions for the examples, with the `versions`
|
895
964
|
keyword. It specifies the versions the example is used for. When not
|
896
|
-
specified, it's shown in all versions with given method.
|
965
|
+
specified, it's shown in all versions with the given method.
|
897
966
|
|
898
967
|
When referencing or quering the resource/method descripion, this
|
899
968
|
format should be used: "version#resource#method". When not specified,
|
@@ -906,7 +975,7 @@ the default version is used instead.
|
|
906
975
|
|
907
976
|
The default markup language is `RDoc
|
908
977
|
<http://rdoc.rubyforge.org/RDoc/Markup.html>`_. It can be changed in
|
909
|
-
config file (``config.markup=``) to one of these:
|
978
|
+
the config file (``config.markup=``) to one of these:
|
910
979
|
|
911
980
|
Markdown
|
912
981
|
Use Apipie::Markup::Markdown.new. You need Maruku gem.
|
@@ -914,8 +983,8 @@ Markdown
|
|
914
983
|
Textile
|
915
984
|
Use Apipie::Markup::Textile.new. You need RedCloth gem.
|
916
985
|
|
917
|
-
Or provide you own object with ``to_html(text)`` method.
|
918
|
-
For inspiration this is how Textile markup usage
|
986
|
+
Or provide you own object with a ``to_html(text)`` method.
|
987
|
+
For inspiration, this is how Textile markup usage is implemented:
|
919
988
|
|
920
989
|
.. code:: ruby
|
921
990
|
|
@@ -934,13 +1003,13 @@ Localization
|
|
934
1003
|
|
935
1004
|
Apipie has support for localized API documentation in both formats (JSON and HTML).
|
936
1005
|
Apipie uses the library I18n for localization of itself.
|
937
|
-
Check ``config/locales`` directory for available
|
1006
|
+
Check ``config/locales`` directory for available translations.
|
938
1007
|
|
939
|
-
|
940
|
-
As
|
941
|
-
and how to translate a string using library your project
|
1008
|
+
A major part of strings in the documentation comes from the API.
|
1009
|
+
As preferences regarding localization libraries differ amongst project, Apipie needs to know how to set the locale for your project,
|
1010
|
+
and how to translate a string using the library your project uses. That can be done using lambdas in configuration.
|
942
1011
|
|
943
|
-
Sample configuration when your project
|
1012
|
+
Sample configuration when your project uses FastGettext
|
944
1013
|
|
945
1014
|
|
946
1015
|
.. code:: ruby
|
@@ -959,7 +1028,7 @@ Sample configuration when your project use FastGettext
|
|
959
1028
|
end
|
960
1029
|
end
|
961
1030
|
|
962
|
-
And the strings in API documentation
|
1031
|
+
And the strings in the API documentation need to be marked with the ``N_()`` function
|
963
1032
|
|
964
1033
|
.. code:: ruby
|
965
1034
|
|
@@ -968,7 +1037,7 @@ And the strings in API documentation needs to be marked with the ``N_()`` functi
|
|
968
1037
|
|
969
1038
|
|
970
1039
|
|
971
|
-
When your project use I18n, localization related configuration could
|
1040
|
+
When your project use I18n, localization related configuration could appear as follows
|
972
1041
|
|
973
1042
|
.. code:: ruby
|
974
1043
|
|
@@ -986,7 +1055,7 @@ When your project use I18n, localization related configuration could look like a
|
|
986
1055
|
end
|
987
1056
|
end
|
988
1057
|
|
989
|
-
And the strings in API documentation needs to be in the form of translation keys
|
1058
|
+
And the strings in the API documentation needs to be in the form of translation keys
|
990
1059
|
|
991
1060
|
.. code:: ruby
|
992
1061
|
|
@@ -994,12 +1063,12 @@ And the strings in API documentation needs to be in the form of translation keys
|
|
994
1063
|
param :session, String, :desc => "user_is_logged_in", :required => true
|
995
1064
|
|
996
1065
|
|
997
|
-
The localized versions of the documentation are distinguished by
|
1066
|
+
The localized versions of the documentation are distinguished by language in the filename.
|
998
1067
|
E.g. ``doc/apidoc/apidoc.cs.html`` is static documentation in the Czech language.
|
999
1068
|
If the language is missing, e.g. ``doc/apidoc/apidoc.html``,
|
1000
1069
|
the documentation is localized with the ``default_locale``.
|
1001
1070
|
|
1002
|
-
The dynamic documentation follows the same schema. The ``http://localhost:3000/apidoc/v1.cs.html`` is documentation for version '1' of the API in the Czech language. For JSON
|
1071
|
+
The dynamic documentation follows the same schema. The ``http://localhost:3000/apidoc/v1.cs.html`` is documentation for version '1' of the API in the Czech language. For JSON descriptions, the API applies the same format: ``http://localhost:3000/apidoc/v1.cs.json``
|
1003
1072
|
|
1004
1073
|
|
1005
1074
|
================
|
@@ -1016,19 +1085,19 @@ This will copy the Apipie views to ``app/views/apipie/apipies`` and
|
|
1016
1085
|
==============
|
1017
1086
|
|
1018
1087
|
To generate a static version of documentation (perhaps to put it on
|
1019
|
-
project site or something) run ``rake apipie:static`` task. It will
|
1020
|
-
create set of
|
1021
|
-
directory. If you prefer a
|
1022
|
-
By default the documentation for default API version is
|
1023
|
-
used
|
1088
|
+
your project site or something), run the ``rake apipie:static`` task. It will
|
1089
|
+
create a set of HTML files (multi-pages, single-page, plain) in your doc
|
1090
|
+
directory. If you prefer a JSON version run ``rake apipie:static_json``.
|
1091
|
+
By default the documentation for the default API version is
|
1092
|
+
used. You can specify the version with ``rake apipie:static[2.0]``
|
1024
1093
|
|
1025
1094
|
When you want to avoid any unnecessary computation in production mode,
|
1026
1095
|
you can generate a cache with ``rake apipie:cache`` and configure the
|
1027
1096
|
app to use it in production with ``config.use_cache = Rails.env.production?``
|
1028
1097
|
|
1029
|
-
If, for some complex
|
1098
|
+
If, for some complex cases, you need to generate/re-generate just part of the cache
|
1030
1099
|
use ``rake apipie:cache cache_part=index`` resp. ``rake apipie:cache cache_part=resources``
|
1031
|
-
To generate it
|
1100
|
+
To generate it for different locations for further processing use ``rake apipie:cache OUT=/tmp/apipie_cache``.
|
1032
1101
|
|
1033
1102
|
===================
|
1034
1103
|
JSON checksums
|
@@ -1036,7 +1105,7 @@ To generate it to different location for further processing use ``rake apipie:ca
|
|
1036
1105
|
|
1037
1106
|
If the API client needs to be sure that the JSON didn't changed, add
|
1038
1107
|
the ``ApipieChecksumInHeaders`` middleware in your rails app.
|
1039
|
-
It can add checksum of
|
1108
|
+
It can add a checksum of the entire JSON document in the response headers.
|
1040
1109
|
|
1041
1110
|
.. code::
|
1042
1111
|
|
@@ -1062,7 +1131,7 @@ And in your apipie initializer allow checksum calculation
|
|
1062
1131
|
By default the header is added to responses for ``config.doc_base_url`` and ``/api``.
|
1063
1132
|
It can be changed in configuration (see `Configuration Reference`_ for details).
|
1064
1133
|
|
1065
|
-
The checksum calculation is lazy, done with first request. If you run with ``use_cache = true``,
|
1134
|
+
The checksum calculation is lazy, and done with the first request. If you run with ``use_cache = true``,
|
1066
1135
|
do not forget to run the rake task ``apipie:cache``.
|
1067
1136
|
|
1068
1137
|
|
@@ -1078,18 +1147,18 @@ Documentation Bootstrapping
|
|
1078
1147
|
|
1079
1148
|
Let's say you have an application without REST API documentation.
|
1080
1149
|
However you have a set of tests that are run against this API. A lot
|
1081
|
-
of information is already included in
|
1150
|
+
of information is already included in these tests, it just needs to be
|
1082
1151
|
extracted somehow. Luckily, Apipie provides such a feature.
|
1083
1152
|
|
1084
1153
|
When running the tests, set the ``APIPIE_RECORD=params`` environment
|
1085
|
-
variable or call ``Apipie.record('params')`` from specs starter. You can either use it with functional tests
|
1154
|
+
variable or call ``Apipie.record('params')`` from specs starter. You can either use it with functional tests:
|
1086
1155
|
|
1087
1156
|
.. code::
|
1088
1157
|
|
1089
1158
|
APIPIE_RECORD=params rake test:functionals
|
1090
1159
|
|
1091
1160
|
or you can run your server with this param, in case you run the tests
|
1092
|
-
against running server
|
1161
|
+
against running server:
|
1093
1162
|
|
1094
1163
|
.. code::
|
1095
1164
|
|
@@ -1103,8 +1172,8 @@ Examples Recording
|
|
1103
1172
|
------------------
|
1104
1173
|
|
1105
1174
|
You can also use the tests to generate up-to-date examples for your
|
1106
|
-
code.
|
1107
|
-
tests or a running server, setting ``APIPIE_RECORD=examples`` or
|
1175
|
+
code. Similar to the bootstrapping process, you can use it with functional
|
1176
|
+
tests or a running server, setting ``APIPIE_RECORD=examples`` or calling ``Apipie.record('examples')`` in your specs starter.
|
1108
1177
|
|
1109
1178
|
.. code::
|
1110
1179
|
|
@@ -1113,13 +1182,17 @@ tests or a running server, setting ``APIPIE_RECORD=examples`` or by calling ``Ap
|
|
1113
1182
|
|
1114
1183
|
The data is written into ``doc/apipie_examples.yml``. By default,
|
1115
1184
|
only the first example is shown for each action. You can customize
|
1116
|
-
this by setting ``show_in_doc`` attribute at each example.
|
1185
|
+
this by setting the ``show_in_doc`` attribute at each example.
|
1186
|
+
|
1187
|
+
You can add a title to the examples (useful when showing more than
|
1188
|
+
one example per method) by adding a 'title' attribute.
|
1117
1189
|
|
1118
1190
|
.. code::
|
1119
1191
|
|
1120
1192
|
--- !omap
|
1121
1193
|
- announcements#index:
|
1122
1194
|
- !omap
|
1195
|
+
- title: This is a custom title for this example
|
1123
1196
|
- verb: :GET
|
1124
1197
|
- path: /api/blabla/1
|
1125
1198
|
- versions:
|
@@ -1165,8 +1238,8 @@ And then configure RSpec in this way:
|
|
1165
1238
|
config.filter_run :show_in_doc => true if ENV['APIPIE_RECORD']
|
1166
1239
|
end
|
1167
1240
|
|
1168
|
-
This way, when running in recording mode, only the tests that
|
1169
|
-
``:show_in_doc`` metadata will be
|
1241
|
+
This way, when running in recording mode, only the tests that have been marked with the
|
1242
|
+
``:show_in_doc`` metadata will be run, and hence only those will be used as examples.
|
1170
1243
|
|
1171
1244
|
====================
|
1172
1245
|
Bindings Generator
|
@@ -1174,18 +1247,18 @@ This way, when running in recording mode, only the tests that has been marked wi
|
|
1174
1247
|
|
1175
1248
|
In earlier versions (<= 0.0.13), there was a simple client generator
|
1176
1249
|
as a part of Apipie gem. As more features and users came to Apipie,
|
1177
|
-
|
1250
|
+
there was a greater need for changes on a per project basis. It's
|
1178
1251
|
hard (or even impossible) to provide a generic solution for the client
|
1179
|
-
code. We also don't want to tell you what's the
|
1252
|
+
code. We also don't want to tell you what's the right way to do it
|
1180
1253
|
(what gems to use, how the API should look like etc.).
|
1181
1254
|
|
1182
|
-
Therefore you can't generate
|
1255
|
+
Therefore you can't generate client code directly by a rake task in
|
1183
1256
|
further versions.
|
1184
1257
|
|
1185
|
-
There is, however, even better and more flexible way to reuse your API
|
1258
|
+
There is, however, an even better and more flexible way to reuse your API
|
1186
1259
|
documentation for this purpose: using the API the Apipie
|
1187
|
-
provides in the generator code.
|
1188
|
-
`Foreman API bindings <https://github.com/mbacovsky/foreman_api>`_
|
1260
|
+
provides in the generator code. Check out
|
1261
|
+
`Foreman API bindings <https://github.com/mbacovsky/foreman_api>`_, as they
|
1189
1262
|
use exactly this approach. You also don't need to run the service,
|
1190
1263
|
provided it uses Apipie as a backend.
|
1191
1264
|
|
@@ -1196,8 +1269,8 @@ And if you write one on your own, don't hesitate to share it with us!
|
|
1196
1269
|
Disqus Integration
|
1197
1270
|
====================
|
1198
1271
|
|
1199
|
-
You can
|
1200
|
-
|
1272
|
+
You can setup `Disqus <http://www.disqus.com>`_ discussion within
|
1273
|
+
your documentation. Just set the credentials in the Apipie
|
1201
1274
|
configuration:
|
1202
1275
|
|
1203
1276
|
.. code:: ruby
|