swagger-docs 0.1.6 → 0.1.7

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: 17bc5fa1520eb501616e867896c0f7f7000c31fd
4
- data.tar.gz: 068f350b226c8fc91d300fce8fd1ac9770ee694e
3
+ metadata.gz: 79b8f96fd5d1dd858fd3a235edfdffb7428e6580
4
+ data.tar.gz: 6e897b7424794ce10918a371d2cf7b7c92ac8586
5
5
  SHA512:
6
- metadata.gz: 0ea1c7020b0b029f3d897a2c21fb6b72850bb64358d789473ef2bcba346da34e1b730f216435b42f5767742d3bd7e79da19e3c3d38c697402920688e0dc55f0c
7
- data.tar.gz: a38e70b6f391db69f06676372ffd1dab18ecebc9c042bea9844221ada5bf2196ccf8610e026d73df108e7b43f5b555641a64ca4dad52d4b99036a34abf1a8a49
6
+ metadata.gz: 05e6f94b18aa17704e320384ebe444188ed248eb0a6d36f195e997ea51c915ac7335a11e8b8de4166fceca8866bcf8249152eeeebd950e4c9ca808426426ae4e
7
+ data.tar.gz: 78e2d06232b9244590fa5a886d5f8530ed8564de293e8237a6b7781a2507ef81708fb3b3c99a9203d59f607b97ac488552d991d91c3dc50a372a61c3ebb4ca35
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.1.7
2
+
3
+ - Make camelizing of model properties configurable. #55
4
+
1
5
  ## 0.1.6
2
6
 
3
7
  - Document notes DSL
data/README.md CHANGED
@@ -102,6 +102,12 @@ The following table shows all the current configuration options and their defaul
102
102
  <td>:pretty</td>
103
103
  </tr>
104
104
 
105
+ <tr>
106
+ <td><b>camelize_model_properties</b></td>
107
+ <td>Camelizes property names of models. For example, a property name called first_name would be converted to firstName.</td>
108
+ <td>true</td>
109
+ </tr>
110
+
105
111
  </tbody>
106
112
  </table>
107
113
 
@@ -174,19 +180,19 @@ end
174
180
  ### DRYing up common documentation
175
181
 
176
182
  Suppose you have a header or a parameter that must be present on several controllers and methods. Instead of duplicating it on all the controllers you can do this on your API base controller:
177
-
183
+
178
184
  ```ruby
179
185
  class Api::BaseController < ActionController::Base
180
186
  class << self
181
187
  Swagger::Docs::Generator::set_real_methods
182
-
188
+
183
189
  def inherited(subclass)
184
190
  super
185
191
  subclass.class_eval do
186
192
  setup_basic_api_documentation
187
193
  end
188
194
  end
189
-
195
+
190
196
  private
191
197
  def setup_basic_api_documentation
192
198
  [:index, :show, :create, :update, :delete].each do |api_action|
@@ -198,7 +204,7 @@ class Api::BaseController < ActionController::Base
198
204
  end
199
205
  end
200
206
  ```
201
-
207
+
202
208
  And then use it as a superclass to all you API controllers. All the subclassed controllers will have the same documentation applied to them.
203
209
 
204
210
  ### DSL Methods
@@ -608,7 +614,7 @@ users.json output:
608
614
 
609
615
  ## Thanks to our contributors
610
616
 
611
- Thanks to jdar, fotinakis, stevschmid, ldnunes and all of our contributors for making swagger-docs even better.
617
+ Thanks to jdar, fotinakis, stevschmid, ldnunes, aaronrenner and all of our contributors for making swagger-docs even better.
612
618
 
613
619
  ## Contributing
614
620
 
