swagger-docs 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/lib/swagger/docs/dsl.rb +1 -1
- data/lib/swagger/docs/generator.rb +43 -24
- data/lib/swagger/docs/methods.rb +2 -2
- data/lib/swagger/docs/task.rb +1 -1
- data/lib/swagger/docs/version.rb +1 -1
- data/pec +120 -0
- data/spec/fixtures/controllers/application_controller.rb +1 -1
- data/spec/fixtures/controllers/ignored_controller.rb +1 -1
- data/spec/fixtures/controllers/sample_controller.rb +6 -0
- data/spec/lib/swagger/docs/generator_spec.rb +3 -2
- data/spec/spec_helper.rb +0 -1
- metadata +3 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3eb51612948dcffe393a0fee74c1f79dcfb64ae9
|
4
|
+
data.tar.gz: ccaa3a9f4e00a1b022b1c71eb9431fba6675efee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 191427fd69bff4114b6a55a844430e373f7505eab2d0a63781c027fd468691b6f19fe36fb2fea1107a585b1e6acfc6152e2934a4ff96e5f022a0e944c7b822a8
|
7
|
+
data.tar.gz: 1fe19613a6cc7d7a7cf70cb1c1b2ee85d92bb9fc9a9aecb34d6b8c33b6d49180a9aceea273ff021b5f1d23f36cd1705d5d0b7d132bdc9081146ca10454b3dd9f
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/swagger/docs/dsl.rb
CHANGED
@@ -17,34 +17,53 @@ module Swagger
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def write_docs(apis = nil)
|
20
|
+
results = generate_docs(apis)
|
21
|
+
results.each{|api_version, result| write_doc(result) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def write_doc(result)
|
25
|
+
settings = result[:settings]
|
26
|
+
config = result[:config]
|
27
|
+
create_output_paths(settings[:api_file_path])
|
28
|
+
clean_output_paths(settings[:api_file_path]) if config[:clean_directory] || false
|
29
|
+
root = result[:root]
|
30
|
+
resources = root.delete 'resources'
|
31
|
+
write_to_file("#{settings[:api_file_path]}/api-docs.json", root, config)
|
32
|
+
resources.each do |resource|
|
33
|
+
resource_file_path = resource.delete 'resourceFilePath'
|
34
|
+
write_to_file(File.join(settings[:api_file_path], "#{resource_file_path}.json"), resource, config)
|
35
|
+
end
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
def generate_docs(apis=nil)
|
20
40
|
apis ||= Config.registered_apis
|
21
41
|
results = {}
|
22
42
|
set_real_methods
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
results[
|
43
|
+
|
44
|
+
apis[DEFAULT_VER] = DEFAULT_CONFIG if apis.empty?
|
45
|
+
|
46
|
+
apis.each do |api_version, config|
|
47
|
+
settings = get_settings(api_version, config)
|
48
|
+
config.reverse_merge!(DEFAULT_CONFIG)
|
49
|
+
results[api_version] = generate_doc(api_version, settings, config)
|
50
|
+
results[api_version][:settings] = settings
|
51
|
+
results[api_version][:config] = config
|
30
52
|
end
|
31
53
|
results
|
32
54
|
end
|
33
55
|
|
34
|
-
def
|
35
|
-
settings = get_settings(api_version, config)
|
36
|
-
|
37
|
-
create_output_paths(settings[:api_file_path])
|
38
|
-
clean_output_paths(settings[:api_file_path]) if config[:clean_directory] || false
|
39
|
-
|
56
|
+
def generate_doc(api_version, settings, config)
|
40
57
|
root = { :api_version => api_version, :swagger_version => "1.2", :base_path => settings[:base_path] + "/", :apis => []}
|
41
58
|
results = {:processed => [], :skipped => []}
|
59
|
+
resources = []
|
42
60
|
|
43
61
|
get_route_paths(settings[:controller_base_path]).each do |path|
|
44
62
|
ret = process_path(path, root, config, settings)
|
45
63
|
results[ret[:action]] << ret
|
46
64
|
if ret[:action] == :processed
|
47
|
-
|
65
|
+
resource = generate_resource(ret[:path], ret[:apis], ret[:models], settings, root, config)
|
66
|
+
resources << resource
|
48
67
|
debased_path = get_debased_path(ret[:path], settings[:controller_base_path])
|
49
68
|
resource_api = {
|
50
69
|
path: "#{Config.transform_path(trim_leading_slash(debased_path))}.{format}",
|
@@ -53,9 +72,9 @@ module Swagger
|
|
53
72
|
root[:apis] << resource_api
|
54
73
|
end
|
55
74
|
end
|
56
|
-
|
75
|
+
root[:resources] = resources
|
57
76
|
camelize_keys_deep!(root)
|
58
|
-
|
77
|
+
results[:root] = root
|
59
78
|
results
|
60
79
|
end
|
61
80
|
|
@@ -115,7 +134,7 @@ module Swagger
|
|
115
134
|
{action: :processed, path: path, apis: apis, models: models, klass: klass}
|
116
135
|
end
|
117
136
|
|
118
|
-
def
|
137
|
+
def generate_resource(path, apis, models, settings, root, config)
|
119
138
|
debased_path = get_debased_path(path, settings[:controller_base_path])
|
120
139
|
demod = "#{debased_path.to_s.camelize}".demodulize.camelize.underscore
|
121
140
|
resource_path = trim_leading_slash(debased_path.to_s.underscore)
|
@@ -123,24 +142,24 @@ module Swagger
|
|
123
142
|
camelize_keys_deep!(resource)
|
124
143
|
# Add the already-normalized models to the resource.
|
125
144
|
resource = resource.merge({:models => models}) if models.present?
|
126
|
-
|
127
|
-
|
145
|
+
resource[:resource_file_path] = resource_path
|
146
|
+
resource
|
128
147
|
end
|
129
148
|
|
130
149
|
def get_route_path_apis(path, route, klass, settings, config)
|
131
150
|
models, apis = {}, []
|
132
151
|
action = route.defaults[:action]
|
133
152
|
verb = route.verb.source.to_s.delete('$'+'^').downcase.to_sym
|
134
|
-
return apis if !operations = klass.swagger_actions[action.to_sym]
|
153
|
+
return {apis: apis, models: models} if !operations = klass.swagger_actions[action.to_sym]
|
135
154
|
operations = Hash[operations.map {|k, v| [k.to_s.gsub("@","").to_sym, v.respond_to?(:deep_dup) ? v.deep_dup : v.dup] }] # rename :@instance hash keys
|
136
155
|
operations[:method] = verb
|
137
156
|
operations[:nickname] = "#{path.camelize}##{action}"
|
138
|
-
|
157
|
+
|
139
158
|
api_path = transform_spec_to_api_path(route.path.spec, settings[:controller_base_path], config[:api_extension_type])
|
140
159
|
operations[:parameters] = filter_path_params(api_path, operations[:parameters]) if operations[:parameters]
|
141
160
|
|
142
161
|
apis << {:path => api_path, :operations => [operations]}
|
143
|
-
models = get_klass_models(klass)
|
162
|
+
models = get_klass_models(klass)
|
144
163
|
|
145
164
|
{apis: apis, models: models}
|
146
165
|
end
|
@@ -164,8 +183,8 @@ module Swagger
|
|
164
183
|
base_path = trim_trailing_slash(config[:base_path] || "")
|
165
184
|
controller_base_path = trim_leading_slash(config[:controller_base_path] || "")
|
166
185
|
base_path += "/#{controller_base_path}" unless controller_base_path.empty?
|
167
|
-
api_file_path = config[:api_file_path]
|
168
|
-
settings = {
|
186
|
+
api_file_path = config[:api_file_path]
|
187
|
+
settings = {
|
169
188
|
base_path: base_path,
|
170
189
|
controller_base_path: controller_base_path,
|
171
190
|
api_file_path: api_file_path
|
data/lib/swagger/docs/methods.rb
CHANGED
@@ -27,9 +27,9 @@ module Swagger
|
|
27
27
|
|
28
28
|
def swagger_api(action, &block)
|
29
29
|
@swagger_dsl ||= {}
|
30
|
-
return if @swagger_dsl[action]
|
31
30
|
dsl = SwaggerDSL.call(action, self, &block)
|
32
|
-
@swagger_dsl[action]
|
31
|
+
@swagger_dsl[action] ||= {}
|
32
|
+
@swagger_dsl[action].deep_merge!(dsl) { |key, old, new| Array(old) + Array(new) }
|
33
33
|
end
|
34
34
|
|
35
35
|
def swagger_model(model_name, &block)
|
data/lib/swagger/docs/task.rb
CHANGED
data/lib/swagger/docs/version.rb
CHANGED
data/pec
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
[1mdiff --git a/lib/swagger/docs/generator.rb b/lib/swagger/docs/generator.rb[m
|
2
|
+
[1mindex ed18606..6455c92 100644[m
|
3
|
+
[1m--- a/lib/swagger/docs/generator.rb[m
|
4
|
+
[1m+++ b/lib/swagger/docs/generator.rb[m
|
5
|
+
[36m@@ -17,34 +17,56 @@[m [mmodule Swagger[m
|
6
|
+
end[m
|
7
|
+
[m
|
8
|
+
def write_docs(apis = nil)[m
|
9
|
+
[31m- apis ||= Config.registered_apis[m
|
10
|
+
[31m- results = {}[m
|
11
|
+
[31m- set_real_methods[m
|
12
|
+
[31m- unless apis.empty?[m
|
13
|
+
[31m- apis.each do |api_version,config|[m
|
14
|
+
[31m- config.reverse_merge!(DEFAULT_CONFIG)[m
|
15
|
+
[31m- results[api_version] = write_doc(api_version, config)[m
|
16
|
+
[31m- end[m
|
17
|
+
[31m- else[m
|
18
|
+
[31m- results[DEFAULT_VER] = write_doc(DEFAULT_VER, DEFAULT_CONFIG)[m
|
19
|
+
[32m+[m[32m results = generate_docs(apis)[m
|
20
|
+
[32m+[m[32m results.each do |api_version, result|[m
|
21
|
+
[32m+[m[32m write_doc(result)[m
|
22
|
+
end[m
|
23
|
+
results[m
|
24
|
+
end[m
|
25
|
+
[m
|
26
|
+
[31m- def write_doc(api_version, config)[m
|
27
|
+
[31m- settings = get_settings(api_version, config)[m
|
28
|
+
[31m-[m
|
29
|
+
[32m+[m[32m def write_doc(result)[m
|
30
|
+
[32m+[m[32m settings = result[:settings][m
|
31
|
+
[32m+[m[32m config = result[:config][m
|
32
|
+
create_output_paths(settings[:api_file_path])[m
|
33
|
+
clean_output_paths(settings[:api_file_path]) if config[:clean_directory] || false[m
|
34
|
+
[32m+[m[32m root = result[:root][m
|
35
|
+
[32m+[m[32m resources = root.delete 'resources'[m
|
36
|
+
[32m+[m[32m write_to_file("#{settings[:api_file_path]}/api-docs.json", root, config)[m
|
37
|
+
[32m+[m[32m resources.each do |resource|[m
|
38
|
+
[32m+[m[32m resource_file_path = resource.delete 'resourceFilePath'[m
|
39
|
+
[32m+[m[32m write_to_file(File.join(settings[:api_file_path], "#{resource_file_path}.json"), resource, config)[m
|
40
|
+
[32m+[m[32m end[m
|
41
|
+
[32m+[m[32m result[m
|
42
|
+
[32m+[m[32m end[m
|
43
|
+
[m
|
44
|
+
[32m+[m[32m def generate_docs(apis=nil)[m
|
45
|
+
[32m+[m[32m apis ||= Config.registered_apis[m
|
46
|
+
[32m+[m[32m results = {}[m
|
47
|
+
[32m+[m[32m set_real_methods[m
|
48
|
+
[32m+[m
|
49
|
+
[32m+[m[32m apis[DEFAULT_VER] = DEFAULT_CONFIG if apis.empty?[m
|
50
|
+
[32m+[m
|
51
|
+
[32m+[m[32m apis.each do |api_version, config|[m
|
52
|
+
[32m+[m[32m settings = get_settings(api_version, config)[m
|
53
|
+
[32m+[m[32m config.reverse_merge!(DEFAULT_CONFIG)[m
|
54
|
+
[32m+[m[32m results[api_version] = generate_doc(api_version, settings, config)[m
|
55
|
+
[32m+[m[32m results[api_version][:settings] = settings[m
|
56
|
+
[32m+[m[32m results[api_version][:config] = config[m
|
57
|
+
[32m+[m[32m end[m
|
58
|
+
[32m+[m[32m results[m
|
59
|
+
[32m+[m[32m end[m
|
60
|
+
[32m+[m
|
61
|
+
[32m+[m[32m def generate_doc(api_version, settings, config)[m
|
62
|
+
root = { :api_version => api_version, :swagger_version => "1.2", :base_path => settings[:base_path] + "/", :apis => []}[m
|
63
|
+
results = {:processed => [], :skipped => []}[m
|
64
|
+
[32m+[m[32m resources = [][m
|
65
|
+
[m
|
66
|
+
get_route_paths(settings[:controller_base_path]).each do |path|[m
|
67
|
+
ret = process_path(path, root, config, settings)[m
|
68
|
+
results[ret[:action]] << ret[m
|
69
|
+
if ret[:action] == :processed[m
|
70
|
+
[31m- create_resource_file(ret[:path], ret[:apis], ret[:models], settings, root, config)[m
|
71
|
+
[32m+[m[32m resource = generate_resource(ret[:path], ret[:apis], ret[:models], settings, root, config)[m
|
72
|
+
[32m+[m[32m resources << resource[m
|
73
|
+
debased_path = get_debased_path(ret[:path], settings[:controller_base_path])[m
|
74
|
+
resource_api = {[m
|
75
|
+
path: "#{Config.transform_path(trim_leading_slash(debased_path))}.{format}",[m
|
76
|
+
[36m@@ -53,9 +75,9 @@[m [mmodule Swagger[m
|
77
|
+
root[:apis] << resource_api[m
|
78
|
+
end[m
|
79
|
+
end[m
|
80
|
+
[31m-[m
|
81
|
+
[32m+[m[32m root[:resources] = resources[m
|
82
|
+
camelize_keys_deep!(root)[m
|
83
|
+
[31m- write_to_file("#{settings[:api_file_path]}/api-docs.json", root, config)[m
|
84
|
+
[32m+[m[32m results[:root] = root[m
|
85
|
+
results[m
|
86
|
+
end[m
|
87
|
+
[m
|
88
|
+
[36m@@ -115,7 +137,7 @@[m [mmodule Swagger[m
|
89
|
+
{action: :processed, path: path, apis: apis, models: models, klass: klass}[m
|
90
|
+
end[m
|
91
|
+
[m
|
92
|
+
[31m- def create_resource_file(path, apis, models, settings, root, config)[m
|
93
|
+
[32m+[m[32m def generate_resource(path, apis, models, settings, root, config)[m
|
94
|
+
debased_path = get_debased_path(path, settings[:controller_base_path])[m
|
95
|
+
demod = "#{debased_path.to_s.camelize}".demodulize.camelize.underscore[m
|
96
|
+
resource_path = trim_leading_slash(debased_path.to_s.underscore)[m
|
97
|
+
[36m@@ -123,8 +145,8 @@[m [mmodule Swagger[m
|
98
|
+
camelize_keys_deep!(resource)[m
|
99
|
+
# Add the already-normalized models to the resource.[m
|
100
|
+
resource = resource.merge({:models => models}) if models.present?[m
|
101
|
+
[31m- # write controller resource file[m
|
102
|
+
[31m- write_to_file(File.join(settings[:api_file_path], "#{resource_path}.json"), resource, config)[m
|
103
|
+
[32m+[m[32m resource[:resource_file_path] = resource_path[m
|
104
|
+
[32m+[m[32m resource[m
|
105
|
+
end[m
|
106
|
+
[m
|
107
|
+
def get_route_path_apis(path, route, klass, settings, config)[m
|
108
|
+
[1mdiff --git a/spec/lib/swagger/docs/generator_spec.rb b/spec/lib/swagger/docs/generator_spec.rb[m
|
109
|
+
[1mindex d319ac3..ba57ccd 100644[m
|
110
|
+
[1m--- a/spec/lib/swagger/docs/generator_spec.rb[m
|
111
|
+
[1m+++ b/spec/lib/swagger/docs/generator_spec.rb[m
|
112
|
+
[36m@@ -298,7 +298,7 @@[m [mdescribe Swagger::Docs::Generator do[m
|
113
|
+
}[m
|
114
|
+
}[m
|
115
|
+
}[m
|
116
|
+
[31m- expect(models['Tag']).to eq expected_model[m
|
117
|
+
[32m+[m[32m expect(models['tag']).to eq expected_model[m
|
118
|
+
end[m
|
119
|
+
end[m
|
120
|
+
end[m
|
@@ -10,7 +10,13 @@ module Api
|
|
10
10
|
param :query, :page, :integer, :optional, "Page number"
|
11
11
|
param :path, :nested_id, :integer, :optional, "Team Id"
|
12
12
|
response :unauthorized
|
13
|
+
end
|
14
|
+
|
15
|
+
swagger_api :index do
|
13
16
|
response :not_acceptable, "The request you made is not acceptable"
|
17
|
+
end
|
18
|
+
|
19
|
+
swagger_api :index do
|
14
20
|
response :requested_range_not_satisfiable
|
15
21
|
end
|
16
22
|
|
@@ -19,7 +19,8 @@ describe Swagger::Docs::Generator do
|
|
19
19
|
stub_route("^PUT$", "update", "api/v1/sample", "/api/v1/sample/:id(.:format)"),
|
20
20
|
stub_route("^DELETE$", "destroy", "api/v1/sample", "/api/v1/sample/:id(.:format)"),
|
21
21
|
stub_route("^GET$", "new", "api/v1/sample", "/api/v1/sample/new(.:format)"), # no parameters for this method
|
22
|
-
stub_route("^GET$", "index", "", "/api/v1/empty_path") # intentional empty path should not cause any errors
|
22
|
+
stub_route("^GET$", "index", "", "/api/v1/empty_path"), # intentional empty path should not cause any errors
|
23
|
+
stub_route("^GET$", "ignored", "api/v1/sample", "/api/v1/ignored(.:format)") # an action without documentation should not cause any errors
|
23
24
|
]}
|
24
25
|
|
25
26
|
let(:tmp_dir) { Pathname.new('/tmp/swagger-docs/') }
|
@@ -298,7 +299,7 @@ describe Swagger::Docs::Generator do
|
|
298
299
|
}
|
299
300
|
}
|
300
301
|
}
|
301
|
-
expect(models['
|
302
|
+
expect(models['tag']).to eq expected_model
|
302
303
|
end
|
303
304
|
end
|
304
305
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swagger-docs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rich Hollis
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
RYcsqDfanYBx7QcftOnbeQq7/Ep7Zx+W9+Ph3TiJLMLdAr7bLkgN1SjvrjTL5mQR
|
31
31
|
FuQtYvE4LKiUQpG7vLTRB78dQBlSj9fnv2OM9w==
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2014-04-
|
33
|
+
date: 2014-04-07 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: bundler
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/swagger/docs/task.rb
|
127
127
|
- lib/swagger/docs/version.rb
|
128
128
|
- lib/tasks/swagger.rake
|
129
|
+
- pec
|
129
130
|
- spec/fixtures/controllers/application_controller.rb
|
130
131
|
- spec/fixtures/controllers/ignored_controller.rb
|
131
132
|
- spec/fixtures/controllers/sample_controller.rb
|
metadata.gz.sig
CHANGED
Binary file
|