swagger-ui 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -0
- data/Gemfile.lock +53 -0
- data/LICENSE.txt +20 -0
- data/README.md +54 -0
- data/Rakefile +25 -0
- data/VERSION +1 -0
- data/examples/index.html.haml +35 -0
- data/lib/generators/swagger/install/install_generator.rb +10 -0
- data/lib/swagger/api.rb +24 -0
- data/lib/swagger/document.rb +23 -0
- data/lib/swagger/operation.rb +61 -0
- data/lib/swagger/version.rb +3 -0
- data/lib/swagger.rb +13 -0
- data/public/javascripts/swagger.js +3943 -0
- data/public/stylesheets/swagger.css +1371 -0
- data/swagger-ui.gemspec +59 -0
- data/swagger.gemspec +57 -0
- metadata +100 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionpack (3.2.11)
|
5
|
+
activemodel (= 3.2.11)
|
6
|
+
activesupport (= 3.2.11)
|
7
|
+
builder (~> 3.0.0)
|
8
|
+
erubis (~> 2.7.0)
|
9
|
+
journey (~> 1.0.4)
|
10
|
+
rack (~> 1.4.0)
|
11
|
+
rack-cache (~> 1.2)
|
12
|
+
rack-test (~> 0.6.1)
|
13
|
+
sprockets (~> 2.2.1)
|
14
|
+
activemodel (3.2.11)
|
15
|
+
activesupport (= 3.2.11)
|
16
|
+
builder (~> 3.0.0)
|
17
|
+
activesupport (3.2.11)
|
18
|
+
i18n (~> 0.6)
|
19
|
+
multi_json (~> 1.0)
|
20
|
+
builder (3.0.4)
|
21
|
+
erubis (2.7.0)
|
22
|
+
git (1.2.5)
|
23
|
+
hike (1.2.1)
|
24
|
+
i18n (0.6.1)
|
25
|
+
jeweler (1.8.4)
|
26
|
+
bundler (~> 1.0)
|
27
|
+
git (>= 1.2.5)
|
28
|
+
rake
|
29
|
+
rdoc
|
30
|
+
journey (1.0.4)
|
31
|
+
json (1.7.6)
|
32
|
+
multi_json (1.5.0)
|
33
|
+
rack (1.4.4)
|
34
|
+
rack-cache (1.2)
|
35
|
+
rack (>= 0.4)
|
36
|
+
rack-test (0.6.2)
|
37
|
+
rack (>= 1.0)
|
38
|
+
rake (10.0.3)
|
39
|
+
rdoc (3.12)
|
40
|
+
json (~> 1.4)
|
41
|
+
sprockets (2.2.2)
|
42
|
+
hike (~> 1.2)
|
43
|
+
multi_json (~> 1.0)
|
44
|
+
rack (~> 1.0)
|
45
|
+
tilt (~> 1.1, != 1.3.0)
|
46
|
+
tilt (1.3.3)
|
47
|
+
|
48
|
+
PLATFORMS
|
49
|
+
ruby
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
actionpack
|
53
|
+
jeweler
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Justin Dell
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Swagger
|
2
|
+
|
3
|
+
Rails wrapper for the [Swagger-UI](https://github.com/wordnik/swagger-ui, 'Swagger') API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'swagger-ui', :require => 'swagger'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Then run the generator:
|
16
|
+
|
17
|
+
$ rails generate swagger:install
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Check out index.html.haml in the examples for basic setup. A docs controller and routes will also be required. Fill out the api in the docs controller. Example:
|
22
|
+
|
23
|
+
class Api::DocsController < Api::ApiController
|
24
|
+
include Swagger
|
25
|
+
|
26
|
+
def index
|
27
|
+
document = {
|
28
|
+
"swaggerVersion" => "1.1",
|
29
|
+
"basePath" => api_docs_url,
|
30
|
+
"apis" => [
|
31
|
+
{"path" => "/foo",
|
32
|
+
"description" => ""}]
|
33
|
+
}
|
34
|
+
render :json => document
|
35
|
+
end
|
36
|
+
|
37
|
+
def foo
|
38
|
+
document(api_url, :resourcePath => 'foos') do |doc|
|
39
|
+
doc.api('/foos') do |api|
|
40
|
+
api.operations(['GET','POST']) do |op|
|
41
|
+
op.param('param', :required => true)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "swagger-ui"
|
18
|
+
gem.homepage = "http://github.com/justindell/swagger"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Swagger UI}
|
21
|
+
gem.description = %Q{Swagger UI wrapper}
|
22
|
+
gem.email = "dell.justin@gmail.com"
|
23
|
+
gem.authors = ["Justin Dell", "Brian Tatnall"]
|
24
|
+
end
|
25
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,35 @@
|
|
1
|
+
%html
|
2
|
+
%head
|
3
|
+
%link{ :href => '/stylesheets/swagger.css', :media => 'screen', :rel => 'stylesheet', :type => 'text/css' }
|
4
|
+
%script{ :src => '/javascripts/swagger.js', :type => 'text/javascript'}
|
5
|
+
|
6
|
+
%body
|
7
|
+
%div#message-bar.swagger-ui-wrap
|
8
|
+
%div#swagger-ui-container.swagger-ui-wrap
|
9
|
+
|
10
|
+
%script
|
11
|
+
! $(function () {
|
12
|
+
! window.swaggerUi = new SwaggerUi({
|
13
|
+
! discoveryUrl:"http://localhost:3000/api/docs",
|
14
|
+
! apiKey:"",
|
15
|
+
! dom_id:"swagger-ui-container",
|
16
|
+
! supportHeaderParams: false,
|
17
|
+
! supportedSubmitMethods: ['get', 'post'],
|
18
|
+
! onComplete: function(swaggerApi, swaggerUi){
|
19
|
+
! if(console) {
|
20
|
+
! console.log("Loaded SwaggerUI")
|
21
|
+
! console.log(swaggerApi);
|
22
|
+
! console.log(swaggerUi);
|
23
|
+
! }
|
24
|
+
! $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
|
25
|
+
! },
|
26
|
+
! onFailure: function(data) {
|
27
|
+
! if(console) {
|
28
|
+
! console.log("Unable to Load SwaggerUI");
|
29
|
+
! console.log(data);
|
30
|
+
! }
|
31
|
+
! },
|
32
|
+
! docExpansion: "none"
|
33
|
+
! });
|
34
|
+
! window.swaggerUi.load();
|
35
|
+
! });
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Swagger
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path('../../../../../', __FILE__)
|
4
|
+
|
5
|
+
def copy_files
|
6
|
+
template 'public/stylesheets/swagger.css', 'public/stylesheets/swagger.css'
|
7
|
+
template 'public/javascripts/swagger.js', 'public/javascripts/swagger.js'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/swagger/api.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Swagger
|
2
|
+
class Api
|
3
|
+
def initialize path, opts
|
4
|
+
defaults = {
|
5
|
+
:path => path,
|
6
|
+
:operations => []}
|
7
|
+
@values = defaults.merge opts
|
8
|
+
end
|
9
|
+
|
10
|
+
def path
|
11
|
+
@values[:path]
|
12
|
+
end
|
13
|
+
|
14
|
+
def operations http_methods, opts = {}
|
15
|
+
operations = http_methods.map { |m| Operation.new self, m, opts }
|
16
|
+
operations.each { |o| yield(o) }
|
17
|
+
@values[:operations].concat operations
|
18
|
+
end
|
19
|
+
|
20
|
+
def as_json options
|
21
|
+
@values.as_json options
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Swagger
|
2
|
+
class Document
|
3
|
+
def initialize base_path, opts = {}
|
4
|
+
defaults = {
|
5
|
+
:swaggerVersion => "1.1",
|
6
|
+
:basePath => base_path,
|
7
|
+
:apis => [],
|
8
|
+
}
|
9
|
+
@values = defaults.merge opts
|
10
|
+
end
|
11
|
+
|
12
|
+
def api path, opts = {}
|
13
|
+
a = Api.new(path, opts)
|
14
|
+
yield(a)
|
15
|
+
@values[:apis] << a
|
16
|
+
a
|
17
|
+
end
|
18
|
+
|
19
|
+
def as_json options
|
20
|
+
@values.as_json options
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Swagger
|
2
|
+
class Operation
|
3
|
+
include ActionView::Helpers::TextHelper
|
4
|
+
HTTP_METHODS = ["GET","POST","PUT","DELETE"]
|
5
|
+
ALLOWABLE_VALUES_VALUE_TYPES = ["LIST", "RANGE"]
|
6
|
+
CONTAINERS = ['List', 'Set', 'Array']
|
7
|
+
PRIMITIVES = ['byte', 'boolean', 'int', 'long', 'float', 'double', 'string', 'Date']
|
8
|
+
PARAM_TYPES = {
|
9
|
+
'body' => {:types => ['complex', 'container'],
|
10
|
+
:allowMultipleAllowed => false},
|
11
|
+
'path' => {:types => PRIMITIVES,
|
12
|
+
:allowMultipleAllowed => false},
|
13
|
+
'query' => {:types => PRIMITIVES,
|
14
|
+
:allowMultipleAllowed => true},
|
15
|
+
'header' => {:types => PRIMITIVES,
|
16
|
+
:allowMultipleAllowed => false}
|
17
|
+
}
|
18
|
+
|
19
|
+
def initialize api, http_method, opts = {}
|
20
|
+
bomb("#{http_method} not valid. httpMethod must be one of: #{HTTP_METHODS}") unless HTTP_METHODS.include?(http_method)
|
21
|
+
defaults = {
|
22
|
+
:nickname => api.path.split('/').last,
|
23
|
+
:httpMethod => http_method,
|
24
|
+
:parameters => [],
|
25
|
+
:summary => ""
|
26
|
+
}
|
27
|
+
values = defaults.merge opts
|
28
|
+
values[:notes] = simple_format(values[:notes]) if values[:notes]
|
29
|
+
@values = values
|
30
|
+
end
|
31
|
+
|
32
|
+
def param name, opts = {}
|
33
|
+
defaults = {
|
34
|
+
:name => name,
|
35
|
+
:description => name,
|
36
|
+
:required => false,
|
37
|
+
:allowMultiple => false,
|
38
|
+
:dataType => "string",
|
39
|
+
:paramType => "query"
|
40
|
+
}
|
41
|
+
if list = opts.delete(:list)
|
42
|
+
defaults.merge!(:allowableValues => {:values => list,
|
43
|
+
:valueType => "LIST"})
|
44
|
+
end
|
45
|
+
if range = opts.delete(:range)
|
46
|
+
defaults.merge!(:allowableValues => {:min => range.first,
|
47
|
+
:max => range.last,
|
48
|
+
:valueType => "RANGE"})
|
49
|
+
end
|
50
|
+
result = defaults.merge(opts)
|
51
|
+
bomb("#{result[:dataType]} not valid. dataType must be one of #{PRIMITIVES.inspect}") unless PRIMITIVES.include?(result[:dataType])
|
52
|
+
bomb("#{result[:paramType]} not valid. paramType must be one of #{PARAM_TYPES.keys.inspect}") unless PARAM_TYPES.keys.include?(result[:paramType])
|
53
|
+
bomb("allowMultiple not allowed for paramType #{result[:paramType]}") if result[:allowMultiple] && !PARAM_TYPES[result[:paramType]][:allowMultipleAllowed]
|
54
|
+
@values[:parameters] << result
|
55
|
+
end
|
56
|
+
|
57
|
+
def as_json options
|
58
|
+
@values.as_json options
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/swagger.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'action_view'
|
3
|
+
require "swagger/version"
|
4
|
+
require 'swagger/document'
|
5
|
+
require 'swagger/api'
|
6
|
+
require 'swagger/operation'
|
7
|
+
|
8
|
+
module Swagger
|
9
|
+
def document base_path, opts = {}
|
10
|
+
d = Document.new(base_path, opts).tap { |d| yield(d) }
|
11
|
+
render :json => d
|
12
|
+
end
|
13
|
+
end
|