swagalicious 0.5.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89c4466d6c9ea1d244b293d16b86edf9f1611b3c9fd0f6057cd3dcf63a8328ed
4
- data.tar.gz: 0e879d017afc95a8a4f0f27a02e0dad9650eb2eefb07c468d8b732c2149ef70f
3
+ metadata.gz: abb72dfd425ef772e7d2c0cf1ac1079d60cf9d6310b790dc9eef4454e145d7dd
4
+ data.tar.gz: 3c919667c104eba4d2e01d4177e68e4bcc5d57a8a0d33ee02b9fcfa79d3cf899
5
5
  SHA512:
6
- metadata.gz: 2de2c29b4bb1dd5a19fed86010160a445b5f4add5a05a01608761704101f502173dc537c863d7cff8577cac0b8047d7c74a8d4018bfaed1ec61bb63af0918e07
7
- data.tar.gz: ec3740df707edf4485ed2d50294f6a696bec691541f102c4b63094e9218d5973a8406299570eb642d10721867073858e41919e40e1a96fb8f5674ea318021b84
6
+ metadata.gz: 12939af351eaebed0316bff2af98b58fcf7f39041311920bab9338f163e5dc06d2575148fedcb22ab92581f91b9f9d8e28dbffb0bef60cc97faccc2a97bf9718
7
+ data.tar.gz: 8003706edc0671b7cce79a72d9098827de9d114b0d42502684ea980bf4ee22deab1a998603710a82a55321fa0c318b42414d7f047ae5a2028214ae2a8354a647
data/Gemfile.lock CHANGED
@@ -1,11 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swagalicious (0.5.0)
4
+ swagalicious (1.0.0)
5
5
  faraday (~> 2.0)
6
6
  faraday-rack
7
7
  json-schema
8
8
  oj (~> 3.11.0)
9
+ ox (~> 2.14.14)
9
10
  rack-test (~> 2.0.2)
10
11
  rspec (~> 3)
11
12
 
@@ -20,36 +21,45 @@ GEM
20
21
  zeitwerk (~> 2.3)
21
22
  addressable (2.8.1)
22
23
  public_suffix (>= 2.0.2, < 6.0)
24
+ ast (2.4.2)
23
25
  byebug (11.1.3)
24
26
  coderay (1.1.3)
25
27
  concurrent-ruby (1.1.10)
26
28
  diff-lcs (1.5.0)
27
29
  factory_bot (6.1.0)
28
30
  activesupport (>= 5.0.0)
29
- faraday (2.5.2)
31
+ faraday (2.7.3)
30
32
  faraday-net_http (>= 2.0, < 3.1)
31
33
  ruby2_keywords (>= 0.0.4)
32
- faraday-net_http (3.0.0)
34
+ faraday-net_http (3.0.2)
33
35
  faraday-rack (2.0.0)
34
36
  faraday (~> 2.0)
35
37
  i18n (1.10.0)
36
38
  concurrent-ruby (~> 1.0)
39
+ json (2.6.3)
37
40
  json-schema (3.0.0)
38
41
  addressable (>= 2.8)
39
42
  method_source (1.0.0)
40
43
  minitest (5.16.1)
41
44
  oj (3.11.8)
45
+ ox (2.14.14)
46
+ parallel (1.22.1)
47
+ parser (3.2.1.0)
48
+ ast (~> 2.4.1)
42
49
  pry (0.13.1)
43
50
  coderay (~> 1.1)
44
51
  method_source (~> 1.0)
45
52
  pry-byebug (3.9.0)
46
53
  byebug (~> 11.0)
47
54
  pry (~> 0.13.0)
48
- public_suffix (5.0.0)
49
- rack (3.0.0)
55
+ public_suffix (5.0.1)
56
+ rack (3.0.4.1)
50
57
  rack-test (2.0.2)
51
58
  rack (>= 1.3)
59
+ rainbow (3.1.1)
52
60
  rake (12.3.3)
61
+ regexp_parser (2.7.0)
62
+ rexml (3.2.5)
53
63
  rspec (3.11.0)
54
64
  rspec-core (~> 3.11.0)
55
65
  rspec-expectations (~> 3.11.0)
@@ -63,9 +73,28 @@ GEM
63
73
  diff-lcs (>= 1.2.0, < 2.0)
64
74
  rspec-support (~> 3.11.0)
65
75
  rspec-support (3.11.0)
76
+ rubocop (1.48.0)
77
+ json (~> 2.3)
78
+ parallel (~> 1.10)
79
+ parser (>= 3.2.0.0)
80
+ rainbow (>= 2.2.2, < 4.0)
81
+ regexp_parser (>= 1.8, < 3.0)
82
+ rexml (>= 3.2.5, < 4.0)
83
+ rubocop-ast (>= 1.26.0, < 2.0)
84
+ ruby-progressbar (~> 1.7)
85
+ unicode-display_width (>= 2.4.0, < 3.0)
86
+ rubocop-ast (1.27.0)
87
+ parser (>= 3.2.1.0)
88
+ rubocop-capybara (2.17.1)
89
+ rubocop (~> 1.41)
90
+ rubocop-rspec (2.19.0)
91
+ rubocop (~> 1.33)
92
+ rubocop-capybara (~> 2.17)
93
+ ruby-progressbar (1.13.0)
66
94
  ruby2_keywords (0.0.5)
