rspec_api_documentation-open_api 0.3 → 0.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
  SHA256:
3
- metadata.gz: 3c66175610afed0eca49004505d516f26a383e7eadcb1c3bf7c8f403d618e7f4
4
- data.tar.gz: ab0bce8839245d60d7932566f125d5dfecd63a2640dcf2ffcc32ce36f8152524
3
+ metadata.gz: ce385336b5da4fccad72237ddc1fa72631d18e8f4c63613e4926b79add0a9826
4
+ data.tar.gz: 67ef06d2de2781000aba3e280c13d3caeca075d2540158425241fc4fd035a11f
5
5
  SHA512:
6
- metadata.gz: 176d6c576ab36496d54fe59fbb2ab0abe237e16c5dad8ee5fc9b3d1b04585c182c239854b8c2ecadd6b45d0aef0d61eb96ce5a9cb0b34308ce6d096a1054b853
7
- data.tar.gz: 2c08f46097a4c991d053f8981294654678c07c067d33e5d77db12be557cb7d253e64634a4217d92cb01f273d4122b8f299c432f8d5a55479be8e5b6382bc6db1
6
+ metadata.gz: 6682f1fadce890fa519b9a299652587277427a3ce9fab2f3d0df1cfc7c07eaf7de5cf4d46c71126d25939e42581130a7f3c7be4504566ed6678ccd141e2672f8
7
+ data.tar.gz: 4c3f2b975dc713f685fccbbdb06d39daf65b4511d75c93fc9272145c032fad13c389911b437d87ef7fe55b4df2df3e89fcf1c52bc682df9a28172e4d03204350
data/.rubocop.yml CHANGED
@@ -1,9 +1,9 @@
1
- inherit_from:
2
- - https://raw.githubusercontent.com/cookpad/guides/master/.rubocop.yml
3
-
4
1
  AllCops:
5
2
  TargetRubyVersion: 2.5
6
3
 
4
+ Metrics/LineLength:
5
+ Max: 160
6
+
7
7
  Rails/UnknownEnv:
8
8
  Environments:
9
9
  - production
data/bin/setup CHANGED
@@ -4,5 +4,3 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,5 +1,5 @@
1
1
  module RspecApiDocumentation
2
2
  module OpenApi
3
- VERSION = "0.3".freeze
3
+ VERSION = '0.4'.freeze
4
4
  end
5
5
  end
@@ -2,7 +2,7 @@ module RspecApiDocumentation
2
2
  module Writers
3
3
  class OpenApiJsonWriter < OpenApiWriter
4
4
  def write
5
- File.open(configuration.docs_dir.join("open_api.json"), "w") do |f|
5
+ File.open(configuration.docs_dir.join('open_api.json'), 'w') do |f|
6
6
  f.write JSON.pretty_generate(get_hash)
7
7
  end
8
8
  end
@@ -1,7 +1,7 @@
1
- require "json"
2
- require "yaml"
3
- require "cgi"
4
- require "rspec_api_documentation/writers/json_writer"
1
+ require 'json'
2
+ require 'yaml'
3
+ require 'cgi'
4
+ require 'rspec_api_documentation/writers/json_writer'
5
5
 
6
6
  module RspecApiDocumentation
7
7
  module Writers
@@ -9,11 +9,11 @@ module RspecApiDocumentation
9
9
  attr_writer :types, :swagger
10
10
 
11
11
  def write
12
- File.open(configuration.docs_dir.join("open_api.json"), "w") do |f|
12
+ File.open(configuration.docs_dir.join('open_api.json'), 'w') do |f|
13
13
  f.write JSON.pretty_generate(get_hash)
14
14
  end
15
15
 
16
- File.open(configuration.docs_dir.join("open_api.yaml"), "w") do |f|
16
+ File.open(configuration.docs_dir.join('open_api.yaml'), 'w') do |f|
17
17
  f.write get_hash.to_yaml
18
18
  end
19
19
  end
@@ -21,157 +21,157 @@ module RspecApiDocumentation
21
21
  def get_hash
