grape-swagger 0.7.2 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +33 -0
- data/.rubocop.yml +36 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +90 -0
- data/CONTRIBUTING.md +126 -0
- data/Gemfile +1 -21
- data/LICENSE.txt +1 -1
- data/README.md +397 -0
- data/RELEASING.md +80 -0
- data/Rakefile +6 -23
- data/UPGRADING.md +47 -0
- data/grape-swagger.gemspec +26 -84
- data/lib/grape-swagger.rb +237 -111
- data/lib/grape-swagger/errors.rb +9 -0
- data/lib/grape-swagger/markdown.rb +23 -0
- data/lib/grape-swagger/markdown/kramdown_adapter.rb +37 -0
- data/lib/grape-swagger/markdown/redcarpet_adapter.rb +89 -0
- data/lib/grape-swagger/version.rb +3 -0
- data/spec/api_description_spec.rb +41 -0
- data/spec/api_global_models_spec.rb +76 -0
- data/spec/api_models_spec.rb +190 -93
- data/spec/default_api_spec.rb +31 -36
- data/spec/form_params_spec.rb +56 -53
- data/spec/grape-swagger_helper_spec.rb +88 -49
- data/spec/grape-swagger_spec.rb +7 -5
- data/spec/hide_api_spec.rb +58 -55
- data/spec/markdown/kramdown_adapter_spec.rb +38 -0
- data/spec/markdown/markdown_spec.rb +27 -0
- data/spec/markdown/redcarpet_adapter_spec.rb +81 -0
- data/spec/namespaced_api_spec.rb +47 -0
- data/spec/non_default_api_spec.rb +372 -222
- data/spec/response_model_spec.rb +80 -0
- data/spec/simple_mounted_api_spec.rb +113 -118
- data/spec/spec_helper.rb +0 -1
- data/spec/version_spec.rb +8 -0
- data/test/api.rb +62 -0
- data/test/config.ru +10 -2
- data/test/splines.png +0 -0
- metadata +145 -91
- data/.rvmrc +0 -48
- data/CHANGELOG.markdown +0 -55
- data/Gemfile.lock +0 -94
- data/README.markdown +0 -168
- data/VERSION +0 -1
- data/test/nested_api.rb +0 -30
data/Gemfile.lock
DELETED
@@ -1,94 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
activesupport (3.2.13)
|
5
|
-
i18n (= 0.6.1)
|
6
|
-
multi_json (~> 1.0)
|
7
|
-
backports (3.3.2)
|
8
|
-
builder (3.2.2)
|
9
|
-
coderay (1.0.7)
|
10
|
-
descendants_tracker (0.0.1)
|
11
|
-
diff-lcs (1.1.3)
|
12
|
-
ffi (1.9.3-java)
|
13
|
-
git (1.2.5)
|
14
|
-
grape (0.5.0)
|
15
|
-
activesupport
|
16
|
-
builder
|
17
|
-
hashie (>= 1.2.0)
|
18
|
-
multi_json (>= 1.3.2)
|
19
|
-
multi_xml (>= 0.5.2)
|
20
|
-
rack (>= 1.3.0)
|
21
|
-
rack-accept
|
22
|
-
rack-mount
|
23
|
-
virtus
|
24
|
-
grape-entity (0.3.0)
|
25
|
-
activesupport
|
26
|
-
multi_json (>= 1.3.2)
|
27
|
-
hashie (2.0.5)
|
28
|
-
i18n (0.6.1)
|
29
|
-
jeweler (1.8.4)
|
30
|
-
bundler (~> 1.0)
|
31
|
-
git (>= 1.2.5)
|
32
|
-
rake
|
33
|
-
rdoc
|
34
|
-
json (1.7.3)
|
35
|
-
json (1.7.3-java)
|
36
|
-
kramdown (1.3.1)
|
37
|
-
method_source (0.8)
|
38
|
-
multi_json (1.7.7)
|
39
|
-
multi_xml (0.5.4)
|
40
|
-
pry (0.9.10)
|
41
|
-
coderay (~> 1.0.5)
|
42
|
-
method_source (~> 0.8)
|
43
|
-
slop (~> 3.3.1)
|
44
|
-
pry (0.9.10-java)
|
45
|
-
coderay (~> 1.0.5)
|
46
|
-
method_source (~> 0.8)
|
47
|
-
slop (~> 3.3.1)
|
48
|
-
spoon (~> 0.0)
|
49
|
-
rack (1.5.2)
|
50
|
-
rack-accept (0.4.5)
|
51
|
-
rack (>= 0.4)
|
52
|
-
rack-mount (0.8.3)
|
53
|
-
rack (>= 1.0.0)
|
54
|
-
rack-test (0.6.1)
|
55
|
-
rack (>= 1.0)
|
56
|
-
rake (0.9.2.2)
|
57
|
-
rdoc (3.12)
|
58
|
-
json (~> 1.4)
|
59
|
-
rspec (2.11.0)
|
60
|
-
rspec-core (~> 2.11.0)
|
61
|
-
rspec-expectations (~> 2.11.0)
|
62
|
-
rspec-mocks (~> 2.11.0)
|
63
|
-
rspec-core (2.11.1)
|
64
|
-
rspec-expectations (2.11.1)
|
65
|
-
diff-lcs (~> 1.1.3)
|
66
|
-
rspec-mocks (2.11.1)
|
67
|
-
shoulda (3.1.1)
|
68
|
-
shoulda-context (~> 1.0)
|
69
|
-
shoulda-matchers (~> 1.2)
|
70
|
-
shoulda-context (1.0.0)
|
71
|
-
shoulda-matchers (1.2.0)
|
72
|
-
activesupport (>= 3.0.0)
|
73
|
-
slop (3.3.2)
|
74
|
-
spoon (0.0.4)
|
75
|
-
ffi
|
76
|
-
virtus (0.5.5)
|
77
|
-
backports (~> 3.3)
|
78
|
-
descendants_tracker (~> 0.0.1)
|
79
|
-
|
80
|
-
PLATFORMS
|
81
|
-
java
|
82
|
-
ruby
|
83
|
-
|
84
|
-
DEPENDENCIES
|
85
|
-
bundler (> 1.0.0)
|
86
|
-
grape (>= 0.2.0)
|
87
|
-
grape-entity (>= 0.3.0)
|
88
|
-
jeweler (~> 1.8.4)
|
89
|
-
kramdown (>= 1.3.1)
|
90
|
-
pry
|
91
|
-
rack-test
|
92
|
-
rdoc (~> 3.12)
|
93
|
-
rspec
|
94
|
-
shoulda
|
data/README.markdown
DELETED
@@ -1,168 +0,0 @@
|
|
1
|
-
# grape-swagger
|
2
|
-
|
3
|
-
## What is grape-swagger?
|
4
|
-
grape-swagger provides an autogenerated documentation for your [grape](https://github.com/intridea/grape)-API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in [Swagger UI](https://github.com/wordnik/swagger-ui)
|
5
|
-
|
6
|
-
## Related projects
|
7
|
-
* [Grape](https://github.com/intridea/grape)
|
8
|
-
* [Swagger UI](https://github.com/wordnik/swagger-ui)
|
9
|
-
|
10
|
-
## Installation
|
11
|
-
grape-swagger is available as a gem, install it simply via the commandline:
|
12
|
-
|
13
|
-
```gem install grape-swagger```
|
14
|
-
|
15
|
-
or add to your Gemfile:
|
16
|
-
|
17
|
-
```gem 'grape-swagger'```
|
18
|
-
|
19
|
-
## Usage
|
20
|
-
Once you have the gem installed, mount all your different APIs (with ```Grape::API``` superclass) on a root node. In the root class definition, also include ```add_swagger_documentation```, this sets up the system and registers the documentation on '/swagger_doc.json'. Setup done, you can restart your local server now.
|
21
|
-
|
22
|
-
|
23
|
-
``` ruby
|
24
|
-
require 'grape-swagger'
|
25
|
-
|
26
|
-
module API
|
27
|
-
class Root < Grape::API
|
28
|
-
mount API::Cats
|
29
|
-
mount API::Dogs
|
30
|
-
mount API::Pirates
|
31
|
-
add_swagger_documentation
|
32
|
-
end
|
33
|
-
end
|
34
|
-
```
|
35
|
-
|
36
|
-
To explore your API, either download [Swagger UI](https://github.com/wordnik/swagger-ui) and set it up yourself or go to the [online swagger demo](http://petstore.swagger.wordnik.com/) and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc.json).
|
37
|
-
If you use the online demo, make sure your API supports foreign requests by enabling CORS in grape, otherwise you'll see the API description, but requests on the API won't return. You can do this by putting below code in your Grape API definition:
|
38
|
-
|
39
|
-
```` ruby
|
40
|
-
before do
|
41
|
-
header['Access-Control-Allow-Origin'] = '*'
|
42
|
-
header['Access-Control-Request-Method'] = '*'
|
43
|
-
end
|
44
|
-
````
|
45
|
-
|
46
|
-
## Configure
|
47
|
-
You can pass a hash with some configuration possibilities to ```add_swagger_documentation```, all of these are optional:
|
48
|
-
* ```:mount_path``` The path were the API documentation is loaded, default '/swagger_doc'
|
49
|
-
* ```:api_version``` Version of the API that's being exposed
|
50
|
-
* ```:base_path``` Basepath of the API that's being exposed, this configuration parameter accepts a Proc to evaluate base_path, useful when you need to use request attributes to determine the base_path.
|
51
|
-
* ```:markdown``` Allow markdown in `notes`, default `false`
|
52
|
-
* ```:hide_documentation_path``` Don't show the '/swagger_doc' path in the generated swagger documentation
|
53
|
-
|
54
|
-
|
55
|
-
## Swagger Header Parameters
|
56
|
-
|
57
|
-
Swagger also supports the documentation of parameters passed in the header. Since grape's ```params[]``` doesn't return header parameters we can specify header parameters seperately in a block after the description.
|
58
|
-
|
59
|
-
``` ruby
|
60
|
-
desc "Return super-secret information", {
|
61
|
-
headers: {
|
62
|
-
"XAuthToken" => {
|
63
|
-
description: "Valdates your identity",
|
64
|
-
required: true
|
65
|
-
},
|
66
|
-
"XOptionalHeader" => {
|
67
|
-
description: "Not reallly needed",
|
68
|
-
required: false
|
69
|
-
}
|
70
|
-
}
|
71
|
-
}
|
72
|
-
```
|
73
|
-
|
74
|
-
## Hiding an endpoint
|
75
|
-
|
76
|
-
You can hide an endpoint by adding ```:hidden => true``` in the description of the endpoint:
|
77
|
-
|
78
|
-
``` ruby
|
79
|
-
desc 'Hide this endpoint', {
|
80
|
-
:hidden => true
|
81
|
-
}
|
82
|
-
```
|
83
|
-
|
84
|
-
## Grape Entities
|
85
|
-
|
86
|
-
Add the [grape-entity](https://github.com/agileanimal/grape-entity) gem to our Gemfile.
|
87
|
-
Please refer to the [grape-entity documentation](https://github.com/gileanimal/grape-entity/blob/master/README.markdown)
|
88
|
-
for more details.
|
89
|
-
|
90
|
-
The following example exposes statuses. And exposes statuses documentation adding :type and :desc.
|
91
|
-
|
92
|
-
```ruby
|
93
|
-
module API
|
94
|
-
|
95
|
-
module Entities
|
96
|
-
class Status < Grape::Entity
|
97
|
-
expose :text, :documentation => { :type => "string", :desc => "Status update text." }
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
class Statuses < Grape::API
|
102
|
-
version 'v1'
|
103
|
-
|
104
|
-
desc 'Statuses index', {
|
105
|
-
:entity => API::Entities::Status
|
106
|
-
}
|
107
|
-
get '/statuses' do
|
108
|
-
statuses = Status.all
|
109
|
-
type = current_user.admin? ? :full : :default
|
110
|
-
present statuses, with: API::Entities::Status, :type => type
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
```
|
115
|
-
|
116
|
-
## Swagger additions
|
117
|
-
grape-swagger allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Swagger UI. The default Swagger UI doesn't allow HTML in the notes field, so you need to use an adapted version of Swagger UI (you can find one at https://github.com/tim-vandecasteele/swagger-ui/tree/vasco).
|
118
|
-
|
119
|
-
We're using [kramdown](http://kramdown.rubyforge.org) for parsing the markdown, specific syntax can be found [here](http://kramdown.rubyforge.org/syntax.html).
|
120
|
-
|
121
|
-
Be sure to enable markdown in the `add_swagger_documentation` call: ':markdown => true'
|
122
|
-
|
123
|
-
``` ruby
|
124
|
-
desc "Reserve a virgin in heaven", {
|
125
|
-
:notes => <<-NOTE
|
126
|
-
Virgins in heaven
|
127
|
-
-----------------
|
128
|
-
|
129
|
-
> A virgin doesn't come for free
|
130
|
-
|
131
|
-
If you want to reserve a virgin in heaven, you have to do
|
132
|
-
some crazy stuff on earth.
|
133
|
-
|
134
|
-
def do_good
|
135
|
-
puts 'help people'
|
136
|
-
end
|
137
|
-
|
138
|
-
* _Will go to Heaven:_ Probably
|
139
|
-
* _Will go to Hell:_ Probably not
|
140
|
-
NOTE
|
141
|
-
}
|
142
|
-
```
|
143
|
-
|
144
|
-
You can also document the HTTP status codes that your API returns with this syntax:
|
145
|
-
|
146
|
-
``` ruby
|
147
|
-
get '/', :http_codes => [
|
148
|
-
[400, "Invalid parameter entry"],
|
149
|
-
] do
|
150
|
-
...
|
151
|
-
end
|
152
|
-
```
|
153
|
-
|
154
|
-
## Contributing to grape-swagger
|
155
|
-
|
156
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
157
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
158
|
-
* Fork the project.
|
159
|
-
* Start a feature/bugfix branch.
|
160
|
-
* Commit and push until you are happy with your contribution.
|
161
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
162
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
163
|
-
|
164
|
-
## Copyright
|
165
|
-
|
166
|
-
Copyright (c) 2012 Tim Vandecasteele. See LICENSE.txt for
|
167
|
-
further details.
|
168
|
-
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.7.2
|
data/test/nested_api.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'grape'
|
2
|
-
require '../lib/grape-swagger'
|
3
|
-
|
4
|
-
class SimpleNestedApi < Grape::API
|
5
|
-
desc "Nested root"
|
6
|
-
get 'index' do
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
class SimpleMountedApi < Grape::API
|
11
|
-
mount SimpleNestedApi => '/nested'
|
12
|
-
desc "Document root"
|
13
|
-
get 'index' do
|
14
|
-
end
|
15
|
-
|
16
|
-
add_swagger_documentation mount_path: '/simple/swagger_doc'
|
17
|
-
end
|
18
|
-
|
19
|
-
class OtherSimpleMountedApi < Grape::API
|
20
|
-
desc "other simple mounted api root"
|
21
|
-
get 'index' do
|
22
|
-
end
|
23
|
-
|
24
|
-
add_swagger_documentation mount_path: '/other_simple/swagger_doc'
|
25
|
-
end
|
26
|
-
|
27
|
-
class SimpleApi < Grape::API
|
28
|
-
mount SimpleMountedApi => '/api'
|
29
|
-
mount OtherSimpleMountedApi => '/other_api'
|
30
|
-
end
|