67
95
  tzinfo (2.0.4)
68
96
  concurrent-ruby (~> 1.0)
97
+ unicode-display_width (2.4.2)
69
98
  zeitwerk (2.6.0)
70
99
 
71
100
  PLATFORMS
@@ -76,7 +105,9 @@ DEPENDENCIES
76
105
  pry-byebug (~> 3.9.0)
77
106
  rake (~> 12.0)
78
107
  rspec (~> 3.0)
108
+ rubocop (~> 1.48.0)
109
+ rubocop-rspec (~> 2.19.0)
79
110
  swagalicious!
80
111
 
81
112
  BUNDLED WITH
82
- 2.3.20
113
+ 2.3.17
@@ -57,8 +57,10 @@ class Swagalicious
57
57
  end
58
58
 
59
59
  def header(name, attributes)
60
- metadata[:response][:headers] ||= {}
61
- metadata[:response][:headers][name] = attributes
60
+ header_name = attributes.delete(:variable) || name
61
+
62
+ metadata[:response][:headers] ||= {}
63
+ metadata[:response][:headers][header_name] = attributes
62
64
  end
63
65
 
64
66
  # NOTE: Similar to 'description', 'examples' need to handle the case when
@@ -1,7 +1,11 @@
1
1
  require "faraday"
2
2
  require "faraday/rack"
3
3
  require "rack"
4
+ require "json"
4
5
  require "oj"
6
+ require "ox"
7
+ require "yaml"
8
+ require "active_support/core_ext/hash/indifferent_access"
5
9
 
6
10
  require_relative "response_validator"
7
11
 
@@ -9,6 +13,55 @@ class Swagalicious
9
13
  module ExampleHelpers
10
14
  include Rack::Test::Methods
11
15
 
16
+ def self.raise_invalid_response(response:, request:, message:)
17
+ raise InvalidResponseTypeError.new(
18
+ headers: response.headers,
19
+ message: message,
20
+ request: request,
21
+ response: response,
22
+ status: response.status,
23
+ )
24
+ end
25
+
26
+ class InvalidResponseTypeError < RuntimeError
27
+ attr_reader :status, :_message, :headers, :response, :request
28
+
29
+ def initialize(status:, message:, headers:, response:, request:)
30
+ @headers = headers
31
+ @_message = message
32
+ @request = request
33
+ @response = response
34
+ @status = status
35
+ end
36
+
37
+ def inspect
38
+ JSON.pretty_generate(to_h)
39
+ end
40
+
41
+ def to_h
42
+ hash = {
43
+ headers: headers.to_h,
44
+ message: _message,
45
+ request: request.slice(:verb, :path, :headers),
46
+ status: status,
47
+ }
48
+
49
+ if parsed_body = Parser.new(request: request, response: response).parse(raise_on_invalid: false)
50
+ hash[:parsed_response] = parsed_body
51
+ end
52
+
53
+ hash
54
+ end
55
+
56
+ def to_s
57
+ "Received unexpected or unparseable response with status code #{status} for #{request[:verb].upcase} #{request[:path]}: #{_message}"
58
+ end
59
+
60
+ def message
61
+ inspect
62
+ end
63
+ end
64
+
12
65
  class MockResponse
13
66
  attr_reader :body, :status, :headers
14
67
 
@@ -19,6 +72,44 @@ class Swagalicious
19
72
  end
20
73
  end
21
74
 
75
+ class Parser
76
+ attr_accessor :body
77
+ attr_reader :content_type, :request, :response
78
+
79
+ def initialize(response:, request:)
80
+ @content_type = response.headers["Content-Type"]
81
+ @body = response.body
82
+ @response = response
83
+ @request = request
84
+ end
85
+
86
+ def parse(raise_on_invalid: true)
87
+ # Redirections shouldnt be parsed
88
+ if response.status >= 300 && response.status <= 399
89
+ return
90
+ end
91
+
92
+ case content_type
93
+ when /json/
94
+ self.body = "{}" if self.body.empty?
95
+
96
+ Oj.load(self.body, symbol_keys: true)
97
+ when /ya?ml/
98
+ body = "---" if self.body.empty?
99
+
100
+ (YAML.load(self.body) || {}).with_indifferent_access
101
+ when /xml/
102
+ (Ox.load(self.body, mode: :hash_no_attrs) || {}).with_indifferent_access
103
+ when "", nil
104
+ self.body
105
+ else
106
+ return unless raise_on_invalid
107
+
108
+ Swagalicious::ExampleHelpers.raise_invalid_response(response: response, request: request, message: "Invalid Content-Type header #{response.headers["Content-Type"]}")
109
+ end
110
+ end
111
+ end
112
+
22
113
  def app
