swagger-docs 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fae3be7ad414552f11679fa9038c9bdadc8e313a
4
- data.tar.gz: d669dca205c935dcd57af4b405fe5e777cd014ff
3
+ metadata.gz: 3eb51612948dcffe393a0fee74c1f79dcfb64ae9
4
+ data.tar.gz: ccaa3a9f4e00a1b022b1c71eb9431fba6675efee
5
5
  SHA512:
6
- metadata.gz: 0c6cc1f82349ef06372e0443312f7abd7e154942b35631f4176b450e841d3b83c905e396d665d22c7874aa244c8dd5b1422b9451e8f2c6de84a9a04243b05b9c
7
- data.tar.gz: fd41c0368dc912c349bcf58ab3e286b6b3506a4295afcb5fda9e8452e13851cf048ad8ddd28b489dcdf20ca47645a3dd673f2edfc116b167c465e97300e42cf8
6
+ metadata.gz: 191427fd69bff4114b6a55a844430e373f7505eab2d0a63781c027fd468691b6f19fe36fb2fea1107a585b1e6acfc6152e2934a4ff96e5f022a0e944c7b822a8
7
+ data.tar.gz: 1fe19613a6cc7d7a7cf70cb1c1b2ee85d92bb9fc9a9aecb34d6b8c33b6d49180a9aceea273ff021b5f1d23f36cd1705d5d0b7d132bdc9081146ca10454b3dd9f
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -97,4 +97,4 @@ module Swagger
97
97
  end
98
98
  end
99
99
  end
