Skip to content

Commit eeae205

Browse files
authored
Merge pull request #71 from civisanalytics/civis-5620-ruby-3-support
[CIVIS-5620] Ruby 3 support
2 parents 719960f + 13c0431 commit eeae205

File tree

9 files changed

+64
-62
lines changed

9 files changed

+64
-62
lines changed

.rubocop.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
AllCops:
2-
TargetRubyVersion: 2.1
2+
TargetRubyVersion: 2.7
3+
NewCops: enable
34

4-
Layout/IndentHeredoc:
5+
Layout/HeredocIndentation:
56
Enabled: false
67

78
Metrics/AbcSize:
@@ -15,7 +16,7 @@ Metrics/BlockLength:
1516
Metrics/ClassLength:
1617
Max: 250
1718

18-
Metrics/LineLength:
19+
Layout/LineLength:
1920
Max: 120
2021

2122
Metrics/MethodLength:
@@ -31,7 +32,7 @@ Security/YAMLLoad:
3132
Style/Documentation:
3233
Enabled: false
3334

34-
Style/FileName:
35+
Naming/FileName:
3536
Enabled: false
3637

3738
Style/FrozenStringLiteralComment:

.ruby-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.5.0
1+
3.2.1

.travis.yml

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
language: ruby
22
cache: bundler
33
rvm:
4-
- 2.0
5-
- 2.1.10
6-
- 2.2.9
7-
- 2.3.6
8-
- 2.4.3
9-
- 2.5.0
4+
- 2.7.7
5+
- 3.0.5
6+
- 3.1.3
7+
- 3.2.1
108
before_install:
119
- gem update --system
1210
- rvm @global do gem install bundler

CHANGELOG.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
77

8-
### Added
9-
10-
* Added 2.5.0 to the Travis matrix
8+
## [2.0.0]
119

1210
### Changed
1311

14-
* Bumped the Ruby version for development to 2.5.0
15-
* Bumped the Travis matrix to 2.2.9, 2.3.6, and 2.4.3
16-
* Bumped the Pry version for development to 0.11
17-
* Bumped the Rake version for development to 12
18-
* Bumped the VCR version for development to 4
12+
* Bumped the Ruby version for development to 2.7.7
13+
* Bumped the RuboCop version for development to 1.48.0
14+
* Bumped the Travis matrix to 2.7.7, 3.0.5, 3.1.3, 3.2.1
15+
* Bumped the Pry version for development to 0.14.1
16+
* Bumped the VCR version for development to 6.1
1917
* RuboCop fixes
2018

2119
## [1.1.2] - 2017-09-13

lib/swagger/diff/diff.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def new_or_changed_request_params
133133
def new_child?(req, old)
134134
idx = req.rindex('/')
135135
return false unless idx
136+
136137
key = req[0..idx]
137138
old.none? { |param| param.start_with?(key) }
138139
end
@@ -142,8 +143,10 @@ def changed_request_params_enumerator(from, to, req_msg, missing_msg)
142143
from.request_params.each do |key, old_params|
143144
new_params = to.request_params[key]
144145
next if new_params.nil?
146+
145147
(new_params[:required] - old_params[:required]).each do |req|
146148
next if new_child?(req, old_params[:all])
149+
147150
yielder << [key, format(req_msg, req: req)]
148151
end
149152
(old_params[:all] - new_params[:all]).each do |req|
@@ -181,7 +184,8 @@ def changed_response_attributes_enumerator(from, to, attr_msg, code_msg)
181184
from.response_attributes.each do |key, old_attributes|
182185
new_attributes = to.response_attributes[key]
183186
next if new_attributes.nil?
184-
old_attributes.keys.each do |code|
187+
188+
old_attributes.each_key do |code|
185189
if new_attributes.key?(code)
186190
(old_attributes[code] - new_attributes[code]).each do |resp|
187191
yielder << [key, format(attr_msg, code: code, resp: resp)]

lib/swagger/diff/specification.rb

+31-31
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,48 @@ def endpoints
1414

1515
def request_params
1616
@request_params ||= begin
17-
ret = {}
18-
@endpoint_hash.each do |key, endpoint|
19-
ret[key] = request_params_inner(params_or_nil(endpoint))
20-
end
21-
ret
22-
end
17+
ret = {}
18+
@endpoint_hash.each do |key, endpoint|
19+
ret[key] = request_params_inner(params_or_nil(endpoint))
20+
end
21+
ret
22+
end
2323
end
2424