@@ -0,0 +1,120 @@
1
+ module Swagger
2
+ module Docs
3
+ class ApiDeclarationFile
4
+ attr_reader :metadata, :apis
5
+
6
+ def initialize(metadata, apis, models)
7
+ @metadata = metadata
8
+ @apis = camelize_keys_deep apis
9
+ @models = models
10
+ end
11
+
12
+ def generate_resource
13
+ resource = build_resource_root_hash
14
+ # Add the already-normalized models to the resource.
15
+ resource = resource.merge({:models => models}) if models.present?
16
+ resource
17
+ end
18
+
19
+ def base_path
20
+ metadata.base_path
21
+ end
22
+
23
+ def path
24
+ metadata.path
25
+ end
26
+
27
+ def swagger_version
28
+ metadata.swagger_version
29
+ end
30
+
31
+ def api_version
32
+ metadata.api_version
33
+ end
34
+
35
+ def controller_base_path
36
+ metadata.controller_base_path
37
+ end
38
+
39
+ def camelize_model_properties
40
+ metadata.camelize_model_properties
41
+ end
42
+
43
+ def resource_path
44
+ demod
45
+ end
46
+
47
+ def resource_file_path
48
+ trim_leading_slash(debased_path.to_s.underscore)
49
+ end
50
+
51
+ def models
52
+ normalize_model_properties @models
53
+ end
54
+
55
+ private
56
+
57
+ def build_resource_root_hash
58
+ {
59
+ "apiVersion" => api_version,
60
+ "swaggerVersion" => swagger_version,
61
+ "basePath" => base_path,
62
+ "resourcePath" => resource_path,
63
+ "apis" => apis,
64
+ "resourceFilePath" => resource_file_path
65
+ }
66
+ end
67
+
68
+ def normalize_model_properties(models)
69
+ Hash[
70
+ models.map do |k, v|
71
+ if camelize_model_properties
72
+ [k.to_s, camelize_keys_deep(v)]
73
+ else
74
+ [k.to_s, stringify_keys_deep(v)]
75
+ end
76
+ end]
77
+ end
78
+
79
+ def demod
80
+ "#{debased_path.to_s.camelize}".demodulize.camelize.underscore
81
+ end
82
+
83
+ def debased_path
84
+ path.gsub("#{controller_base_path}", "")
85
+ end
86
+
87
+ def trim_leading_slash(str)
88
+ return str if !str
89
+ str.gsub(/\A\/+/, '')
90
+ end
91
+
92
+ def camelize_keys_deep(obj)
93
+ process_keys_deep(obj){|key| key.to_s.camelize(:lower)}
94
+ end
95
+
96
+ def stringify_keys_deep(obj)
97
+ process_keys_deep(obj){|key| key.to_s}
98
+ end
99
+
100
+ def process_keys_deep(obj, &block)
101
+ if obj.is_a? Hash
102
+ Hash[
103
+ obj.map do |k, v|
104
+ new_key = block.call(k)
105
+ new_value = process_keys_deep v, &block
106
+ [new_key, new_value]
107
+ end
108
+ ]
109
+ elsif obj.is_a? Array
110
+ new_value = obj.collect do |a|
111
+ process_keys_deep a, &block
112
+ end
113
+ else
114
+ obj
115
+ end
116
+ end
117
+
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,18 @@
1
+ module Swagger
2
+ module Docs
3
+ class ApiDeclarationFileMetadata
4
+ DEFAULT_SWAGGER_VERSION = "1.2"
5
+
6
+ attr_reader :api_version, :path, :base_path, :controller_base_path, :swagger_version, :camelize_model_properties
7
+
8
+ def initialize(api_version, path, base_path, controller_base_path, options={})
9
+ @api_version = api_version
10
+ @path = path
11
+ @base_path = base_path
12
+ @controller_base_path = controller_base_path
13
+ @swagger_version = options.fetch(:swagger_version, DEFAULT_SWAGGER_VERSION)
14
+ @camelize_model_properties = options.fetch(:camelize_model_properties, true)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -54,7 +54,7 @@ module Swagger
54
54
  end
55
55
 
56
56
  def generate_doc(api_version, settings, config)
57
- root = { :api_version => api_version, :swagger_version => "1.2", :base_path => settings[:base_path] + "/", :apis => []}
57
+ root = { "apiVersion" => api_version, "swaggerVersion" => "1.2", "basePath" => settings[:base_path] + "/", :apis => []}
58
58
  results = {:processed => [], :skipped => []}
59
59
  resources = []
60
60
 
@@ -71,8 +71,7 @@ module Swagger
71
71
  root[:apis] << resource_api
72
72
  end
73
73
  end
74
- root[:resources] = resources
75
- camelize_keys_deep!(root)
74
+ root['resources'] = resources
76
75
  results[:root] = root
77
76
  results
78
77
  end
@@ -134,15 +133,12 @@ module Swagger
134
133
  end
135
134
 
136
135
  def generate_resource(path, apis, models, settings, root, config)
137
- debased_path = get_debased_path(path, settings[:controller_base_path])
138
- demod = "#{debased_path.to_s.camelize}".demodulize.camelize.underscore
139
- resource_path = trim_leading_slash(debased_path.to_s.underscore)
140
- resource = root.merge({:resource_path => "#{demod}", :apis => apis})
141
- camelize_keys_deep!(resource)
142
- # Add the already-normalized models to the resource.
143
- resource = resource.merge({:models => models}) if models.present?
144
- resource[:resource_file_path] = resource_path
145
- resource
136
+ metadata = ApiDeclarationFileMetadata.new(root["apiVersion"], path, root["basePath"],
137
+ settings[:controller_base_path],
138
+ camelize_model_properties: config.fetch(:camelize_model_properties, true),
139
+ swagger_version: root["swaggerVersion"])
140
+ declaration = ApiDeclarationFile.new(metadata, apis, models)
141
+ declaration.generate_resource
146
142
  end
147
143
 
148
144
  def get_route_path_apis(path, route, klass, settings, config)
@@ -1,5 +1,5 @@
1
1
  module Swagger
2
2
  module Docs
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
data/lib/swagger/docs.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "swagger/docs/config"
2
2
  require "swagger/docs/dsl"
3
+ require "swagger/docs/api_declaration_file_metadata"
4
+ require "swagger/docs/api_declaration_file"
3
5
  require "swagger/docs/generator"
4
6
  require "swagger/docs/impotent_methods"
5
7
  require "swagger/docs/methods"
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Swagger::Docs::ApiDeclarationFileMetadata do
4
+
5
+ describe "#initialize" do
6
+ it "sets the api_version property" do
7
+ metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath")
8
+
9
+ expect(metadata.api_version).to eq("1.0")
10
+ end
11
+
12
+ it "sets the path property" do
13
+ metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath")
14
+
15
+ expect(metadata.path).to eq("path")
16
+ end
17
+
18
+ it "sets the base_path property" do
19
+ metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath")
20
+
21
+ expect(metadata.base_path).to eq("basePath")
22
+ end
23
+
24
+ it "sets the controller_base_path property" do
25
+ metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath")
26
+
27
+ expect(metadata.controller_base_path).to eq("controllerBasePath")
28
+ end
29
+
30
+ it "defaults the swagger_version property to DEFAULT_SWAGGER_VERSION" do
31
+ metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath")
32
+
33
+ expect(metadata.swagger_version).to eq(described_class::DEFAULT_SWAGGER_VERSION)
34
+ end
35
+
36
+ it "allows the swagger_version property to be_overriden" do
37
+ metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath", swagger_version: "2.0")
38
+
39
+ expect(metadata.swagger_version).to eq("2.0")
40
+ end
41
+
42
+
43
+ it "defaults the camelize_model_properties property to true" do
44
+ metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath")
45
+
46
+ expect(metadata.camelize_model_properties).to eq(true)
47
+ end
48
+
49
+ it "allows the camelize_model_properties property to be overidden" do
50
+ metadata = described_class.new("1.0", "path", "basePath", "controllerBasePath", camelize_model_properties: false)
51
+
52
+ expect(metadata.camelize_model_properties).to eq(false)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,198 @@
1
+ require "spec_helper"
2
+
3
+ describe Swagger::Docs::ApiDeclarationFile do
4
+ let(:apis) do
5
+ [
6
+ {
7
+ :path=>"sample/{id}",
8
+ :operations=>[
9
+ {
10
+ :summary=>"Updates an existing User",
11
+ :parameters=>[
12
+ {:param_type=>:path, :name=>:id, :type=>:integer, :description=>"User Id", :required=>true},
13
+ {:param_type=>:form, :name=>:first_name, :type=>:string, :description=>"First name", :required=>false},
14
+ {:param_type=>:form, :name=>:last_name, :type=>:string, :description=>"Last name", :required=>false},
15
+ {:param_type=>:form, :name=>:email, :type=>:string, :description=>"Email address", :required=>false},
16
+ {:param_type=>:form, :name=>:tag, :type=>:Tag, :description=>"Tag object", :required=>true}
17
+ ],
18
+ :response_messages=>[
19
+ {:code=>401, :message=>"Unauthorized"},
20
+ {:code=>404, :message=>"Not Found"},
21
+ {:code=>406, :message=>"Not Acceptable"}
22
+ ],
23
+ :notes=>"Only the given fields are updated.",
24
+ :method=>:put,
25
+ :nickname=>"Api::V1::Sample#update"
26
+ }
27
+ ]
28
+ }
29
+ ]
30
+ end
31
+
32
+ let(:models) do
33
+ {
34
+ :Tag=>
35
+ {
36
+ :id=>:Tag,
37
+ :required=>[:id],
38
+ :properties=>
39
+ {
40
+ :id=>{:type=>:integer, :description=>"User Id"},
41
+ :first_name=>{:type=>:string, :description=>"First Name"},
42
+ :last_name=>{:type=>:string, :description=>"Last Name"}
43
+ },
44
+ :description=>"A Tag object."
45
+ }
46
+ }
47
+ end
48
+
49
+ let(:metadata) do
50
+ Swagger::Docs::ApiDeclarationFileMetadata.new("1.0", "api/v1/sample", "http://api.no.where/", "")
51
+ end
52
+
53
+ describe "#generate_resource" do
54
+
55
+ it "generates the appropriate response" do
56
+ declaration = described_class.new(metadata, apis, models)
57
+
58
+ expected_response = {
59
+ "apiVersion"=> declaration.api_version,
60
+ "swaggerVersion"=> declaration.swagger_version,
61
+ "basePath"=> declaration.base_path,
62
+ "apis"=> declaration.apis,
63
+ "resourcePath"=> declaration.resource_path,
64
+ :models=> declaration.models,
65
+ "resourceFilePath" => declaration.resource_file_path
66
+ }
67
+ expect(declaration.generate_resource).to eq(expected_response)
68
+ end
69
+ end
70
+
71
+ describe "#base_path" do
72
+ it "returns metadata.base_path" do
73
+ metadata = double("metadata", base_path: "/hello")
74
+ declaration = described_class.new(metadata, apis, models)
75
+ expect(declaration.base_path).to eq(metadata.base_path)
76
+ end
77
+ end
78
+
79
+ describe "#path" do
80
+ it "returns metadata.path" do
81
+ metadata = double("metadata", path: "/hello")
82
+ declaration = described_class.new(metadata, apis, models)
83
+ expect(declaration.path).to eq(metadata.path)
84
+ end
85
+ end
86
+
87
+ describe "#controller_base_path" do
88
+ it "returns metadata.controller_base_path" do
89
+ metadata = double("metadata", controller_base_path: "/hello")
90
+ declaration = described_class.new(metadata, apis, models)
91
+ expect(declaration.controller_base_path).to eq(metadata.controller_base_path)
92
+ end
93
+ end
94
+
95
+ describe "#swagger_version" do
96
+ it "returns metadata.swagger_version" do
97
+ metadata = double("metadata", swagger_version: "1.2")
98
+ declaration = described_class.new(metadata, apis, models)
99
+ expect(declaration.swagger_version).to eq(metadata.swagger_version)
100
+ end
101
+ end
102
+
103
+ describe "#api_version" do
104
+ it "returns metadata.api_version" do
105
+ metadata = double("metadata", api_version: "1.0")
106
+ declaration = described_class.new(metadata, apis, models)
107
+ expect(declaration.api_version).to eq(metadata.api_version)
108
+ end
109
+ end
110
+
111
+ describe "#camelize_model_properties" do
112
+ it "returns metadata.camelize_model_properties" do
113
+ metadata = double("metadata", camelize_model_properties: false)
114
+ declaration = described_class.new(metadata, apis, models)
115
+ expect(declaration.camelize_model_properties).to eq(metadata.camelize_model_properties)
116
+ end
117
+ end
118
+
119
+ describe "#models" do
120
+ context "with camelize_model_properties set to true" do
121
+ it "returns a models hash that's ready for output" do
122
+ declaration = described_class.new(metadata, apis, models)
123
+ allow(declaration).to receive(:camelize_model_properties).and_return(true)
124
+ expected_models_hash = {
125
+ "Tag" =>
126
+ {
127
+ "id" => :Tag,
128
+ "required" =>[:id],
129
+ "properties" =>
130
+ {
131
+ "id" =>{"type"=>:integer, "description"=>"User Id"},
132
+ "firstName"=>{"type"=>:string, "description"=>"First Name"},
133
+ "lastName"=>{"type"=>:string, "description"=>"Last Name"},
134
+ },
135
+ "description"=>"A Tag object."
136
+ }
137
+ }
138
+
139
+ expect(declaration.models).to eq(expected_models_hash)
140
+ end
141
+ end
142
+
143
+ context "with camelize_model_properties set to false" do
144
+ it "returns a models hash that's ready for output" do
145
+ declaration = described_class.new(metadata, apis, models)
146
+ allow(declaration).to receive(:camelize_model_properties).and_return(false)
147
+ expected_models_hash = {
148
+ "Tag" =>
149
+ {
150
+ "id" => :Tag,
151
+ "required" =>[:id],
152
+ "properties" =>
153
+ {
154
+ "id" =>{"type"=>:integer, "description"=>"User Id"},
155
+ "first_name"=>{"type"=>:string, "description"=>"First Name"},
156
+ "last_name"=>{"type"=>:string, "description"=>"Last Name"},
157
+ },
158
+ "description"=>"A Tag object."
159
+ }
160
+ }
161
+
162
+ expect(declaration.models).to eq(expected_models_hash)
163
+ end
164
+ end
165
+ end
166
+
167
+ describe "#apis" do
168
+ it "returns a api hash that's ready for output" do
169
+ declaration = described_class.new(metadata, apis, models)
170
+ expected_apis_array = [
171
+ {
172
+ "path"=>"sample/{id}",
173
+ "operations"=>[
174
+ {
175
+ "summary"=>"Updates an existing User",
176
+ "parameters"=>[
177
+ {"paramType"=>:path, "name"=>:id, "type"=>:integer, "description"=>"User Id", "required"=>true},
178
+ {"paramType"=>:form, "name"=>:first_name, "type"=>:string, "description"=>"First name", "required"=>false},
179
+ {"paramType"=>:form, "name"=>:last_name, "type"=>:string, "description"=>"Last name", "required"=>false},
180
+ {"paramType"=>:form, "name"=>:email, "type"=>:string, "description"=>"Email address", "required"=>false},
181
+ {"paramType"=>:form, "name"=>:tag, "type"=>:Tag, "description"=>"Tag object", "required"=>true}
182
+ ],
183
+ "responseMessages"=>[
184
+ {"code"=>401, "message"=>"Unauthorized"},
185
+ {"code"=>404, "message"=>"Not Found"},
186
+ {"code"=>406, "message"=>"Not Acceptable"}
187
+ ],
188
+ "notes"=>"Only the given fields are updated.",
189
+ "method"=>:put,
190
+ "nickname"=>"Api::V1::Sample#update"
191
+ }
192
+ ]
193
+ }
194
+ ]
195
+ expect(declaration.apis).to eq(expected_apis_array)
196
+ end
197
+ end
198
+ end
@@ -321,7 +321,7 @@ describe Swagger::Docs::Generator do
321
321
  }
