swagger_yard 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +126 -99
- data/Rakefile +6 -22
- data/lib/swagger_yard/api.rb +26 -109
- data/lib/swagger_yard/api_declaration.rb +89 -23
- data/lib/swagger_yard/authorization.rb +36 -0
- data/lib/swagger_yard/configuration.rb +14 -0
- data/lib/swagger_yard/listing_info.rb +15 -0
- data/lib/swagger_yard/model.rb +69 -0
- data/lib/swagger_yard/operation.rb +149 -0
- data/lib/swagger_yard/parameter.rb +62 -3
- data/lib/swagger_yard/property.rb +36 -0
- data/lib/swagger_yard/resource_listing.rb +57 -18
- data/lib/swagger_yard/type.rb +34 -0
- data/lib/swagger_yard/version.rb +1 -1
- data/lib/swagger_yard.rb +35 -78
- metadata +70 -48
- data/app/controllers/swagger_yard/application_controller.rb +0 -4
- data/app/controllers/swagger_yard/swagger_controller.rb +0 -21
- data/app/views/swagger_yard/swagger/doc.html.erb +0 -80
- data/lib/generators/swagger_yard/doc_generator.rb +0 -11
- data/lib/generators/swagger_yard/js_generator.rb +0 -13
- data/lib/swagger_yard/cache.rb +0 -50
- data/lib/swagger_yard/engine.rb +0 -18
- data/lib/swagger_yard/local_dispatcher.rb +0 -51
- data/lib/swagger_yard/parser.rb +0 -35
- data/public/swagger-ui/css/hightlight.default.css +0 -135
- data/public/swagger-ui/css/screen.css +0 -1759
- data/public/swagger-ui/images/logo_small.png +0 -0
- data/public/swagger-ui/images/pet_store_api.png +0 -0
- data/public/swagger-ui/images/throbber.gif +0 -0
- data/public/swagger-ui/images/wordnik_api.png +0 -0
- data/public/swagger-ui/lib/MD5.js +0 -319
- data/public/swagger-ui/lib/backbone-min.js +0 -38
- data/public/swagger-ui/lib/handlebars-1.0.rc.1.js +0 -1920
- data/public/swagger-ui/lib/highlight.7.3.pack.js +0 -1
- data/public/swagger-ui/lib/jquery-1.8.0.min.js +0 -2
- data/public/swagger-ui/lib/jquery.ba-bbq.min.js +0 -18
- data/public/swagger-ui/lib/jquery.slideto.min.js +0 -1
- data/public/swagger-ui/lib/jquery.wiggle.min.js +0 -8
- data/public/swagger-ui/lib/swagger.js +0 -794
- data/public/swagger-ui/lib/underscore-min.js +0 -32
- data/public/swagger-ui/swagger-ui.js +0 -2090
- data/public/swagger-ui/swagger-ui_org.js +0 -2005
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6dfc1b55809fb818e9c2f1742a741ebbf72eb4d
|
4
|
+
data.tar.gz: cb8f8253cb95a55c04d6a9cd5b7488ae6d40b47c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2c1e0eb8a9461f39d3161f62592c8cdf817f0c1fa9a8d3b9fc8231a171ee30246eb84fb74be727ed1671fde7244d740d5e4b774e5f5d0334c913e15270b7182
|
7
|
+
data.tar.gz: d47a31030769ce82fb1a7cb5242899562c229ed023c3155f25be48fd5f91a9d8ab7f9c67587073d645a0958d9b3bec7dec2d23ca55b2cf5dcfcca2ce54d90c21
|
data/README.md
CHANGED
@@ -1,15 +1,9 @@
|
|
1
|
-
SwaggerYard
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
Parsing of the Yardocs happens during the server startup and the data will be subsequently cached to the Rails cache you have defined.
|
8
|
-
If your API is large expect a slow server start up time.
|
9
|
-
|
10
|
-
Installation
|
11
|
-
-----------------
|
12
|
-
|
1
|
+
# SwaggerYard #
|
2
|
+
|
3
|
+
SwaggerYard is a gem to convert extended YARD syntax comments into the swagger spec compliant json format.
|
4
|
+
|
5
|
+
## Installation ##
|
6
|
+
|
13
7
|
Put SwaggerYard in your Gemfile:
|
14
8
|
|
15
9
|
gem 'swagger_yard'
|
@@ -19,95 +13,128 @@ Install the gem with Bunder:
|
|
19
13
|
bundle install
|
20
14
|
|
21
15
|
|
22
|
-
Getting Started
|
23
|
-
|
16
|
+
## Getting Started ##
|
17
|
+
|
18
|
+
### Place your configuration in a your rails initializers ###
|
24
19
|
|
25
|
-
Place your configuration in a your rails initializers
|
26
|
-
|
27
20
|
# config/initializers/swagger_yard.rb
|
28
21
|
SwaggerYard.configure do |config|
|
29
|
-
config.swagger_version = "1.
|
30
|
-
config.api_version = "0
|
31
|
-
config.
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
# config/routes.rb
|
38
|
-
mount SwaggerYard::Engine, at: "/swagger"
|
39
|
-
|
40
|
-
|
41
|
-
Example
|
42
|
-
----------------
|
43
|
-
|
44
|
-
Here is a example of how to use SwaggerYard
|
45
|
-
|
46
|
-
|
47
|
-
# @resource Account ownership
|
48
|
-
#
|
49
|
-
# @resource_path /accounts/ownerships
|
50
|
-
#
|
51
|
-
# This document describes the API for creating, reading, and deleting account ownerships.
|
52
|
-
#
|
53
|
-
class Accounts::OwnershipsController < ActionController::Base
|
54
|
-
##
|
55
|
-
# Returns a list of ownerships associated with the account.
|
56
|
-
#
|
57
|
-
# @notes Status can be -1(Deleted), 0(Inactive), 1(Active), 2(Expired) and 3(Cancelled).
|
58
|
-
#
|
59
|
-
# @path [GET] /accounts/ownerships.{format_type}
|
60
|
-
#
|
61
|
-
# @parameter [Integer] offset Used for pagination of response data (default: 25 items per response). Specifies the offset of the next block of data to receive.
|
62
|
-
# @parameter [Array] status Filter by status. (e.g. status[]=1&status[]=2&status[]=3).
|
63
|
-
# @parameter_list [String] sort_order Orders response by fields. (e.g. sort_order=created_at).
|
64
|
-
# [List] id
|
65
|
-
# [List] begin_at
|
66
|
-
# [List] end_at
|
67
|
-
# [List] created_at
|
68
|
-
# @parameter [Boolean] sort_descending Reverse order of sort_order sorting, make it descending.
|
69
|
-
# @parameter [Date] begin_at_greater Filters response to include only items with begin_at >= specified timestamp (e.g. begin_at_greater=2012-02-15T02:06:56Z).
|
70
|
-
# @parameter [Date] begin_at_less Filters response to include only items with begin_at <= specified timestamp (e.g. begin_at_less=2012-02-15T02:06:56Z).
|
71
|
-
# @parameter [Date] end_at_greater Filters response to include only items with end_at >= specified timestamp (e.g. end_at_greater=2012-02-15T02:06:56Z).
|
72
|
-
# @parameter [Date] end_at_less Filters response to include only items with end_at <= specified timestamp (e.g. end_at_less=2012-02-15T02:06:56Z).
|
73
|
-
#
|
74
|
-
def index
|
75
|
-
...
|
76
|
-
end
|
22
|
+
config.swagger_version = "1.2"
|
23
|
+
config.api_version = "1.0"
|
24
|
+
config.reload = Rails.env.development?
|
25
|
+
|
26
|
+
# where your swagger spec json will show up
|
27
|
+
config.swagger_spec_base_path = "http://localhost:3000/swagger/api"
|
28
|
+
# where your actual api is hosted from
|
29
|
+
config.api_base_path = "http://localhost:3000/api"
|
77
30
|
end
|
78
31
|
|
79
|
-
|
80
|
-
!
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
[
|
107
|
-
[
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
32
|
+
# register swagger tags with YARD
|
33
|
+
SwaggerYard.register_custom_yard_tags!
|
34
|
+
|
35
|
+
## Example Documentation ##
|
36
|
+
|
37
|
+
### Here is an example of how to use SwaggerYard in your Controller ###
|
38
|
+
|
39
|
+
**Note:** Model references should be Capitalized or CamelCased, basic types (integer, boolean, string, etc) should be lowercased everywhere.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# @resource Account ownership
|
43
|
+
#
|
44
|
+
# @resource_path /accounts/ownerships
|
45
|
+
#
|
46
|
+
# This document describes the API for creating, reading, and deleting account ownerships.
|
47
|
+
#
|
48
|
+
class Accounts::OwnershipsController < ActionController::Base
|
49
|
+
##
|
50
|
+
# Returns a list of ownerships associated with the account.
|
51
|
+
#
|
52
|
+
# @notes Status can be -1(Deleted), 0(Inactive), 1(Active), 2(Expired) and 3(Cancelled).
|
53
|
+
#
|
54
|
+
# @path [GET] /accounts/ownerships.{format_type}
|
55
|
+
#
|
56
|
+
# @parameter offset [integer] Used for pagination of response data (default: 25 items per response). Specifies the offset of the next block of data to receive.
|
57
|
+
# @parameter status [array<string>] Filter by status. (e.g. status[]=1&status[]=2&status[]=3).
|
58
|
+
# @parameter_list [String] sort_order Orders response by fields. (e.g. sort_order=created_at).
|
59
|
+
# [List] id
|
60
|
+
# [List] begin_at
|
61
|
+
# [List] end_at
|
62
|
+
# [List] created_at
|
63
|
+
# @parameter sort_descending [boolean] Reverse order of sort_order sorting, make it descending.
|
64
|
+
# @parameter begin_at_greater [date] Filters response to include only items with begin_at >= specified timestamp (e.g. begin_at_greater=2012-02-15T02:06:56Z).
|
65
|
+
# @parameter begin_at_less [date] Filters response to include only items with begin_at <= specified timestamp (e.g. begin_at_less=2012-02-15T02:06:56Z).
|
66
|
+
# @parameter end_at_greater [date] Filters response to include only items with end_at >= specified timestamp (e.g. end_at_greater=2012-02-15T02:06:56Z).
|
67
|
+
# @parameter end_at_less [date] Filters response to include only items with end_at <= specified timestamp (e.g. end_at_less=2012-02-15T02:06:56Z).
|
68
|
+
#
|
69
|
+
def index
|
70
|
+
...
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Returns an ownership for an account by id
|
75
|
+
#
|
76
|
+
# @path [GET] /accounts/ownerships/{id}.{format_type}
|
77
|
+
# @response_type [Ownership]
|
78
|
+
# @error_message [EmptyOwnership] 404 Ownership not found
|
79
|
+
# @error_message 400 Invalid ID supplied
|
80
|
+
#
|
81
|
+
def show
|
82
|
+
...
|
83
|
+
end
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
### Here is an example of how to use SwaggerYard in your Model ###
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
#
|
91
|
+
# @model Pet
|
92
|
+
#
|
93
|
+
# @property id(required) [integer] the identifier for the pet
|
94
|
+
# @property name [Array<string>] the names for the pet
|
95
|
+
# @property age [integer] the age of the pet
|
96
|
+
# @property relatives(required) [Array<Pet>] other Pets in its family
|
97
|
+
#
|
98
|
+
class Pet
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
To then use your `Model` in your `Controller` documentation, add `@parameter`s:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
# @parameter [Pet] pet The pet object
|
106
|
+
```
|
107
|
+
|
108
|
+
## Authorization ##
|
109
|
+
|
110
|
+
Currently, SwaggerYard only supports API Key auth descriptions. Start by adding `@authorization` to your `ApplicationController`.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
#
|
114
|
+
# @authorization [api_key] header X-APPLICATION-API-KEY
|
115
|
+
#
|
116
|
+
class ApplicationController < ActionController::Base
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
Then you can use these authorizations from your controller or actions in a controller. The name comes from either header or query plus the name of the key downcased/underscored.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
#
|
124
|
+
# @authorize_with header_x_application_api_key
|
125
|
+
#
|
126
|
+
class PetController < ApplicationController
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
## UI ##
|
131
|
+
|
132
|
+
It is advisable to use something like [swagger-ui_rails](https://github.com/3scale/swagger-ui_rails/tree/dev-2.1.3) for your UI needs inside of Rails.
|
133
|
+
|
134
|
+
## More Information ##
|
135
|
+
|
136
|
+
* [Swagger-ui](https://github.com/wordnik/swagger-ui)
|
137
|
+
* [swagger-ui_rails](https://github.com/3scale/swagger-ui_rails/tree/dev-2.1.3)
|
138
|
+
* [Yard](https://github.com/lsegal/yard)
|
139
|
+
* [Swagger-spec version 1.2](https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md)
|
140
|
+
* [Swagger-spec version 2.0](https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md)
|
data/Rakefile
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
rescue LoadError
|
5
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
-
end
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
|
7
4
|
begin
|
8
5
|
require 'rdoc/task'
|
9
6
|
rescue LoadError
|
@@ -20,21 +17,8 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
20
17
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
18
|
end
|
22
19
|
|
23
|
-
|
24
|
-
load 'rails/tasks/engine.rake'
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
Bundler::GemHelper.install_tasks
|
29
|
-
|
30
|
-
require 'rake/testtask'
|
31
|
-
|
32
|
-
Rake::TestTask.new(:test) do |t|
|
33
|
-
t.libs << 'lib'
|
34
|
-
t.libs << 'test'
|
35
|
-
t.pattern = 'test/**/*_test.rb'
|
36
|
-
t.verbose = false
|
37
|
-
end
|
20
|
+
require 'rspec/core/rake_task'
|
38
21
|
|
22
|
+
RSpec::Core::RakeTask.new(:spec)
|
39
23
|
|
40
|
-
task :default => :
|
24
|
+
task :default => :spec
|
data/lib/swagger_yard/api.rb
CHANGED
@@ -1,132 +1,49 @@
|
|
1
1
|
module SwaggerYard
|
2
2
|
class Api
|
3
|
-
|
4
|
-
attr_accessor :path, :parameters, :description, :http_method, :response_class, :summary, :notes, :error_responses
|
3
|
+
attr_accessor :path, :description
|
5
4
|
|
6
|
-
def
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
value = tag.text
|
12
|
-
|
13
|
-
case tag.tag_name
|
14
|
-
when "path"
|
15
|
-
parse_path(value)
|
16
|
-
when "parameter"
|
17
|
-
@parameters << parse_parameter(value)
|
18
|
-
when "parameter_list"
|
19
|
-
@parameters << parse_parameter_list(value)
|
20
|
-
when "summary"
|
21
|
-
@summary = value
|
22
|
-
when "notes"
|
23
|
-
@notes = value.gsub("\n", "<br\>")
|
24
|
-
end
|
5
|
+
def self.path_from_yard_object(yard_object)
|
6
|
+
if tag = yard_object.tags.detect {|t| t.tag_name == "path"}
|
7
|
+
tag.text
|
8
|
+
else
|
9
|
+
nil
|
25
10
|
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_yard_object(yard_object, api_declaration)
|
14
|
+
path = path_from_yard_object(yard_object)
|
15
|
+
description = yard_object.docstring
|
26
16
|
|
27
|
-
|
28
|
-
@parameters << add_format_parameters
|
17
|
+
new(path, description, api_declaration)
|
29
18
|
end
|
30
19
|
|
31
|
-
def
|
32
|
-
@
|
20
|
+
def initialize(path, description, api_declaration)
|
21
|
+
@api_declaration = api_declaration
|
22
|
+
@description = description
|
23
|
+
@path = path
|
24
|
+
|
25
|
+
@operations = []
|
33
26
|
end
|
34
27
|
|
35
|
-
def
|
36
|
-
|
37
|
-
"httpMethod" => http_method,
|
38
|
-
"nickname" => path[1..-1].gsub(/[^a-zA-Z\d:]/, '-').squeeze("-") + http_method.downcase,
|
39
|
-
"responseClass" => response_class || "void",
|
40
|
-
"produces" => ["application/json", "application/xml"],
|
41
|
-
"parameters" => parameters,
|
42
|
-
"summary" => summary || description,
|
43
|
-
"notes" => notes,
|
44
|
-
"errorResponses" => error_responses
|
45
|
-
}
|
28
|
+
def add_operation(yard_object)
|
29
|
+
@operations << Operation.from_yard_object(yard_object, self)
|
46
30
|
end
|
47
31
|
|
48
32
|
def to_h
|
49
33
|
{
|
50
34
|
"path" => path,
|
51
35
|
"description" => description,
|
52
|
-
"operations" =>
|
36
|
+
"operations" => @operations.map(&:to_h)
|
53
37
|
}
|
54
38
|
end
|
55
39
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
private
|
61
|
-
##
|
62
|
-
# Example: [GET] /api/v2/ownerships.{format_type}
|
63
|
-
def parse_path(string)
|
64
|
-
@http_method, @path = string.match(/^\[(\w*)\]\s*(.*)$/).captures
|
65
|
-
|
66
|
-
path_params = @path.scan(/\{([^\}]+)\}/).flatten.reject { |value| value == "format_type" }
|
67
|
-
|
68
|
-
path_params.each do |path_param|
|
69
|
-
@parameters << {
|
70
|
-
"paramType" => "path",
|
71
|
-
"name" => path_param,
|
72
|
-
"description" => "Scope response to #{path_param}",
|
73
|
-
"dataType" => "string",
|
74
|
-
"required" => true,
|
75
|
-
"allowMultiple" => false
|
76
|
-
}
|
77
|
-
end
|
40
|
+
def model_names
|
41
|
+
@operations.map(&:model_names).flatten.compact.uniq
|
42
|
+
# @parameters.select {|p| ref?(p['type'])}.map {|p| p['type']}.uniq
|
78
43
|
end
|
79
44
|
|
80
|
-
|
81
|
-
|
82
|
-
# Example: [Array] status(required) Filter by status. (e.g. status[]=1&status[]=2&status[]=3)
|
83
|
-
# Example: [Integer] media[media_type_id] ID of the desired media type.
|
84
|
-
def parse_parameter(string)
|
85
|
-
data_type, name, required, description = string.match(/\A\[(\w*)\]\s*([\w\[\]]*)(\(required\))?\s*(.*)\Z/).captures
|
86
|
-
allow_multiple = name.gsub!("[]", "")
|
87
|
-
|
88
|
-
parameter = {
|
89
|
-
"paramType" => "query",
|
90
|
-
"name" => name,
|
91
|
-
"description" => description,
|
92
|
-
"dataType" => data_type.downcase,
|
93
|
-
"required" => required.present?,
|
94
|
-
"allowMultiple" => allow_multiple.present?
|
95
|
-
}
|
96
|
-
end
|
97
|
-
|
98
|
-
##
|
99
|
-
# Example: [String] sort_order Orders ownerships by fields. (e.g. sort_order=created_at)
|
100
|
-
# [List] id
|
101
|
-
# [List] begin_at
|
102
|
-
# [List] end_at
|
103
|
-
# [List] created_at
|
104
|
-
def parse_parameter_list(string)
|
105
|
-
data_type, name, required, description, set_string = string.match(/\A\[(\w*)\]\s*(\w*)(\(required\))?\s*(.*)\n([.\s\S]*)\Z/).captures
|
106
|
-
|
107
|
-
list_values = set_string.split("[List]").map(&:strip).reject { |string| string.empty? }
|
108
|
-
|
109
|
-
parameter = {
|
110
|
-
"paramType" => "query",
|
111
|
-
"name" => name,
|
112
|
-
"description" => description,
|
113
|
-
"dataType" => data_type.downcase,
|
114
|
-
"required" => required.present?,
|
115
|
-
"allowMultiple" => false,
|
116
|
-
"allowableValues" => {"valueType" => 'LIST', "values" => list_values}
|
117
|
-
}
|
118
|
-
end
|
119
|
-
|
120
|
-
def add_format_parameters
|
121
|
-
@add_format_parameters ||= {
|
122
|
-
"paramType" => "path",
|
123
|
-
"name" => "format_type",
|
124
|
-
"description" => "Response format either JSON or XML",
|
125
|
-
"dataType" => "string",
|
126
|
-
"required" => true,
|
127
|
-
"allowMultiple" => false,
|
128
|
-
"allowableValues" => {"valueType" => "LIST", "values" => ["json", "xml"]}
|
129
|
-
}
|
45
|
+
def ref?(data_type)
|
46
|
+
@api_declaration.ref?(data_type)
|
130
47
|
end
|
131
48
|
end
|
132
49
|
end
|
@@ -1,42 +1,108 @@
|
|
1
1
|
module SwaggerYard
|
2
2
|
class ApiDeclaration
|
3
3
|
attr_accessor :description, :resource_path
|
4
|
+
attr_reader :apis, :authorizations
|
5
|
+
|
6
|
+
def initialize(resource_listing)
|
7
|
+
@resource_listing = resource_listing
|
4
8
|
|
5
|
-
def initialize
|
6
9
|
@apis = {}
|
7
|
-
@
|
10
|
+
@authorizations = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid?
|
14
|
+
!@resource_path.nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def add_yard_objects(yard_objects)
|
18
|
+
yard_objects.each do |yard_object|
|
19
|
+
add_yard_object(yard_object)
|
20
|
+
end
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_yard_object(yard_object)
|
25
|
+
case yard_object.type
|
26
|
+
when :class
|
27
|
+
add_listing_info(ListingInfo.new(yard_object))
|
28
|
+
add_authorizations_to_resource_listing(yard_object)
|
29
|
+
when :method
|
30
|
+
add_api(yard_object)
|
31
|
+
end
|
8
32
|
end
|
9
33
|
|
10
|
-
def add_listing_info(
|
11
|
-
@description =
|
12
|
-
|
13
|
-
|
14
|
-
|
34
|
+
def add_listing_info(listing_info)
|
35
|
+
@description = listing_info.description
|
36
|
+
@resource_path = listing_info.resource_path
|
37
|
+
|
38
|
+
# we only have api_key auth, the value for now is always empty array
|
39
|
+
@authorizations = Hash[listing_info.authorizations.uniq.map {|k| [k, []]}]
|
15
40
|
end
|
16
41
|
|
17
|
-
def add_api(
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
42
|
+
def add_api(yard_object)
|
43
|
+
path = Api.path_from_yard_object(yard_object)
|
44
|
+
|
45
|
+
return if path.nil?
|
46
|
+
|
47
|
+
api = (apis[path] ||= Api.from_yard_object(yard_object, self))
|
48
|
+
api.add_operation(yard_object)
|
49
|
+
end
|
50
|
+
|
51
|
+
# HACK, requires knowledge of resource_listing
|
52
|
+
def add_authorizations_to_resource_listing(yard_object)
|
53
|
+
yard_object.tags.select {|t| t.tag_name == "authorization"}.each do |t|
|
54
|
+
@resource_listing.authorizations << Authorization.from_yard_object(t)
|
55
|
+
end
|
25
56
|
end
|
26
57
|
|
27
58
|
def resource_name
|
28
59
|
@resource_path
|
29
60
|
end
|
30
61
|
|
62
|
+
def models
|
63
|
+
(api_models + property_models).uniq
|
64
|
+
end
|
65
|
+
|
66
|
+
def ref?(name)
|
67
|
+
@resource_listing.models.map(&:id).include?(name)
|
68
|
+
end
|
69
|
+
|
31
70
|
def to_h
|
32
|
-
{
|
33
|
-
"apiVersion" => SwaggerYard.api_version,
|
34
|
-
"swaggerVersion" => SwaggerYard.swagger_version,
|
35
|
-
"basePath" => SwaggerYard.api_base_path,
|
36
|
-
"
|
37
|
-
"apis" =>
|
38
|
-
"models" =>
|
71
|
+
{
|
72
|
+
"apiVersion" => SwaggerYard.config.api_version,
|
73
|
+
"swaggerVersion" => SwaggerYard.config.swagger_version,
|
74
|
+
"basePath" => SwaggerYard.config.api_base_path,
|
75
|
+
"resourcePath" => resource_path,
|
76
|
+
"apis" => apis.values.map(&:to_h),
|
77
|
+
"models" => Hash[models.map {|m| [m.id, m.to_h]}],
|
78
|
+
"authorizations" => authorizations
|
39
79
|
}
|
40
80
|
end
|
81
|
+
|
82
|
+
def listing_hash
|
83
|
+
{
|
84
|
+
"path" => resource_path,
|
85
|
+
"description" => description
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
def model_names_from_apis
|
91
|
+
apis.values.map(&:model_names).flatten.uniq
|
92
|
+
end
|
93
|
+
|
94
|
+
# models selected by the names of models referenced in APIs
|
95
|
+
def api_models
|
96
|
+
@resource_listing.models.select {|m| model_names_from_apis.include?(m.id)}
|
97
|
+
end
|
98
|
+
|
99
|
+
def model_names_from_model_properties
|
100
|
+
api_models.map{|model| model.recursive_properties_model_names(@resource_listing.models) }.flatten.uniq
|
101
|
+
end
|
102
|
+
|
103
|
+
# models selected by names used in properties in models used in APIs
|
104
|
+
def property_models
|
105
|
+
@resource_listing.models.select {|m| model_names_from_model_properties.include?(m.id)}
|
106
|
+
end
|
41
107
|
end
|
42
|
-
end
|
108
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module SwaggerYard
|
2
|
+
class Authorization
|
3
|
+
attr_reader :pass_as, :key
|
4
|
+
attr_writer :name
|
5
|
+
|
6
|
+
def self.from_yard_object(yard_object)
|
7
|
+
new(yard_object.types.first, yard_object.name, yard_object.text)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(type, pass_as, key)
|
11
|
+
@type, @pass_as, @key = type, pass_as, key
|
12
|
+
end
|
13
|
+
|
14
|
+
# the spec suggests most auth names are just the type of auth
|
15
|
+
def name
|
16
|
+
@name ||= [@pass_as, @key].join('_').downcase.gsub('-', '_')
|
17
|
+
end
|
18
|
+
|
19
|
+
def type
|
20
|
+
case @type
|
21
|
+
when "api_key"
|
22
|
+
"apiKey"
|
23
|
+
when "basic_auth"
|
24
|
+
"basicAuth"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_h
|
29
|
+
{
|
30
|
+
"type" => type,
|
31
|
+
"passAs" => @pass_as,
|
32
|
+
"keyname" => @key
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module SwaggerYard
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :swagger_spec_base_path, :api_base_path, :api_path
|
4
|
+
attr_accessor :swagger_version, :api_version
|
5
|
+
attr_accessor :enable, :reload
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.swagger_version = "1.1"
|
9
|
+
self.api_version = "0.1"
|
10
|
+
self.enable = false
|
11
|
+
self.reload = true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module SwaggerYard
|
2
|
+
class ListingInfo
|
3
|
+
attr_reader :description, :resource_path, :authorizations
|
4
|
+
|
5
|
+
def initialize(yard_object)
|
6
|
+
@description = yard_object.docstring
|
7
|
+
|
8
|
+
if tag = yard_object.tags.detect {|t| t.tag_name == "resource_path"}
|
9
|
+
@resource_path = tag.text.downcase
|
10
|
+
end
|
11
|
+
|
12
|
+
@authorizations = yard_object.tags.select {|t| t.tag_name == "authorize_with"}.map(&:text)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|