api_canon 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +83 -11
- data/Rakefile +11 -15
- data/lib/api_canon/documented_action.rb +56 -6
- data/lib/api_canon/version.rb +1 -1
- metadata +45 -23
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f4993fa3f12d27e8ff54ca9ee6becafa5bb6080
|
4
|
+
data.tar.gz: 31e54ec37b2a8715bd0d8c31729734cc34d816ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a851685829187972e5b14c379846f9a1fb42c587b16c52edccf46b79d67d5d61c3cd21653cb70948d07429cb1fc0ec133489462bdc41a7a074670d1613fe1880
|
7
|
+
data.tar.gz: 6d67a86a1019e78a56a566bdac9c5790ec63edfd958e95ee571526bd1814505cb033c23b71f247b1ee8a2841cd57630a8fc2bdaf2b27c309d65775b4e18e8d68
|
data/README.md
CHANGED
@@ -8,29 +8,101 @@ API Canon is a tool for programatically documenting Ruby on Rails APIs with exam
|
|
8
8
|
## Installation and usage
|
9
9
|
If you're using bundler, then put this in your Gemfile:
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
```ruby
|
12
|
+
gem 'api_canon'
|
13
|
+
```
|
13
14
|
Then, in each controller you want to document, add the line
|
14
15
|
|
15
|
-
|
16
|
+
```ruby
|
17
|
+
include ApiCanon
|
18
|
+
```
|
16
19
|
|
17
20
|
... which allows you to describe what all the actions in the controller are concerned about like this:
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
+
```ruby
|
23
|
+
document_controller :as => 'optional_rename' do
|
24
|
+
describe "The actions here are awesome, they allow you to get a list of awesome things, and make awesome things, too!"
|
25
|
+
end
|
26
|
+
```
|
22
27
|
|
23
|
-
|
28
|
+
That is optional, but reccomended, as it gives context to users of your API.
|
24
29
|
|
25
|
-
|
26
|
-
|
27
|
-
|
30
|
+
More usefully you can document all the actions you want like this:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
document_method :index do
|
34
|
+
param :category_codes, :type => :array, :multiple => true, :example_values => Category.all(:limit => 5, :select => :code).map(&:code), :description => "Return only categories for the given category codes", :default => 'some-awesome-category-code'
|
35
|
+
end
|
36
|
+
```
|
28
37
|
|
29
38
|
To view the api documentation, visit the documented controller's index action with '.html' as the format.
|
30
39
|
|
31
40
|
To enable the 'test' button on the generated documentation pages, you'll need to add this to your config/routes.rb file:
|
32
41
|
|
33
|
-
|
42
|
+
```ruby
|
43
|
+
ApiCanon::Routes.draw(self) # Or 'map' instead of 'self' for Rails 2
|
44
|
+
```
|
45
|
+
|
46
|
+
## Examples
|
47
|
+
|
48
|
+
### Standard Rails actions
|
49
|
+
|
50
|
+
If you have an index action, you should render api_canon documentation when params[:format] is html. For example:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class CategoriesController < ApplicationController
|
54
|
+
include ApiCanon
|
55
|
+
|
56
|
+
|
57
|
+
document_method :index do
|
58
|
+
describe "This gives you a bunch of categories."
|
59
|
+
param :node, :type => :string, :values => ['womens-fashion', 'mens-fashion'], :default => 'womens-fashion', :description => "Category code to start with"
|
60
|
+
param :depth, :type => :integer, :values => 1..4, :default => 1, :description => "Maximum depth to include child categories"
|
61
|
+
end
|
62
|
+
def index
|
63
|
+
# Do stuff.
|
64
|
+
respond_to do |format|
|
65
|
+
format.html { render :layout => 'api_canon'} # Defaults to api_canon index
|
66
|
+
format.json { render :json => @some_list_of_objects }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
### Using inherited_resources
|
73
|
+
|
74
|
+
It's a little easier with InheritedResources.
|
75
|
+
Simply include ApiCanon after you call inherit_resources.
|
76
|
+
It will create an index action that renders the documentation if params[:format]
|
77
|
+
is blank or :html, and defaults back to the inherited_resources index action
|
78
|
+
otherwise.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class FunkyCategoriesController < ApplicationController
|
82
|
+
inherit_resources
|
83
|
+
respond_to :json, :xml
|
84
|
+
actions :index, :show
|
85
|
+
|
86
|
+
include ApiCanon
|
87
|
+
|
88
|
+
document_controller :as => 'Categories' do
|
89
|
+
describe "Categories are used for filtering products. They are hierarchical, with 4 levels. These 4 levels are known as Super-Categories, Categories, Sub-Categories and Types. Examples include \"Women's Fashion\", \"Shirts\" and so forth. They are uniquely identifiable by their category_code field."
|
90
|
+
end
|
91
|
+
|
92
|
+
document_method :index do
|
93
|
+
describe "This action returns a filtered tree of categories based on the parameters given in the request."
|
94
|
+
param :hierarchy_level, :values => 1..4, :type => :integer, :default => 1, :description => "Maximum depth to include child categories"
|
95
|
+
param :category_codes, :type => :array, :multiple => true, :example_values => Category.online.enabled.super_categories.all(:limit => 5, :select => :code).map(&:code), :description => "Return only categories for the given category codes", :default => 'mens-fashion-accessories'
|
96
|
+
end
|
97
|
+
|
98
|
+
document_method :show do
|
99
|
+
describe "This action returns a tree of categories starting at the requested root node."
|
100
|
+
param :id, :type => :string, :example_values => Category.online.enabled.super_categories.all(:limit => 5, :select => :code).map(&:code), :description => "Category code to show, the root node for the entire tree.", :default => 'mens-fashion-accessories'
|
101
|
+
end
|
102
|
+
|
103
|
+
#... code to support the above documented parameters etc.
|
104
|
+
end
|
105
|
+
```
|
34
106
|
|
35
107
|
## Going forward
|
36
108
|
|
data/Rakefile
CHANGED
@@ -4,25 +4,21 @@ begin
|
|
4
4
|
rescue LoadError
|
5
5
|
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
6
|
end
|
7
|
-
begin
|
8
|
-
require 'rdoc/task'
|
9
|
-
rescue LoadError
|
10
|
-
require 'rdoc/rdoc'
|
11
|
-
require 'rake/rdoctask'
|
12
|
-
RDoc::Task = Rake::RDocTask
|
13
|
-
end
|
14
|
-
|
15
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
-
rdoc.rdoc_dir = 'rdoc'
|
17
|
-
rdoc.title = 'ApiCanon'
|
18
|
-
rdoc.options << '--line-numbers'
|
19
|
-
rdoc.rdoc_files.include('README.rdoc')
|
20
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
-
end
|
22
7
|
|
23
8
|
require "bundler/gem_tasks"
|
24
9
|
Bundler::GemHelper.install_tasks
|
25
10
|
|
11
|
+
begin
|
12
|
+
require 'yard'
|
13
|
+
YARD::Rake::YardocTask.new do |t|
|
14
|
+
t.files = ['lib/**/*.rb']
|
15
|
+
t.options = ['--markup-provider=redcarpet',
|
16
|
+
'--markup=markdown',
|
17
|
+
'--no-private',
|
18
|
+
'--hide-void-return']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
26
22
|
require "rspec/core/rake_task"
|
27
23
|
|
28
24
|
RSpec::Core::RakeTask.new
|
@@ -10,16 +10,66 @@ module ApiCanon
|
|
10
10
|
:description => "The requested format of the response."
|
11
11
|
@response_codes={}
|
12
12
|
end
|
13
|
+
# The param method describes and gives examples for the parameters your
|
14
|
+
# API action can take.
|
15
|
+
# @see ApiCanon::DocumentedParam
|
16
|
+
# @see ApiCanon::ClassMethods#document_method
|
17
|
+
# @param param_name [Symbol] The name of the parameter, i.e. the param_name
|
18
|
+
# bit in params[param_name]
|
19
|
+
# @param options [Hash] This is where you describe the given parameter.
|
20
|
+
# Valid keys are:
|
21
|
+
#
|
22
|
+
# * default: The default value to fill in for this parameter
|
23
|
+
# * example_values: Example values to show. Use when the input is unconstrained.
|
24
|
+
# * values: Valid values to use. Use when the input is constrained.
|
25
|
+
# * type: The expected type of the value(s) of this param, e.g. :string
|
26
|
+
# * description: A friendly description of what this parameter does or is used for.
|
27
|
+
# * multiple: Can this parameter be used multiple times? I.e. Array values.
|
28
|
+
#
|
29
|
+
# ##Examples:
|
30
|
+
#
|
31
|
+
# ```ruby
|
32
|
+
# document_method :index do
|
33
|
+
# param :hierarchy_level, :values => 1..4, :type => :integer, :default => 1, :description => "Maximum depth to include child categories"
|
34
|
+
# param :category_codes, :type => :array, :multiple => true, :example_values => Category.online.enabled.super_categories.all(:limit => 5, :select => :code).map(&:code), :description => "Return only categories for the given category codes", :default => 'mens-fashion-accessories'
|
35
|
+
# end
|
36
|
+
# ```
|
37
|
+
#
|
13
38
|
def param(param_name, options={})
|
14
39
|
@params[param_name] = DocumentedParam.new param_name, options
|
15
40
|
end
|
16
|
-
|
17
|
-
|
41
|
+
# The response_code method will be used as a DSL method in the
|
42
|
+
# document_method block to describe what you mean when your action returns
|
43
|
+
# the given response code. It currently does nothing.
|
44
|
+
# @see ApiCanon::ClassMethods#document_method
|
45
|
+
# @param code [Integer] The response code to document, e.g. 200, 404 etc.
|
46
|
+
# @param desc [String] Description of what the response code means in your
|
47
|
+
# case, e.g. a 422 might mean the user entered input that failed validation.
|
48
|
+
# ##Examples:
|
49
|
+
#
|
50
|
+
# ```ruby
|
51
|
+
# document_method :index do
|
52
|
+
# response_code 200, "Everything worked"
|
53
|
+
# response_code 404, "Either the requested product or category could not be found"
|
54
|
+
# end
|
55
|
+
# ```
|
56
|
+
def response_code(code, desc="")
|
57
|
+
@response_codes[code] = desc
|
18
58
|
end
|
19
|
-
# The describe method is used as a DSL method in the
|
20
|
-
#
|
21
|
-
# @see ApiCanon::ClassMethods#
|
22
|
-
# @param desc [String] The text to appear at the top of
|
59
|
+
# The describe method is used as a DSL method in the document_method block,
|
60
|
+
# Use it to describe your API action.
|
61
|
+
# @see ApiCanon::ClassMethods#document_method
|
62
|
+
# @param desc [String] The text to appear at the top of the action block in
|
63
|
+
# the generated API documentation page.
|
64
|
+
#
|
65
|
+
# ##Examples:
|
66
|
+
#
|
67
|
+
# ```ruby
|
68
|
+
# document_method :index do
|
69
|
+
# describe "This action returns a filtered tree of categories based on the parameters given in the request."
|
70
|
+
# end
|
71
|
+
# ```
|
72
|
+
#
|
23
73
|
def describe(desc)
|
24
74
|
@description = desc
|
25
75
|
end
|
data/lib/api_canon/version.rb
CHANGED
metadata
CHANGED
@@ -1,36 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_canon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.5
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Cameron Walsh
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-07 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rails
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 2.3.17
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 2.3.17
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,23 +41,50 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: redcarpet
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: |-
|
84
|
+
api_canon is a declarative documentation generator
|
85
|
+
for APIs. Declare the parameters and response codes,
|
86
|
+
describe them, and give some example values. api_canon
|
87
|
+
handles the rest for you.
|
66
88
|
email:
|
67
89
|
- cameron.walsh@gmail.com
|
68
90
|
executables: []
|
@@ -97,27 +119,26 @@ files:
|
|
97
119
|
homepage: http://github.com/cwalsh/api_canon
|
98
120
|
licenses:
|
99
121
|
- MIT
|
122
|
+
metadata: {}
|
100
123
|
post_install_message:
|
101
124
|
rdoc_options: []
|
102
125
|
require_paths:
|
103
126
|
- lib
|
104
127
|
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
128
|
requirements:
|
107
|
-
- -
|
129
|
+
- - '>='
|
108
130
|
- !ruby/object:Gem::Version
|
109
131
|
version: '0'
|
110
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
-
none: false
|
112
133
|
requirements:
|
113
|
-
- -
|
134
|
+
- - '>='
|
114
135
|
- !ruby/object:Gem::Version
|
115
136
|
version: '0'
|
116
137
|
requirements: []
|
117
138
|
rubyforge_project:
|
118
|
-
rubygems_version:
|
139
|
+
rubygems_version: 2.0.3
|
119
140
|
signing_key:
|
120
|
-
specification_version:
|
141
|
+
specification_version: 4
|
121
142
|
summary: Declarative documentation generator for APIs.
|
122
143
|
test_files:
|
123
144
|
- spec/spec_helper.rb
|
@@ -125,3 +146,4 @@ test_files:
|
|
125
146
|
- spec/lib/api_canon/documented_action_spec.rb
|
126
147
|
- spec/lib/api_canon_spec.rb
|
127
148
|
- spec/dummy/log/test.log
|
149
|
+
has_rdoc:
|