322
322
  }
323
323
  }
324
- expect(models['tag']).to eq expected_model
324
+ expect(models['Tag']).to eq expected_model
325
325
  end
326
326
  end
327
327
  end
data.tar.gz.sig CHANGED
Binary file
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.6
4
+ version: 0.1.7
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-05-21 00:00:00.000000000 Z
33
+ date: 2014-05-30 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: bundler
@@ -118,6 +118,8 @@ files:
118
118
  - Rakefile
119
119
  - certs/gem-public_cert.pem
120
120
  - lib/swagger/docs.rb
121
+ - lib/swagger/docs/api_declaration_file.rb
122
+ - lib/swagger/docs/api_declaration_file_metadata.rb
121
123
  - lib/swagger/docs/config.rb
122
124
  - lib/swagger/docs/dsl.rb
123
125
  - lib/swagger/docs/generator.rb
@@ -129,6 +131,8 @@ files:
129
131
  - spec/fixtures/controllers/application_controller.rb
130
132
  - spec/fixtures/controllers/ignored_controller.rb
131
133
  - spec/fixtures/controllers/sample_controller.rb
134
+ - spec/lib/swagger/docs/api_declaration_file_metadata_spec.rb
135
+ - spec/lib/swagger/docs/api_declaration_file_spec.rb
132
136
  - spec/lib/swagger/docs/config_spec.rb