22
22
  index.examples.each do |rspec_example|
23
23
  api = JSONExample.new(rspec_example, configuration).as_json.deep_stringify_keys
24
- description = api["description"]
25
- route = api["route"].gsub(%r{\:version\/}, "")
24
+ description = api['description']
25
+ route = api['route'].gsub(%r{\:version\/}, '')
26
26
  route = route.gsub(%r{\:([^\/]+)}, '{\1}')
27
- params = route.scan(%r{\{([^\/]+)\}}).map { |param| { "name" => param[0], "in" => "path", "required" => true, "schema" => { "type" => "string" } } }
28
- swagger["paths"][route] = swagger["paths"][route] || {}
27
+ params = route.scan(%r{\{([^\/]+)\}}).map { |param| { 'name' => param[0], 'in' => 'path', 'required' => true, 'schema' => { 'type' => 'string' } } }
28
+ swagger['paths'][route] = swagger['paths'][route] || {}
29
29
  responses = {}
30
- req = api["requests"][0]
31
- method = req["request_method"].downcase
32
- result = JSON.parse(req["response_body"]) if req["response_body"]
30
+ req = api['requests'][0]
31
+ method = req['request_method'].downcase
32
+ result = JSON.parse(req['response_body']) if req['response_body']
33
33
  properties = {}
34
34
  result&.each { |k, v| properties[k] = get_properties(v) }
35
35
 
