apipie-rails 0.0.13 → 0.0.14
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/Gemfile.lock +5 -3
- data/README.rst +688 -0
- data/apipie-rails.gemspec +2 -4
- data/app/controllers/apipie/apipies_controller.rb +73 -41
- data/app/views/apipie/apipies/index.html.erb +10 -3
- data/app/views/apipie/apipies/method.html.erb +5 -2
- data/app/views/apipie/apipies/resource.html.erb +10 -3
- data/lib/apipie-rails.rb +1 -0
- data/lib/apipie/apipie_module.rb +28 -79
- data/lib/apipie/application.rb +194 -97
- data/lib/apipie/client/generator.rb +5 -4
- data/lib/apipie/configuration.rb +112 -0
- data/lib/apipie/dsl_definition.rb +93 -16
- data/lib/apipie/extractor.rb +12 -5
- data/lib/apipie/extractor/collector.rb +1 -1
- data/lib/apipie/extractor/recorder.rb +2 -1
- data/lib/apipie/extractor/writer.rb +11 -4
- data/lib/apipie/helpers.rb +15 -4
- data/lib/apipie/markup.rb +3 -4
- data/lib/apipie/method_description.rb +28 -22
- data/lib/apipie/resource_description.rb +44 -42
- data/lib/apipie/routing.rb +3 -1
- data/lib/apipie/static_dispatcher.rb +0 -1
- data/lib/apipie/version.rb +1 -1
- data/lib/generators/apipie/install/README +6 -0
- data/lib/generators/apipie/install/install_generator.rb +25 -0
- data/lib/generators/apipie/install/templates/initializer.rb.erb +7 -0
- data/lib/tasks/apipie.rake +31 -39
- data/spec/controllers/api/v1/architectures_controller_spec.rb +30 -0
- data/spec/controllers/api/v2/architectures_controller_spec.rb +12 -0
- data/spec/controllers/apipies_controller_spec.rb +18 -12
- data/spec/controllers/users_controller_spec.rb +56 -19
- data/spec/dummy/app/controllers/api/base_controller.rb +4 -0
- data/spec/dummy/app/controllers/api/v1/architectures_controller.rb +32 -0
- data/spec/dummy/app/controllers/api/v1/base_controller.rb +11 -0
- data/spec/dummy/app/controllers/api/v2/architectures_controller.rb +32 -0
- data/spec/dummy/app/controllers/api/v2/base_controller.rb +11 -0
- data/spec/dummy/app/controllers/twitter_example_controller.rb +1 -1
- data/spec/dummy/app/controllers/users_controller.rb +2 -3
- data/spec/dummy/config/initializers/apipie.rb +29 -6
- data/spec/lib/method_description_spec.rb +29 -0
- data/spec/lib/param_description_spec.rb +41 -0
- data/spec/lib/resource_description_spec.rb +28 -0
- data/spec/spec_helper.rb +1 -1
- metadata +99 -164
- data/README.rdoc +0 -367
- data/lib/apipie/client/base.rb +0 -133
- data/lib/apipie/client/cli_command.rb +0 -130
- data/lib/apipie/client/main.rb +0 -101
- data/lib/apipie/client/rest_client_oauth.rb +0 -19
- data/lib/apipie/client/template/Gemfile.tt +0 -3
- data/lib/apipie/client/template/README.tt +0 -3
- data/lib/apipie/client/template/Rakefile.tt +0 -2
- data/lib/apipie/client/template/a_name.gemspec.tt +0 -23
- data/lib/apipie/client/template/bin/bin.rb.tt +0 -30
- data/lib/apipie/client/template/lib/a_name.rb.tt +0 -27
- data/lib/apipie/client/template/lib/a_name/commands/cli.rb.tt +0 -22
- data/lib/apipie/client/template/lib/a_name/config.yml +0 -6
- data/lib/apipie/client/template/lib/a_name/resources/resource.rb.tt +0 -22
- data/lib/apipie/client/template/lib/a_name/version.rb.tt +0 -3
- data/lib/apipie/client/thor.rb +0 -21
- data/rubygem-apipie-rails.spec +0 -122
- data/spec/lib/parameter_description_spec.rb +0 -41
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
apipie-rails (0.0.
|
4
|
+
apipie-rails (0.0.13)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
@@ -46,6 +46,8 @@ GEM
|
|
46
46
|
i18n (>= 0.4.0)
|
47
47
|
mime-types (~> 1.16)
|
48
48
|
treetop (~> 1.4.8)
|
49
|
+
maruku (0.6.1)
|
50
|
+
syntax (>= 1.0.0)
|
49
51
|
mime-types (1.19)
|
50
52
|
minitest (3.2.0)
|
51
53
|
multi_json (1.3.6)
|
@@ -76,7 +78,6 @@ GEM
|
|
76
78
|
rake (0.9.2.2)
|
77
79
|
rdoc (3.12)
|
78
80
|
json (~> 1.4)
|
79
|
-
redcarpet (2.1.1)
|
80
81
|
rest-client (1.6.7)
|
81
82
|
mime-types (>= 1.16)
|
82
83
|
rspec (2.11.0)
|
@@ -97,6 +98,7 @@ GEM
|
|
97
98
|
rack (~> 1.0)
|
98
99
|
tilt (~> 1.1, != 1.3.0)
|
99
100
|
sqlite3 (1.3.6)
|
101
|
+
syntax (1.0.0)
|
100
102
|
thor (0.15.4)
|
101
103
|
tilt (1.3.3)
|
102
104
|
treetop (1.4.10)
|
@@ -110,11 +112,11 @@ PLATFORMS
|
|
110
112
|
DEPENDENCIES
|
111
113
|
RedCloth
|
112
114
|
apipie-rails!
|
115
|
+
maruku
|
113
116
|
minitest
|
114
117
|
oauth
|
115
118
|
rails (>= 3.0.10)
|
116
119
|
rake
|
117
|
-
redcarpet
|
118
120
|
rest-client
|
119
121
|
rspec-rails
|
120
122
|
sqlite3
|
data/README.rst
ADDED
@@ -0,0 +1,688 @@
|
|
1
|
+
========================
|
2
|
+
API Documentation Tool
|
3
|
+
========================
|
4
|
+
|
5
|
+
.. image:: https://secure.travis-ci.org/Pajk/apipie-rails.png?branch=master
|
6
|
+
|
7
|
+
Apipie-rails is a DSL and Rails engine for documenting you RESTful
|
8
|
+
API. Instead of traditional use of ``#comments``, Apipie let's you
|
9
|
+
describe the code by code. This brings advantages like:
|
10
|
+
|
11
|
+
* no need to learn yet another syntax, you already know Ruby, right?
|
12
|
+
* possibility reuse the doc for other purposes (like validation)
|
13
|
+
* easier to extend and maintain (no string parsing involved)
|
14
|
+
* possibility to use other sources for documentation purposes (such as
|
15
|
+
routes etc.)
|
16
|
+
|
17
|
+
The documenatation is available right in your app (by default under
|
18
|
+
``/apipie`` path. In development mode, you can see the changes as you
|
19
|
+
go. It's markup language agnostic and even provides an API for reusing
|
20
|
+
the documentation data in form of JSON.
|
21
|
+
|
22
|
+
Getting started
|
23
|
+
---------------
|
24
|
+
|
25
|
+
The easiest way to get Apipie up and running with your app is:
|
26
|
+
|
27
|
+
.. code::
|
28
|
+
|
29
|
+
$ echo "gem 'apipie-rails'" >> Gemfile
|
30
|
+
$ bundle install
|
31
|
+
$ rails g apipie:install
|
32
|
+
|
33
|
+
Now you can start documenting your resources and actions (see
|
34
|
+
`DSL Reference`_ for more info:
|
35
|
+
|
36
|
+
.. code:: ruby
|
37
|
+
|
38
|
+
api :GET, '/users/:id'
|
39
|
+
param :id, :number
|
40
|
+
def show
|
41
|
+
# ...
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
Run your application and see the result at
|
46
|
+
``http://localhost:3000/apipie``. For it's further processing, you can
|
47
|
+
use ``http://localhost:3000/apipie.json``.
|
48
|
+
|
49
|
+
For more comprehensive getting started guide, see
|
50
|
+
`this demo <https://github.com/iNecas/apipie-demo>`_, that includes
|
51
|
+
features like generating documenation from tests, recording examples etc.
|
52
|
+
|
53
|
+
Screenshots
|
54
|
+
-----------
|
55
|
+
|
56
|
+
.. image:: https://img.skitch.com/20120428-nruk3e87xs2cu4ydsjujdh11fq.png
|
57
|
+
.. image:: https://img.skitch.com/20120428-bni2cmq5cyhjuw1jkd78e3qjxn.png
|
58
|
+
|
59
|
+
Authors
|
60
|
+
-------
|
61
|
+
|
62
|
+
`Pajk <https://github.com/Pajk>`_ and `iNecas <https://github.com/iNecas>`_
|
63
|
+
|
64
|
+
Contributors
|
65
|
+
------------
|
66
|
+
|
67
|
+
See `Contributors page <https://github.com/Pajk/apipie-rails/graphs/contributors>`_. Special thanks to all of them!
|
68
|
+
|
69
|
+
License
|
70
|
+
-------
|
71
|
+
|
72
|
+
Apipie-rails is released under the `MIT License <http://opensource.org/licenses/MIT>`_
|
73
|
+
|
74
|
+
===============
|
75
|
+
Documentation
|
76
|
+
===============
|
77
|
+
|
78
|
+
.. contents:: `Table Of Contents`
|
79
|
+
:depth: 2
|
80
|
+
|
81
|
+
===============
|
82
|
+
DSL Reference
|
83
|
+
===============
|
84
|
+
|
85
|
+
Resource Description
|
86
|
+
--------------------
|
87
|
+
|
88
|
+
You can describe a resource on controller level. The description is introcudes by calling
|
89
|
+
``resource description do ... end``.
|
90
|
+
|
91
|
+
Inheritance is supported, so you can specify common params for group of controlelrs in their parent
|
92
|
+
class.
|
93
|
+
|
94
|
+
The following keywords are available (all are optional):
|
95
|
+
|
96
|
+
resource_id
|
97
|
+
How will be the resource referenced in Apipie (paths, ``see`` command etc.), by default `controller_name.downcase` is used.
|
98
|
+
|
99
|
+
name
|
100
|
+
Human readable name of resource. By default ``class.name.humanize`` is used.
|
101
|
+
|
102
|
+
short (also short_description)
|
103
|
+
Short description of the resource (it's shown on both list of resources and resource details)
|
104
|
+
|
105
|
+
desc (also description and full_description)
|
106
|
+
Full description of the resource (shown only on resource details)
|
107
|
+
|
108
|
+
param
|
109
|
+
Common params for all method defined in controller/child controllers.
|
110
|
+
|
111
|
+
api_base_url
|
112
|
+
What url is the resource available under.
|
113
|
+
|
114
|
+
api_versions (also api_version)
|
115
|
+
What versions does the controller define the resource. (See `Versioning`_ for details.)
|
116
|
+
|
117
|
+
formats
|
118
|
+
request / response formats.
|
119
|
+
|
120
|
+
error
|
121
|
+
Describe each possible error that can happen what calling all
|
122
|
+
methods define in controller. HTTP response code and description can be provided.
|
123
|
+
|
124
|
+
app_info
|
125
|
+
In case of versioning, this set's app info description on per_version basis.
|
126
|
+
|
127
|
+
Example:
|
128
|
+
~~~~~~~~
|
129
|
+
|
130
|
+
.. code:: ruby
|
131
|
+
|
132
|
+
resource_description do
|
133
|
+
short 'Site members'
|
134
|
+
path '/users'
|
135
|
+
formats ['json']
|
136
|
+
param :id, Fixnum, :desc => "User ID", :required => false
|
137
|
+
param :resource_param, Hash, :desc => 'Param description for all methods' do
|
138
|
+
param :ausername, String, :desc => "Username for login", :required => true
|
139
|
+
param :apassword, String, :desc => "Password for login", :required => true
|
140
|
+
end
|
141
|
+
api_version "development"
|
142
|
+
error 404, "Missing"
|
143
|
+
error 500, "Server crashed for some <%= reason %>"
|
144
|
+
description <<-EOS
|
145
|
+
== Long description
|
146
|
+
Example resource for rest api documentation
|
147
|
+
These can now be accessed in <tt>shared/header</tt> with:
|
148
|
+
Headline: <%= headline %>
|
149
|
+
First name: <%= person.first_name %>
|
150
|
+
|
151
|
+
If you need to find out whether a certain local variable has been
|
152
|
+
assigned a value in a particular render call, you need to use the
|
153
|
+
following pattern:
|
154
|
+
|
155
|
+
<% if local_assigns.has_key? :headline %>
|
156
|
+
Headline: <%= headline %>
|
157
|
+
<% end %>
|
158
|
+
|
159
|
+
Testing using <tt>defined? headline</tt> will not work. This is an
|
160
|
+
implementation restriction.
|
161
|
+
|
162
|
+
=== Template caching
|
163
|
+
|
164
|
+
By default, Rails will compile each template to a method in order
|
165
|
+
to render it. When you alter a template, Rails will check the
|
166
|
+
file's modification time and recompile it in development mode.
|
167
|
+
EOS
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
Method Description
|
172
|
+
------------------
|
173
|
+
|
174
|
+
Then describe methods available to your API.
|
175
|
+
|
176
|
+
api
|
177
|
+
Say how is this method exposed and provide short description.
|
178
|
+
The first parameter is HTTP method (one of :GET/:POST/:PUT/:DELETE).
|
179
|
+
The second parameter is relative URL path which is mapped to this
|
180
|
+
method. The last parameter is methods short description.
|
181
|
+
You can use this +api+ method more than once for one method. It could
|
182
|
+
be useful when there are more routes mapped to it.
|
183
|
+
|
184
|
+
api_versions (also api_version)
|
185
|
+
What version(s) does the action belong to. (See `Versioning`_ for details.)
|
186
|
+
|
187
|
+
param
|
188
|
+
Look at Parameter description section for details.
|
189
|
+
|
190
|
+
formats
|
191
|
+
Method level request / response formats.
|
192
|
+
|
193
|
+
error
|
194
|
+
Describe each possible error that can happen what calling this
|
195
|
+
method. HTTP response code and description can be provided.
|
196
|
+
|
197
|
+
description
|
198
|
+
Full method description which will be converted to HTML by
|
199
|
+
chosen markup language processor.
|
200
|
+
|
201
|
+
example
|
202
|
+
Provide example of server response, whole communication or response type.
|
203
|
+
It will be formatted as code.
|
204
|
+
|
205
|
+
see
|
206
|
+
Provide reference to another method, this has to be string with
|
207
|
+
controller_name#method_name.
|
208
|
+
|
209
|
+
Example:
|
210
|
+
~~~~~~~~
|
211
|
+
|
212
|
+
.. code:: ruby
|
213
|
+
|
214
|
+
api :GET, "/users/:id", "Show user profile"
|
215
|
+
error :code => 401, :desc => "Unauthorized"
|
216
|
+
error :code => 404, :desc => "Not Found"
|
217
|
+
param :session, String, :desc => "user is logged in", :required => true
|
218
|
+
param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
|
219
|
+
param :array_param, [100, "one", "two", 1, 2], :desc => "array validator"
|
220
|
+
param :boolean_param, [true, false], :desc => "array validator with boolean"
|
221
|
+
param :proc_param, lambda { |val|
|
222
|
+
val == "param value" ? true : "The only good value is 'param value'."
|
223
|
+
}, :desc => "proc validator"
|
224
|
+
description "method description"
|
225
|
+
formats ['json', 'jsonp', 'xml']
|
226
|
+
example " 'user': {...} "
|
227
|
+
see "users#showme"
|
228
|
+
def show
|
229
|
+
#...
|
230
|
+
end
|
231
|
+
|
232
|
+
|
233
|
+
Parameter Description
|
234
|
+
---------------------
|
235
|
+
|
236
|
+
Use ``param`` to describe every possible parameter. You can use Hash validator
|
237
|
+
in cooperation with block given to param method to describe nested parameters.
|
238
|
+
|
239
|
+
name
|
240
|
+
The first argument is parameter name as a symbol.
|
241
|
+
|
242
|
+
validator
|
243
|
+
Second parameter is parameter validator, choose one from section `Validators`_
|
244
|
+
|
245
|
+
desc
|
246
|
+
Parameter description.
|
247
|
+
|
248
|
+
required
|
249
|
+
Set this true/false to make it required/optional. Default is optional
|
250
|
+
|
251
|
+
allow_nil
|
252
|
+
Set true is ``nil`` can be passed for this param.
|
253
|
+
|
254
|
+
Example:
|
255
|
+
~~~~~~~~
|
256
|
+
|
257
|
+
.. code:: ruby
|
258
|
+
|
259
|
+
param :user, Hash, :desc => "User info" do
|
260
|
+
param :username, String, :desc => "Username for login", :required => true
|
261
|
+
param :password, String, :desc => "Password for login", :required => true
|
262
|
+
param :membership, ["standard","premium"], :desc => "User membership"
|
263
|
+
end
|
264
|
+
def create
|
265
|
+
#...
|
266
|
+
end
|
267
|
+
|
268
|
+
|
269
|
+
=========================
|
270
|
+
Configuration Reference
|
271
|
+
=========================
|
272
|
+
|
273
|
+
Create configuration file in e.g. ``/config/initializers/apipie.rb``.
|
274
|
+
You can set application name, footer text, API and documentation base URL
|
275
|
+
and turn off validations. You can also choose your favorite markup language
|
276
|
+
of full descriptions.
|
277
|
+
|
278
|
+
app_name
|
279
|
+
Name of your application used in breadcrumbs navigation.
|
280
|
+
|
281
|
+
copyright
|
282
|
+
Copyright information (shown in page footer).
|
283
|
+
|
284
|
+
doc_base_url
|
285
|
+
Documentation frontend base url.
|
286
|
+
|
287
|
+
api_base_url
|
288
|
+
Base url of your API, most probably /api.
|
289
|
+
|
290
|
+
default_version
|
291
|
+
Default API version to be used (1.0 by default)
|
292
|
+
|
293
|
+
validate
|
294
|
+
Parameters validation is turned off when set to false.
|
295
|
+
|
296
|
+
app_info
|
297
|
+
Application long description.
|
298
|
+
|
299
|
+
reload_controllers
|
300
|
+
Set to enable/disable reloading controllers (and the documentation with it), by default enabled in development.
|
301
|
+
|
302
|
+
api_controllers_matcher
|
303
|
+
For reloading to work properly you need to specify where your API controllers are.
|
304
|
+
|
305
|
+
markup
|
306
|
+
You can choose markup language for descriptions of your application,
|
307
|
+
resources and methods. RDoc is the default but you can choose from
|
308
|
+
Apipie::Markup::Markdown.new or Apipie::Markup::Textile.new.
|
309
|
+
In order to use Markdown you need Redcarpet gem and for Textile you
|
310
|
+
need RedCloth. Add those to your gemfile and run bundle if you
|
311
|
+
want to use them. You can also add any other markup language
|
312
|
+
processor.
|
313
|
+
|
314
|
+
layout
|
315
|
+
Name of a layout template to use instead of Apipie's layout. You can use Apipie.include_stylesheets and Apipie.include_javascripts helpers to include Apipie's stylesheets and javascripts.
|
316
|
+
|
317
|
+
ignored
|
318
|
+
An array of controller names (strings) (might include actions as well)
|
319
|
+
to be ignored when generationg the documentation
|
320
|
+
e.g. ``%w[Api::CommentsController Api::PostsController#post]``
|
321
|
+
|
322
|
+
|
323
|
+
Example:
|
324
|
+
|
325
|
+
.. code:: ruby
|
326
|
+
|
327
|
+
Apipie.configure do |config|
|
328
|
+
config.app_name = "Test app"
|
329
|
+
config.copyright = "© 2012 Pavel Pokorny"
|
330
|
+
config.doc_base_url = "/apidoc"
|
331
|
+
config.api_base_url = "/api"
|
332
|
+
config.validate = false
|
333
|
+
config.markup = Apipie::Markup::Markdown.new
|
334
|
+
config.reload_controllers = true
|
335
|
+
config.api_controllers_matcher = File.join(Rails.root, "app", "controllers", "**","*.rb")
|
336
|
+
config.app_info = <<-"
|
337
|
+
This is where you can inform user about your application and API
|
338
|
+
in general.
|
339
|
+
", '1.0'
|
340
|
+
end
|
341
|
+
|
342
|
+
|
343
|
+
============
|
344
|
+
Validators
|
345
|
+
============
|
346
|
+
|
347
|
+
Every parameter needs to have associated validator. For now there are some
|
348
|
+
basic validators. You can always provide your own to reach complex
|
349
|
+
results.
|
350
|
+
|
351
|
+
If validations are enabled (default state) the parameters of every
|
352
|
+
request are validated. If the value is wrong a +ArgumentError+ exception
|
353
|
+
is raised and can be rescued and processed. It contains some description
|
354
|
+
of parameter value expectations. Validations can be turned off
|
355
|
+
in configuration file.
|
356
|
+
|
357
|
+
|
358
|
+
TypeValidator
|
359
|
+
-------------
|
360
|
+
Check the parameter type. Only String, Hash and Array are supported
|
361
|
+
for the sake of simplicity. Read more to to find out how to add
|
362
|
+
your own validator.
|
363
|
+
|
364
|
+
.. code:: ruby
|
365
|
+
|
366
|
+
param :session, String, :desc => "user is logged in", :required => true
|
367
|
+
param :facts, Hash, :desc => "Additional optional facts about the user"
|
368
|
+
|
369
|
+
|
370
|
+
RegexpValidator
|
371
|
+
---------------
|
372
|
+
Check parameter value against given regular expression.
|
373
|
+
|
374
|
+
.. code:: ruby
|
375
|
+
|
376
|
+
param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
|
377
|
+
|
378
|
+
|
379
|
+
ArrayValidator
|
380
|
+
--------------
|
381
|
+
|
382
|
+
Check if parameter value is included given array.
|
383
|
+
|
384
|
+
.. code:: ruby
|
385
|
+
|
386
|
+
param :array_param, [100, "one", "two", 1, 2], :desc => "array validator"
|
387
|
+
|
388
|
+
|
389
|
+
ProcValidator
|
390
|
+
-------------
|
391
|
+
|
392
|
+
If you need more complex validation and you know you won't reuse it you
|
393
|
+
can use Proc/lambda validator. Provide your own Proc taking value
|
394
|
+
of parameter as the only argument. Return true if value pass validation
|
395
|
+
or return some text about what is wrong. _Don't use the keyword *return*
|
396
|
+
if you provide instance of Proc (with lambda it is ok), just use the last
|
397
|
+
statement return property of ruby.
|
398
|
+
|
399
|
+
.. code:: ruby
|
400
|
+
|
401
|
+
param :proc_param, lambda { |val|
|
402
|
+
val == "param value" ? true : "The only good value is 'param value'."
|
403
|
+
}, :desc => "proc validator"
|
404
|
+
|
405
|
+
|
406
|
+
HashValidator
|
407
|
+
-------------
|
408
|
+
|
409
|
+
You can describe hash parameters in depth if you provide a block with
|
410
|
+
description of nested values.
|
411
|
+
|
412
|
+
.. code:: ruby
|
413
|
+
|
414
|
+
param :user, Hash, :desc => "User info" do
|
415
|
+
param :username, String, :desc => "Username for login", :required => true
|
416
|
+
param :password, String, :desc => "Password for login", :required => true
|
417
|
+
param :membership, ["standard","premium"], :desc => "User membership"
|
418
|
+
end
|
419
|
+
|
420
|
+
|
421
|
+
NilValidator
|
422
|
+
------------
|
423
|
+
|
424
|
+
In fact there is any NilValidator but setting it to nil can be used to
|
425
|
+
override parameters described on resource level.
|
426
|
+
|
427
|
+
.. code:: ruby
|
428
|
+
|
429
|
+
param :user, nil
|
430
|
+
def destroy
|
431
|
+
#...
|
432
|
+
end
|
433
|
+
|
434
|
+
|
435
|
+
Adding custom validator
|
436
|
+
-----------------------
|
437
|
+
|
438
|
+
Only basic validators are included but it is really easy to add your own.
|
439
|
+
Create new initializer with subclass of Apipie::Validator::BaseValidator.
|
440
|
+
Two methods are required to implement - instance method
|
441
|
+
<tt>validate(value)</tt> and class method
|
442
|
+
<tt>build(param_description, argument, options, block)</tt>.
|
443
|
+
|
444
|
+
When searching for validator +build+ method of every subclass of
|
445
|
+
Apipie::Validator::BaseValidator is called. The first one whitch return
|
446
|
+
constructed validator object is used.
|
447
|
+
|
448
|
+
Example: Adding IntegerValidator
|
449
|
+
|
450
|
+
We want to check if parameter value is an integer like this:
|
451
|
+
|
452
|
+
.. code:: ruby
|
453
|
+
|
454
|
+
param :id, Integer, :desc => "Company ID"
|
455
|
+
|
456
|
+
So we create apipie_validators.rb initializer with this content:
|
457
|
+
|
458
|
+
.. code:: ruby
|
459
|
+
|
460
|
+
class IntegerValidator < Apipie::Validator::BaseValidator
|
461
|
+
|
462
|
+
def initialize(param_description, argument)
|
463
|
+
super(param_description)
|
464
|
+
@type = argument
|
465
|
+
end
|
466
|
+
|
467
|
+
def validate(value)
|
468
|
+
return false if value.nil?
|
469
|
+
!!(value.to_s =~ /^[-+]?[0-9]+$/)
|
470
|
+
end
|
471
|
+
|
472
|
+
def self.build(param_description, argument, options, block)
|
473
|
+
if argument == Integer || argument == Fixnum
|
474
|
+
self.new(param_description, argument)
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
def description
|
479
|
+
"Must be #{@type}."
|
480
|
+
end
|
481
|
+
end
|
482
|
+
|
483
|
+
Parameters of the build method:
|
484
|
+
|
485
|
+
param_description
|
486
|
+
Instance of Apipie::ParamDescription contains all
|
487
|
+
given informations about validated parameter.
|
488
|
+
|
489
|
+
argument
|
490
|
+
Specified validator, in our example it is +Integer+
|
491
|
+
|
492
|
+
options
|
493
|
+
Hash with specified options, for us just ``{:desc => "Company ID"}``
|
494
|
+
|
495
|
+
block
|
496
|
+
Block converted into Proc, use it as you desire. In this example nil.
|
497
|
+
|
498
|
+
|
499
|
+
============
|
500
|
+
Versioning
|
501
|
+
============
|
502
|
+
|
503
|
+
Every resource/method can belong to one or more versions. The version is
|
504
|
+
specified with the `api_version` DSL keyword. When not specified,
|
505
|
+
the resource belong to `config.default_version` ("1.0" by default)
|
506
|
+
|
507
|
+
.. code:: ruby
|
508
|
+
|
509
|
+
resource_description do
|
510
|
+
api_versions "1", "2"
|
511
|
+
end
|
512
|
+
|
513
|
+
api :GET, "/api/users/"
|
514
|
+
api_version "1"
|
515
|
+
def index
|
516
|
+
# ...
|
517
|
+
end
|
518
|
+
|
519
|
+
In the example above we say the whole controller/resource is defined
|
520
|
+
for versions "1" and "2", but we override this with explicitly saying
|
521
|
+
`index` belongs only to version "1". Also inheritance works (therefore
|
522
|
+
we can specify the api_version for the parent controller and all
|
523
|
+
children will know about that).
|
524
|
+
|
525
|
+
From the Apipie API perspective, the resources belong to version.
|
526
|
+
With versioning, there are paths like this provided by apipie:
|
527
|
+
|
528
|
+
.. code::
|
529
|
+
|
530
|
+
/apipie/1/users/index
|
531
|
+
/apipie/2/users/index
|
532
|
+
|
533
|
+
When not specifying the version explicitly in the path (or in dsl),
|
534
|
+
default version (`Apipie.configuration.default_version`) is used
|
535
|
+
instead ("1.0" by default). Therefore, the application that doesn't
|
536
|
+
need versioning should work as before.
|
537
|
+
|
538
|
+
The static page generator takes version parameter (or uses default).
|
539
|
+
|
540
|
+
You can specify the versions for the examples, with `versions`
|
541
|
+
keyword. It specifies the versions the example is used for. When not
|
542
|
+
specified, it's shown in all versions with given method.
|
543
|
+
|
544
|
+
When referencing or quering the resource/method descripion, this
|
545
|
+
format should be used: "verson#resource#method". When not specified,
|
546
|
+
the default version is used instead.
|
547
|
+
|
548
|
+
|
549
|
+
========
|
550
|
+
Markup
|
551
|
+
========
|
552
|
+
|
553
|
+
The default markup language is `RDoc
|
554
|
+
<http://rdoc.rubyforge.org/RDoc/Markup.html>`_. It can be changed in
|
555
|
+
config file (``config.markup=``) to one of these:
|
556
|
+
|
557
|
+
Markdown
|
558
|
+
Use Apipie::Markup::Markdown.new. You need Maruku gem.
|
559
|
+
|
560
|
+
Textile
|
561
|
+
Use Apipie::Markup::Textile.new. You need RedCloth gem.
|
562
|
+
|
563
|
+
Or provide you own object with ``to_html(text)`` method.
|
564
|
+
For inspiration this is how Textile look like:
|
565
|
+
|
566
|
+
.. code:: ruby
|
567
|
+
|
568
|
+
class Textile
|
569
|
+
def initialize
|
570
|
+
require 'RedCloth'
|
571
|
+
end
|
572
|
+
def to_html(text)
|
573
|
+
RedCloth.new(text).to_html
|
574
|
+
end
|
575
|
+
end
|
576
|
+
|
577
|
+
|
578
|
+
==============
|
579
|
+
Static files
|
580
|
+
==============
|
581
|
+
|
582
|
+
To generate static version of documentation (perhaps to put it on
|
583
|
+
project site or something) run ``rake apipie:static`` task. It will
|
584
|
+
create set of html files (multi-pages, single-page, plain) in your doc
|
585
|
+
directory. By default the documentation for default API version is
|
586
|
+
used, you can specify the version with ``rake apipie:static[2.0]``
|
587
|
+
|
588
|
+
When you want to avoid any unnecessary computation in production mode,
|
589
|
+
you can generate a cache with ``rake apipie:cache`` and configure the
|
590
|
+
app to use it in production with ``config.use_cache = Rails.env.production?``
|
591
|
+
|
592
|
+
===================
|
593
|
+
Tests Integration
|
594
|
+
===================
|
595
|
+
|
596
|
+
Apipie integrates with automated testing in two ways. *Documentation
|
597
|
+
bootstrapping* and *examples recording*.
|
598
|
+
|
599
|
+
Documentation Bootstrapping
|
600
|
+
---------------------------
|
601
|
+
|
602
|
+
Let's say you have an application without REST API documentation.
|
603
|
+
However you have a set of tests that are run against this API. A lot
|
604
|
+
of information is already included in this tests, it just needs to be
|
605
|
+
extracted somehow. Luckily, Apipie provides such a feature.
|
606
|
+
|
607
|
+
When running the tests, set the ``APIPIE_RECORD=params`` environment
|
608
|
+
variable. You can either use it with functional tests
|
609
|
+
|
610
|
+
.. code::
|
611
|
+
|
612
|
+
APIPIE_RECORD=params rake test:functionals
|
613
|
+
|
614
|
+
or you can run your server with this param, in case you run the tests
|
615
|
+
against running server
|
616
|
+
|
617
|
+
.. code::
|
618
|
+
|
619
|
+
APIPIE_RECORD=params rails server
|
620
|
+
|
621
|
+
When the process quits, the data from requests/responses are used to
|
622
|
+
determine the documentation. It's quite raw, but it makes the initial
|
623
|
+
phase much easier.
|
624
|
+
|
625
|
+
Examples Recording
|
626
|
+
------------------
|
627
|
+
|
628
|
+
You can also use the tests to generate up-to-date examples for your
|
629
|
+
code. Similarly to the bootstrapping, you can use it with functional
|
630
|
+
tests or a running server, setting ``APIPIE_RECORD=examples``
|
631
|
+
|
632
|
+
.. code::
|
633
|
+
|
634
|
+
APIPIE_RECORD=examples rake test:functionals
|
635
|
+
APIPIE_RECORD=examples rails server
|
636
|
+
|
637
|
+
The data are written into ``doc/apipie_examples.yml``. By default,
|
638
|
+
only the first example is shown for each action. You can customize
|
639
|
+
this by setting ``show_in_doc`` attribute at each example.
|
640
|
+
|
641
|
+
|
642
|
+
====================
|
643
|
+
Bindings Generator
|
644
|
+
====================
|
645
|
+
|
646
|
+
In earlier versions (<= 0.0.13), there was a simple client generator
|
647
|
+
as a part of Apipie gem. As more features and users came to Apipie,
|
648
|
+
more and more there was a need for changes on per project basis. It's
|
649
|
+
hard (or even impossible) to provide a generic solution for the client
|
650
|
+
code. We also don't want to tell you what's the rigth way to do it
|
651
|
+
(what gems to use, how the API should look like etc.).
|
652
|
+
|
653
|
+
Therefore you can't generate a client code directly by a rake task in
|
654
|
+
further versions.
|
655
|
+
|
656
|
+
There is, however, even better and more flexible way to reuse your API
|
657
|
+
documentation for this purpose: using the API the Apipie
|
658
|
+
provides in the generator code. You can inspire by
|
659
|
+
`Foreman API bindings <https://github.com/mbacovsky/foreman_api>`_ that
|
660
|
+
use exactly this approach. You also don't need to run the service,
|
661
|
+
provided it uses Apipie as a backend.
|
662
|
+
|
663
|
+
And if you write one on your own, don't hesitate to share it with us!
|
664
|
+
|
665
|
+
====================
|
666
|
+
Disqus Integration
|
667
|
+
====================
|
668
|
+
|
669
|
+
You can get a `Disqus <http://www.disqus.com>`_ discussion for the
|
670
|
+
right into your documentation. Just set the credentials in Apipie
|
671
|
+
configuration:
|
672
|
+
|
673
|
+
.. code:: ruby
|
674
|
+
|
675
|
+
config.disqus_shortname = "MyProjectDoc"
|
676
|
+
|
677
|
+
=====================
|
678
|
+
External References
|
679
|
+
=====================
|
680
|
+
|
681
|
+
* `Getting started tutorial <https://github.com/iNecas/apipie-demo>`_ -
|
682
|
+
including examples of using the tests integration and versioning.
|
683
|
+
|
684
|
+
* `Real-world application usage <https://github.com/Katello/katello>`_
|
685
|
+
|
686
|
+
* `Read-world application usage with versioning <https://github.com/theforeman/foreman>`_
|
687
|
+
|
688
|
+
* `Using Apipie API to generate bindings <https://github.com/mbacovsky/foreman_api>`_
|