23
114
  @app ||= if defined?(Rails)
24
115
  Rails.application
@@ -52,10 +143,9 @@ class Swagalicious
52
143
  end
53
144
  end
54
145
 
55
- body = response.body
56
- body = "{}" if body.empty?
146
+ Swagalicious::ExampleHelpers.raise_invalid_response(response: response, request: request, message: "Received unexpected response code") unless response.status.to_s == metadata[:response][:code].to_s
57
147
 
58
- @body = Oj.load(body, symbol_keys: true)
148
+ @body = Parser.new(request: request, response: response).parse
59
149
 
60
150
  metadata[:paths] ||= []
61
151
  metadata[:paths] << request[:path]
@@ -91,12 +91,14 @@ class Swagalicious
91
91
 
92
92
  request[:path] = template.tap do |path_template|
93
93
  parameters.select { |p| p[:in] == :path }.each do |p|
94
- path_template.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s)
94
+ variable_name = p[:variable_name] || p[:name]
95
+ path_template.gsub!("{#{p[:name]}}", example.send(variable_name).to_s)
95
96
  end
96
97
 
97
98
  parameters.select { |p| p[:in] == :query }.each_with_index do |p, i|
99
+ variable_name = p[:variable_name] || p[:name]
98
100
  path_template.concat(i.zero? ? "?" : "&")
99
- path_template.concat(build_query_string_part(p, example.send(p[:name])))
101
+ path_template.concat(build_query_string_part(p, example.send(variable_name)))
100
102
  end
101
103
  end
102
104
  end
@@ -122,7 +124,11 @@ class Swagalicious
122
124
  def add_headers(request, metadata, swagger_doc, parameters, example)
123
125
  tuples = parameters
124
126
  .select { |p| p[:in] == :header }
125
- .map { |p| [p[:name], example.send(p[:name]).to_s] }
127
+ .map do |p|
128
+ variable_name = p[:variable_name] || p[:name]
129
+
130
+ [p[:name], example.send(variable_name).to_s]
131
+ end
126
132
 
127
133
  # Accept header
128
134
  produces = metadata[:operation][:produces] || swagger_doc[:produces]
@@ -55,7 +55,7 @@ class Swagalicious
55
55
  private
56
56
 
57
57
  def pretty_generate(doc)
58
- if config.swagger_format == :yaml
58
+ if config.swagger_format == :yaml || doc.delete(:swagger_format) == :yaml
59
59
  clean_doc = yaml_prepare(doc)
60
60
  YAML.dump(clean_doc)
61
61
  else # config errors are thrown in "def swagger_format", no throw needed here
@@ -1,3 +1,3 @@
1
1
  class Swagalicious
2
- VERSION = "0.5.2"
2
+ VERSION = "1.0.0"
3
3
  end
data/swagalicious.gemspec CHANGED
@@ -28,9 +28,12 @@ Gem::Specification.new do |spec|
28
28
  spec.add_dependency "faraday-rack"
29
29
  spec.add_dependency "json-schema"
30
30
  spec.add_dependency "oj", "~> 3.11.0"
31
+ spec.add_dependency "ox", "~> 2.14.14"
31
32
  spec.add_dependency "rack-test", "~> 2.0.2"
32
33
  spec.add_dependency "rspec", "~> 3"
33
34
 
35
+ spec.add_development_dependency "rubocop", "~> 1.48.0"
36
+ spec.add_development_dependency "rubocop-rspec", "~> 2.19.0"
34
37
  spec.add_development_dependency "factory_bot", "~> 6.1.0"
35
38
  spec.add_development_dependency "pry-byebug", "~> 3.9.0"
36
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swagalicious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Howe
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-14 00:00:00.000000000 Z
11
+ date: 2023-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.11.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: ox
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.14.14
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.14.14
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rack-test
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,34 @@ dependencies:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
110
  version: '3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.48.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.48.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 2.19.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 2.19.0
97
139
  - !ruby/object:Gem::Dependency
98
140
  name: factory_bot
99
141
  requirement: !ruby/object:Gem::Requirement
@@ -160,7 +202,7 @@ metadata:
160
202
  allowed_push_host: https://rubygems.org
161
203
  homepage_uri: https://github.com/ehowe/swagalicious
162
204
  source_code_uri: https://github.com/ehowe/swagalicious
163
- post_install_message:
205
+ post_install_message:
164
206
  rdoc_options: []
165
207
  require_paths:
166
208
  - lib
@@ -175,8 +217,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
217
  - !ruby/object:Gem::Version
176
218
  version: '0'
177
219
  requirements: []
178
- rubygems_version: 3.0.3.1
179
- signing_key:
220
+ rubygems_version: 3.2.33
221
+ signing_key:
180
222
  specification_version: 4
181
223
  summary: RSwag without Rails
182
224
  test_files: []