100
- end
100
+ end
@@ -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
- unless apis.empty?
24
- apis.each do |api_version,config|
25
- config.reverse_merge!(DEFAULT_CONFIG)
26
- results[api_version] = write_doc(api_version, config)
27
- end
28
- else
29
- results[DEFAULT_VER] = write_doc(DEFAULT_VER, DEFAULT_CONFIG)
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 write_doc(api_version, config)
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
- create_resource_file(ret[:path], ret[:apis], ret[:models], settings, root, config)
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
- write_to_file("#{settings[:api_file_path]}/api-docs.json", root, config)
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 create_resource_file(path, apis, models, settings, root, config)
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
- # write controller resource file
127
- write_to_file(File.join(settings[:api_file_path], "#{resource_path}.json"), resource, config)
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
@@ -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] = dsl
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)
@@ -6,4 +6,4 @@ module Swagger
6
6
  end
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Swagger
2
2
  module Docs
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
data/pec ADDED
@@ -0,0 +1,120 @@
1
+ diff --git a/lib/swagger/docs/generator.rb b/lib/swagger/docs/generator.rb
2
+ index ed18606..6455c92 100644
3
+ --- a/lib/swagger/docs/generator.rb
4
+ +++ b/lib/swagger/docs/generator.rb
5
+ @@ -17,34 +17,56 @@ module Swagger
6
+ end
7
+ 
8
+ def write_docs(apis = nil)
9
+ - apis ||= Config.registered_apis
10
+ - results = {}
11
+ - set_real_methods
12
+ - unless apis.empty?
13
+ - apis.each do |api_version,config|
14
+ - config.reverse_merge!(DEFAULT_CONFIG)
15
+ - results[api_version] = write_doc(api_version, config)
16
+ - end
17
+ - else
18
+ - results[DEFAULT_VER] = write_doc(DEFAULT_VER, DEFAULT_CONFIG)
19
+ + results = generate_docs(apis)
20
+ + results.each do |api_version, result|
21
+ + write_doc(result)
22
+ end
23
+ results
24
+ end
25
+ 
26
+ - def write_doc(api_version, config)
27
+ - settings = get_settings(api_version, config)
28
+ -
29
+ + def write_doc(result)
30
+ + settings = result[:settings]
31
+ + config = result[:config]
32
+ create_output_paths(settings[:api_file_path])
33
+ clean_output_paths(settings[:api_file_path]) if config[:clean_directory] || false
34
+ + root = result[:root]
35
+ + resources = root.delete 'resources'
36
+ + write_to_file("#{settings[:api_file_path]}/api-docs.json", root, config)
37
+ + resources.each do |resource|
38
+ + resource_file_path = resource.delete 'resourceFilePath'
39
+ + write_to_file(File.join(settings[:api_file_path], "#{resource_file_path}.json"), resource, config)
40
+ + end
41
+ + result
42
+ + end
43
+ 
44
+ + def generate_docs(apis=nil)
45
+ + apis ||= Config.registered_apis
46
+ + results = {}
47
+ + set_real_methods
48
+ +
49
+ + apis[DEFAULT_VER] = DEFAULT_CONFIG if apis.empty?
50
+ +
51
+ + apis.each do |api_version, config|
52
+ + settings = get_settings(api_version, config)
53
+ + config.reverse_merge!(DEFAULT_CONFIG)
54
+ + results[api_version] = generate_doc(api_version, settings, config)
55
+ + results[api_version][:settings] = settings
56
+ + results[api_version][:config] = config
57
+ + end
58
+ + results
59
+ + end
60
+ +
61
+ + def generate_doc(api_version, settings, config)
62
+ root = { :api_version => api_version, :swagger_version => "1.2", :base_path => settings[:base_path] + "/", :apis => []}
63
+ results = {:processed => [], :skipped => []}
64
+ + resources = []
65
+ 
66
+ get_route_paths(settings[:controller_base_path]).each do |path|
67
+ ret = process_path(path, root, config, settings)
68
+ results[ret[:action]] << ret
69
+ if ret[:action] == :processed
70
+ - create_resource_file(ret[:path], ret[:apis], ret[:models], settings, root, config)
71
+ + resource = generate_resource(ret[:path], ret[:apis], ret[:models], settings, root, config)
72
+ + resources << resource
73
+ debased_path = get_debased_path(ret[:path], settings[:controller_base_path])
74
+ resource_api = {
75
+ path: "#{Config.transform_path(trim_leading_slash(debased_path))}.{format}",
76
+ @@ -53,9 +75,9 @@ module Swagger
77
+ root[:apis] << resource_api
78
+ end
79
+ end
80
+ -
81
+ + root[:resources] = resources
82
+ camelize_keys_deep!(root)
83
+ - write_to_file("#{settings[:api_file_path]}/api-docs.json", root, config)
84
+ + results[:root] = root
85
+ results
86
+ end
87
+ 
88
+ @@ -115,7 +137,7 @@ module Swagger
89
+ {action: :processed, path: path, apis: apis, models: models, klass: klass}
90
+ end
91
+ 
92
+ - def create_resource_file(path, apis, models, settings, root, config)
93
+ + def generate_resource(path, apis, models, settings, root, config)
94
+ debased_path = get_debased_path(path, settings[:controller_base_path])
95
+ demod = "#{debased_path.to_s.camelize}".demodulize.camelize.underscore
96
+ resource_path = trim_leading_slash(debased_path.to_s.underscore)
97
+ @@ -123,8 +145,8 @@ module Swagger
98
+ camelize_keys_deep!(resource)
99
+ # Add the already-normalized models to the resource.
100
+ resource = resource.merge({:models => models}) if models.present?
101
+ - # write controller resource file
102
+ - write_to_file(File.join(settings[:api_file_path], "#{resource_path}.json"), resource, config)
103
+ + resource[:resource_file_path] = resource_path
104
+ + resource
105
+ end
106
+ 
107
+ def get_route_path_apis(path, route, klass, settings, config)
108
+ diff --git a/spec/lib/swagger/docs/generator_spec.rb b/spec/lib/swagger/docs/generator_spec.rb
109
+ index d319ac3..ba57ccd 100644
110
+ --- a/spec/lib/swagger/docs/generator_spec.rb
111
+ +++ b/spec/lib/swagger/docs/generator_spec.rb
112
+ @@ -298,7 +298,7 @@ describe Swagger::Docs::Generator do
113
+ }
114
+ }
115
+ }
116
+ - expect(models['Tag']).to eq expected_model
117
+ + expect(models['tag']).to eq expected_model
118
+ end
119
+ end
120
+ end
@@ -1,3 +1,3 @@
1
1
  class ApplicationController
2
2
 
3
- end
3
+ end
@@ -3,4 +3,4 @@ module Api
3
3
  class IgnoredController
4
4
  end
5
5
  end
6
- end
6
+ end
@@ -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['Tag']).to eq expected_model
302
+ expect(models['tag']).to eq expected_model
302
303
  end
303
304
  end
304
305
  end
data/spec/spec_helper.rb CHANGED
@@ -41,4 +41,3 @@ end
41
41
  def get_api_parameter(api, name)
42
42
  api["parameters"].each{|param| return param if param["name"] == name}
43
43
  end
44
-
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.3
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-03 00:00:00.000000000 Z
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