133
137
  - spec/lib/swagger/docs/generator_spec.rb
134
138
  - spec/spec_helper.rb
@@ -162,6 +166,8 @@ test_files:
162
166
  - spec/fixtures/controllers/application_controller.rb
163
167
  - spec/fixtures/controllers/ignored_controller.rb
164
168
  - spec/fixtures/controllers/sample_controller.rb
169
+ - spec/lib/swagger/docs/api_declaration_file_metadata_spec.rb
170
+ - spec/lib/swagger/docs/api_declaration_file_spec.rb
165
171
  - spec/lib/swagger/docs/config_spec.rb
166
172
  - spec/lib/swagger/docs/generator_spec.rb
167
173
  - spec/spec_helper.rb
metadata.gz.sig CHANGED
@@ -1 +1,2 @@
1
- ,|�em����}�z�������m��]ȿ8V��3V���AZ����YE�΅|~�,/_,sOׄ�>�4ۿ��3�}|�S$ը�` 0 �n�Ӷ+��5N[o�"�N%�]6]�túz�}�ݩ�Z\��q�����;ep�2O8$�}������\b� �yLh������3$�B�;��DJ�S��'�"� ��FeCۂ�F������[��n=���o��zQlC_)���η`XA+W �� W�m�L8c����
1
+ p.�>���P�S?�z�~7x��?WeT$��b�����=�$�tI��P ����Z�՘rTi'dI����_�voaK'��/�Q��9D
2
+ 齱������ )5|�v�ng��v]Q�grx��yW�dx?��^�%r�E��Ͻ=�-����F%