2525
def response_attributes
2626
@response_attributes ||= begin
27-
ret = {}
28-
@endpoint_hash.each do |key, endpoint|
29-
ret[key] = response_attributes_inner(endpoint)
30-
end
31-
ret
32-
end
27+
ret = {}
28+
@endpoint_hash.each do |key, endpoint|
29+
ret[key] = response_attributes_inner(endpoint)
30+
end
31+
ret
32+
end
3333
end
3434

3535
private
3636

37-
def merge_refs!(h1, h2)
38-
h2.each do |k, v|
39-
if h1.include?(k)
40-
h1[k] += h1[k].merge(v)
37+
def merge_refs!(hash1, hash2)
38+
hash2.each do |k, v|
39+
if hash1.include?(k)
40+
hash1[k] += hash1[k].merge(v)
4141
else
42-
h1[k] = v
42+
hash1[k] = v
4343
end
4444
end
4545
end
4646

4747
def params_or_nil(endpoint)
48-
endpoint && endpoint['parameters'] || nil
48+
(endpoint && endpoint['parameters']) || nil
4949
end
5050

5151
def parse_swagger(swagger)
5252
if swagger.is_a? Hash
5353
swagger
5454
else
55-
if File.exist?(swagger) || swagger[0..7] =~ %r{^https?://}
56-
swagger = open(swagger).read
55+
if File.exist?(swagger)
56+
swagger = File.read(swagger)
57+
elsif swagger[0..7] =~ %r{^https?://}
58+
swagger = URI.parse(swagger).read
5759
end
5860
begin
5961
JSON.parse(swagger)
@@ -104,7 +106,7 @@ def refs(ref, prefix = '')
104106
{}
105107
end
106108
idx = ref.rindex('/')
107-
key = ref[idx + 1..-1]
109+
key = ref[idx + 1..]
108110
schema(defs.fetch(key, {}), prefix)
109111
end
110112

@@ -150,7 +152,7 @@ def schema(definition, prefix = '')
150152
# rubocop:enable Metrics/CyclomaticComplexity
151153
# rubocop:enable Metrics/AbcSize
152154

153-
def nested(ref, prefix, name, list = false)
155+
def nested(ref, prefix, name, list: false)
154156
# Check for cycles by testing whether name was already added to
155157
# prefix.
156158
key = "#{prefix}#{name}#{'[]' if list}"
@@ -175,20 +177,16 @@ def properties_for_param(prefix, definition)
175177
# rubocop:disable Metrics/ParameterLists
176178
def add_property(ret, prefix, name, schema, required, list)
177179
key = "#{prefix}#{name}"
178-
ret[:required].add(key) if required && required.include?(name)
179-
loc = if schema['in']
180-
schema['in']
181-
else
182-
'body'
183-
end
180+
ret[:required].add(key) if required&.include?(name)
181+
loc = schema['in'] || 'body'
184182
ret[:all].add("#{key} (in: #{loc}, type: #{schema['type']}#{'[]' if list})")
185183
end
186184
# rubocop:enable Metrics/ParameterLists
187185

188-
def properties_for_ref(prefix, name, schema, required, list = false)
186+
def properties_for_ref(prefix, name, schema, required, list: false)
189187
ret = { required: Set.new, all: Set.new }
190188
if schema['$ref']
191-
merge_refs!(ret, nested(schema['$ref'], prefix, name, list))
189+
merge_refs!(ret, nested(schema['$ref'], prefix, name, list: list))
192190
elsif schema['properties']
193191
prefix = "#{name}#{'[]' if list}/"
194192
merge_refs!(ret, properties(schema['properties'], schema['required'], prefix))
@@ -224,7 +222,7 @@ def properties(properties, required, prefix = '')
224222
ret = { required: Set.new, all: Set.new }
225223
properties.each do |name, schema|
226224
if schema['type'] == 'array'
227-
merge_refs!(ret, properties_for_ref(prefix, name, schema['items'], required, true))
225+
merge_refs!(ret, properties_for_ref(prefix, name, schema['items'], required, list: true))
228226
elsif schema['type'] == 'object' || schema['properties']
229227
if schema['allOf']
230228
# TODO: handle nested allOfs.
@@ -243,6 +241,7 @@ def properties(properties, required, prefix = '')
243241
def request_params_inner(params)
244242
ret = { required: Set.new, all: Set.new }
245243
return ret if params.nil?
244+
246245
params.each do |param|
247246
if param['in'] == 'body'
248247
merge_refs!(ret, schema(param['schema']))
@@ -287,6 +286,7 @@ def validate_swagger
287286
JSON::Validator.add_schema(json_schema)
288287
errors = JSON::Validator.fully_validate(schema_for('oai'), JSON.dump(@parsed))
289288
return if errors.empty?
289+
290290
spec = if @spec.to_s.length > 80
291291
"#{@spec.to_s[0..74]} ..."
292292
else

spec/swagger/diff/rspec_matchers_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
it 'should raise Exception without contents when incompatible' do
5555
msg = 'expected Swagger to be compatible, found:'
56-
expected = File.open('spec/fixtures/dummy.v1.json').read
56+
expected = File.read('spec/fixtures/dummy.v1.json')
5757
expect do
5858
expect('spec/fixtures/dummy.v2.json').to be_compatible_with(expected)
5959
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /#{msg}/)

spec/swagger/diff/specification_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@
2828
end
2929

3030
it 'from JSON string' do
31-
contents = File.open('spec/fixtures/petstore.json', 'r').read
31+
contents = File.read('spec/fixtures/petstore.json')
3232
spec = Swagger::Diff::Specification.new(contents)
3333
expect(spec.instance_variable_get(:@parsed)['swagger']).to eq('2.0')
3434
end
3535

3636
it 'from YAML string' do
37-
contents = File.open('spec/fixtures/petstore.yaml', 'r').read
37+
contents = File.read('spec/fixtures/petstore.yaml')
3838
spec = Swagger::Diff::Specification.new(contents)
3939
expect(spec.instance_variable_get(:@parsed)['swagger']).to eq('2.0')
4040
end
4141

4242
it 'from Hash' do
43-
contents = File.open('spec/fixtures/petstore.json', 'r').read
43+
contents = File.read('spec/fixtures/petstore.json')
4444
hash = JSON.parse(contents)
4545
spec = Swagger::Diff::Specification.new(hash)
4646
expect(spec.instance_variable_get(:@parsed)['swagger']).to eq('2.0')
@@ -224,7 +224,7 @@
224224
{"swagger"=>"2.0", "info"=>{"title"=>"Swagger Fixture", "version"=>"1.0"}, ... is not a valid Swagger specification:
225225
226226
The property '#/paths//a//get' did not contain a required property of 'responses' in schema http://swagger.io/v2/schema.json#
227-
WARNING
227+
WARNING
228228
).to_stderr
229229
end
230230
end

swagger-diff.gemspec

+7-6
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ Gem::Specification.new do |spec|
2525
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2626
spec.require_paths = ['lib']
2727

28-
spec.required_ruby_version = '~> 2.0'
28+
spec.required_ruby_version = '>= 2.7'
2929

30-
spec.add_dependency 'json-schema', '~> 2.6'
30+
spec.add_dependency 'json-schema', '~> 3.0'
3131
spec.add_dependency 'rspec-expectations', '~> 3.3'
32-
spec.add_development_dependency 'bundler', '~> 1.9'
33-
spec.add_development_dependency 'pry', '~> 0.11.3'
32+
spec.add_development_dependency 'bundler', '~> 2.4.9'
33+
spec.add_development_dependency 'pry', '~> 0.14.1'
3434
spec.add_development_dependency 'rake', '~> 12.3'
3535
spec.add_development_dependency 'rspec', '~> 3.3'
36-
spec.add_development_dependency 'rubocop', '~> 0.49.0'
37-
spec.add_development_dependency 'vcr', '~> 4.0'
36+
spec.add_development_dependency 'rubocop', '~> 1.48.1'
37+
spec.add_development_dependency 'vcr', '~> 6.1'
3838
spec.add_development_dependency 'webmock', '~> 3.0'
39+
spec.metadata['rubygems_mfa_required'] = 'true'
3940
end

0 commit comments

Comments
 (0)