36
- responses[req["response_status"]] = {
37
- "description" => req["response_status_text"],
38
- "headers" => req["response_headers"].map { |name, _v| [name, { "schema" => { "type" => "string" } }] }.to_h,
39
- "content" => {
40
- "application/json" => {
41
- "schema" => {
42
- "type" => "object",
43
- "properties" => properties
36
+ responses[req['response_status']] = {
37
+ 'description' => req['response_status_text'],
38
+ 'headers' => req['response_headers'].map { |name, _v| [name, { 'schema' => { 'type' => 'string' } }] }.to_h,
39
+ 'content' => {
40
+ 'application/json' => {
41
+ 'schema' => {
42
+ 'type' => 'object',
43
+ 'properties' => properties
44
44
  }
45
45
  }
46
46
  }
47
47
  }
48
- api["parameters"]&.each do |param|
49
- params.push("name" => param["name"],
50
- "in" => "query",
51
- "schema" => {
52
- "type" => "string"
48
+ api['parameters']&.each do |param|
49
+ params.push('name' => param['name'],
50
+ 'in' => 'query',
51
+ 'schema' => {
52
+ 'type' => 'string'
53
53
  },
54
- "description" => param["description"],
55
- "required" => param["required"] || false)
54
+ 'description' => param['description'],
55
+ 'required' => param['required'] || false)
56
56
  end
57
57
 
58
- req["request_query_parameters"]&.each do |name, value|
59
- params.push("name" => name,
60
- "in" => "query",
61
- "schema" => {
62
- "type" => "string"
58
+ req['request_query_parameters']&.each do |name, value|
59
+ params.push('name' => name,
60
+ 'in' => 'query',
61
+ 'schema' => {
62
+ 'type' => 'string'
63
63
  },
64
- "example" => value)
64
+ 'example' => value)
65
65
  end
66
- if req["request_content_type"] == "application/x-www-form-urlencoded" && api["requests"][0]["request_body"]
67
- req["request_body"].scan(/([^\&\=]*)=([^\&]*)/).map do |body|
68
- params.push("name" => body[0],
69
- "in" => "query",
70
- "schema" => {
71
- "type" => "string"
66
+ if req['request_content_type'] == 'application/x-www-form-urlencoded' && api['requests'][0]['request_body']
67
+ req['request_body'].scan(/([^\&\=]*)=([^\&]*)/).map do |body|
68
+ params.push('name' => body[0],
69
+ 'in' => 'query',
70
+ 'schema' => {
71
+ 'type' => 'string'
72
72
  },
73
- "example" => CGI.unescape(body[1]))
73
+ 'example' => CGI.unescape(body[1]))
74
74
  end
75
75
  end
76
- swagger["paths"][route][method] = swagger["paths"][route][method] || {}
77
- swagger["paths"][route][method]["parameters"] = swagger["paths"][route][method]["parameters"] || []
76
+ swagger['paths'][route][method] = swagger['paths'][route][method] || {}
77
+ swagger['paths'][route][method]['parameters'] = swagger['paths'][route][method]['parameters'] || []
78
78
 
79
- req["request_headers"]&.each do |name, value|
80
- if name == "Authorization"
81
- swagger["paths"][route][method]["security"] = if /Bearer (.*)/.match?(value)
82
- unless responses[401] && responses["401"]
79
+ req['request_headers']&.each do |name, value|
80
+ if name == 'Authorization'
81
+ swagger['paths'][route][method]['security'] = if /Bearer (.*)/.match?(value)
82
+ unless responses[401] && responses['401']
83
83
  responses[401] = {
84
- "description" => "Unauthorized Access",
85
- "content" => {
86
- "application/json" => {
87
- "schema" => {
88
- "$ref" => "#/components/schemas/unauthorized"
84
+ 'description' => 'Unauthorized Access',
85
+ 'content' => {
86
+ 'application/json' => {
87
+ 'schema' => {
88
+ '$ref' => '#/components/schemas/unauthorized'
89
89
  }
90
90
  }
91
91
  }
92
92
  }
93
93
  end
94
- [{ "bearerAuth" => [] }]
94
+ [{ 'bearerAuth' => [] }]
95
95
  else
96
- [{ "basicAuth" => [] }]
96
+ [{ 'basicAuth' => [] }]
97
97
  end
98
98
  else
99
- params.unshift("name" => name,
100
- "in" => "header",
101
- "schema" => {
102
- "type" => "string"
99
+ params.unshift('name' => name,
100
+ 'in' => 'header',
101
+ 'schema' => {
102
+ 'type' => 'string'
103
103
  },
104
- "example" => value,
105
- "required" => true)
104
+ 'example' => value,
105
+ 'required' => true)
106
106
  end
107
107
  end
108
108
  params.each do |param|
109
109
  has = false
110
- swagger["paths"][route][method]["parameters"].each_with_index do |p, i|
111
- if p["name"] == param["name"]
112
- swagger["paths"][route][method]["parameters"][i]["example"] = swagger["paths"][route][method]["parameters"][i]["example"] || param["example"]
110
+ swagger['paths'][route][method]['parameters'].each_with_index do |p, i|
111
+ if p['name'] == param['name']
112
+ swagger['paths'][route][method]['parameters'][i]['example'] = swagger['paths'][route][method]['parameters'][i]['example'] || param['example']
113
113
  has = true
114
114
  end
115
115
  end
116
- swagger["paths"][route][method]["parameters"].push(param) unless has
116
+ swagger['paths'][route][method]['parameters'].push(param) unless has
117
117
  end
118
- desc = swagger["paths"][route][method]["description"] || ""
119
- swagger["paths"][route][method]["description"] = desc + "- #{description}"
120
- if params.empty?
121
- swagger["paths"][route][method]["description"] += "\n"
122
- else
123
- swagger["paths"][route][method]["description"] += ", <strong>Needed Parameters:</strong>\n - #{params.map{ |p| p['name'] }.join("\n - ")} \n"
124
- end
125
- swagger["paths"][route][method]["responses"] = responses
118
+ desc = swagger['paths'][route][method]['description'] || ''
119
+ swagger['paths'][route][method]['description'] = desc + "- #{description}"
120
+ swagger['paths'][route][method]['description'] += if params.empty?
121
+ "\n"
122
+ else
123
+ ", <strong>Needed Parameters:</strong>\n - #{params.map{ |p| p['name'] }.join("\n - ")} \n"
124
+ end
125
+ swagger['paths'][route][method]['responses'] = responses
126
126
  end
127
- swagger["components"]["schemas"] = types
127
+ swagger['components']['schemas'] = types
128
128
 
129
129
  swagger
130
130
  end
131
131
 
132
132
  def get_properties(v)
133
133
  case v.class.name
134
- when "Hash"
134
+ when 'Hash'
135
135
  props = v.map { |key, value| [key, get_properties(value)] }.to_h
136
136
  x = {
137
- "type" => "object",
138
- "properties" => props
137
+ 'type' => 'object',
138
+ 'properties' => props
139
139
  }
140
- if v["type"]
141
- type = v["type"].tr("\/", "_")
140
+ if v['type']
141
+ type = v['type'].tr('\/', '_')
142
142
  types[type] = x unless types.key?(type)
143
143
  if types.key?(type)
144
- types[type]["properties"] = hash_deep_assign(types[type]["properties"], x["properties"])
144
+ types[type]['properties'] = hash_deep_assign(types[type]['properties'], x['properties'])
145
145
  else
146
146
  types[type] = x
147
147
  end
148
- return { "$ref" => "#/components/schemas/#{type}" }
148
+ return { '$ref' => "#/components/schemas/#{type}" }
149
149
  end
150
150
  x
151
- when "Array"
151
+ when 'Array'
152
152
  {
153
- "type" => "array",
154
- "items" => get_properties(v[0])
153
+ 'type' => 'array',
154
+ 'items' => get_properties(v[0])
155
155
  }
156
- when "TrueClass", "FalseClass"
156
+ when 'TrueClass', 'FalseClass'
157
157
  {
158
- "type" => "boolean",
159
- "example" => v
158
+ 'type' => 'boolean',
159
+ 'example' => v
160
160
  }
161
- when "NilClass"
161
+ when 'NilClass'
162
162
  {
163
- "type" => "integer",
164
- "nullable" => true
163
+ 'type' => 'integer',
164
+ 'nullable' => true
165
165
  }
166
- when "Integer", "Float"
166
+ when 'Integer', 'Float'
167
167
  {
168
- "type" => "number",
169
- "example" => v
168
+ 'type' => 'number',
169
+ 'example' => v
170
170
  }
171
171
  else
172
172
  {
173
- "type" => v.class.name.downcase,
174
- "example" => v
173
+ 'type' => v.class.name.downcase,
174
+ 'example' => v
175
175
  }
176
176
  end
177
177
  end
@@ -183,18 +183,18 @@ module RspecApiDocumentation
183
183
  hash_deep_assign(target[key], other[key])
184
184
  elsif target.key?(key) && target[key].is_a?(Array) && other[key].is_a?(Array)
185
185
  target[key].push(*other[key]).uniq
186
- elsif key == "$ref"
187
- if other["$ref"] && target["$ref"] && target["$ref"] != other["$ref"]
186
+ elsif key == '$ref'
187
+ if other['$ref'] && target['$ref'] && target['$ref'] != other['$ref']
188
188
  target = {
189
- "oneOf" => [
190
- { "$ref" => target["$ref"] },
191
- { "$ref" => other["$ref"] }
189
+ 'oneOf' => [
190
+ { '$ref' => target['$ref'] },
191
+ { '$ref' => other['$ref'] }
192
192
  ]
193
193
  }
194
- elsif target.key?("oneOf")
195
- target["oneOf"].push("$ref" => other["$ref"]).uniq
194
+ elsif target.key?('oneOf')
195
+ target['oneOf'].push('$ref' => other['$ref']).uniq
196
196
  else
197
- target["$ref"] || other["$ref"]
197
+ target['$ref'] || other['$ref']
198
198
  end
199
199
  elsif target.key?(key)
200
200
  target[key] || other[key]
@@ -202,23 +202,23 @@ module RspecApiDocumentation
202
202
  value
203
203
  end
204
204
  end
205
- target = { "oneOf" => target["oneOf"] } if target.key?("oneOf")
206
- target = { "$ref" => target["$ref"] } if target.key?("$ref")
207
- target = get_properties(target["example"]) if target["example"] && target.key?("nullable")
205
+ target = { 'oneOf' => target['oneOf'] } if target.key?('oneOf')
206
+ target = { '$ref' => target['$ref'] } if target.key?('$ref')
207
+ target = get_properties(target['example']) if target['example'] && target.key?('nullable')
208
208
  target
209
209
  end
210
210
 
211
211
  def types
212
212
  @types ||= {
213
- "unauthorized" => {
214
- "properties" => {
215
- "code" => {
216
- "type" => "string",
217
- "example" => "invalid_client_credentials"
213
+ 'unauthorized' => {
214
+ 'properties' => {
215
+ 'code' => {
216
+ 'type' => 'string',
217
+ 'example' => 'invalid_client_credentials'
218
218
  },
219
- "message" => {
220
- "type" => "string",
221
- "example" => "Not found or invalid client credentials"
219
+ 'message' => {
220
+ 'type' => 'string',
221
+ 'example' => 'Not found or invalid client credentials'
222
222
  }
223
223
  }
224
224
  }
@@ -227,32 +227,32 @@ module RspecApiDocumentation
227
227
 
228
228
  def swagger
229
229
  @swagger ||= {
230
- "openapi" => "3.0.0",
231
- "info" => info,
232
- "servers" => servers,
233
- "paths" => {},
234
- "components" => {
235
- "securitySchemes" => {
236
- "bearerAuth" => {
237
- "type" => "http",
238
- "scheme" => "bearer"
230
+ 'openapi' => '3.0.0',
231
+ 'info' => info,
232
+ 'servers' => servers,
233
+ 'paths' => {},
234
+ 'components' => {
235
+ 'securitySchemes' => {
236
+ 'bearerAuth' => {
237
+ 'type' => 'http',
238
+ 'scheme' => 'bearer'
239
239
  },
240
- "basicAuth" => {
241
- "type" => "http",
242
- "scheme" => "basic"
240
+ 'basicAuth' => {
241
+ 'type' => 'http',
242
+ 'scheme' => 'basic'
243
243
  }
244
244
  },
245
- "schemas" => {}
245
+ 'schemas' => {}
246
246
  }
247
247
  }
248
248
  end
249
249
 
250
250
  def info
251
- configs["info"]
251
+ configs['info']
252
252
  end
253
253
 
254
254
  def servers
255
- configs["servers"]
255
+ configs['servers']
256
256
  end
257
257
 
258
258
  def configs
@@ -266,20 +266,20 @@ module RspecApiDocumentation
266
266
  def default_configs
267
267
  {
268
268
  info: {
269
- version: "1.0.0",
270
- title: "Open API",
271
- description: "Open API",
269
+ version: '1.0.0',
270
+ title: 'Open API',
271
+ description: 'Open API',
272
272
  contact: {
273
- name: "OpenAPI"
273
+ name: 'OpenAPI'
274
274
  }
275
275
  },
276
276
  servers: [
277
277
  {
278
- url: "http://localhost:{port}",
279
- description: "Development server",
278
+ url: 'http://localhost:{port}',
279
+ description: 'Development server',
280
280
  variables: {
281
281
  port: {
282
- default: "3000"
282
+ default: '3000'
283
283
  }
284
284
  }
285
285
  }
@@ -2,7 +2,7 @@ module RspecApiDocumentation
2
2
  module Writers
3
3
  class OpenApiYamlWriter < OpenApiWriter
4
4
  def write
5
- File.open(configuration.docs_dir.join("open_api.yaml"), "w") do |f|
5
+ File.open(configuration.docs_dir.join('open_api.yaml'), 'w') do |f|
6
6
  f.write get_hash.to_yaml
7
7
  end
8
8
  end
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ["lib"]
32
32
 
33
- spec.add_dependency "rspec_api_documentation", "~> 5"
33
+ spec.add_dependency "rspec_api_documentation", "~> 6.0"
34
34
 
35
35
  spec.add_development_dependency "bundler", "~> 1.16"
36
36
  spec.add_development_dependency "minitest", "~> 5.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_api_documentation-open_api
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaditya Taparia
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-21 00:00:00.000000000 Z
11
+ date: 2018-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec_api_documentation
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5'
19
+ version: '6.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '5'
26
+ version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement