Skip to content

Commit

Permalink
Remove unused codes (#162)
Browse files Browse the repository at this point in the history
* Remove unused codes

Now that we support Ruby 2.7 and above, we can remove conditions with versions lower than that.

* Replace String#match with String#match?

`String#match?` is more suitable when only a boolean is needed, as it is faster than `String#match`. OpenapiParser now supports Ruby2.7 and above, and since `String#match?` has been available since Ruby2.4, it’s appropriate to make this switch.
  • Loading branch information
willnet authored Oct 21, 2024
1 parent 7ff08bb commit 92caf4c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 35 deletions.
7 changes: 2 additions & 5 deletions lib/openapi_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,11 @@ def parse_file(content, ext)
end

def parse_yaml(content)
# FIXME: when drop ruby 2.5, we should use permitted_classes
(Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.6.0")) ?
Psych.safe_load(content, [Date, Time]) :
Psych.safe_load(content, permitted_classes: [Date, Time])
Psych.safe_load(content, permitted_classes: [Date, Time])
end

def parse_json(content)
raise "json content is nil" unless content
raise "json content is nil" unless content
JSON.parse(content)
end

Expand Down
5 changes: 1 addition & 4 deletions lib/openapi_parser/schema_validator/string_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ def validate_max_min_length(value, schema)
def validate_email_format(value, schema)
return [value, nil] unless schema.format == 'email'

# match? method is good performance.
# So when we drop ruby 2.3 support we use match? method because this method add ruby 2.4
#return [value, nil] if value.match?(URI::MailTo::EMAIL_REGEXP)
return [value, nil] if value.match(URI::MailTo::EMAIL_REGEXP)
return [value, nil] if value.match?(URI::MailTo::EMAIL_REGEXP)

return [nil, OpenAPIParser::InvalidEmailFormat.new(value, schema.object_reference)]
end
Expand Down
6 changes: 1 addition & 5 deletions spec/openapi_parser/request_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@
it do
expect { subject }.to raise_error do |e|
expect(e).to be_kind_of(OpenAPIParser::ValidateError)
if Gem::Version.create(RUBY_VERSION) <= Gem::Version.create('2.4.0')
expect(e.message).to end_with("expected string, but received Fixnum: 1")
else
expect(e.message).to end_with("expected string, but received Integer: 1")
end
expect(e.message).to end_with("expected string, but received Integer: 1")
end
end
end
Expand Down
25 changes: 4 additions & 21 deletions spec/openapi_parser/schema_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -571,12 +571,7 @@
nested_array = params['nested_array']
first_data = nested_array[0]
expect(first_data['update_time'].class).to eq DateTime

if Gem::Version.create(RUBY_VERSION) <= Gem::Version.create('2.4.0')
expect(first_data['per_page'].class).to eq Fixnum # rubocop:disable Lint/UnifiedInteger
else
expect(first_data['per_page'].class).to eq Integer
end
expect(first_data['per_page'].class).to eq Integer
end
end

Expand Down Expand Up @@ -774,27 +769,15 @@
nested_array = params['nested_array']
first_data = nested_array[0]
expect(first_data['update_time'].class).to eq String
if Gem::Version.create(RUBY_VERSION) <= Gem::Version.create('2.4.0')
expect(first_data['per_page'].class).to eq Fixnum # rubocop:disable Lint/UnifiedInteger
else
expect(first_data['per_page'].class).to eq Integer
end
expect(first_data['per_page'].class).to eq Integer

second_data = nested_array[1]
expect(second_data['update_time'].class).to eq String
if Gem::Version.create(RUBY_VERSION) <= Gem::Version.create('2.4.0')
expect(first_data['per_page'].class).to eq Fixnum # rubocop:disable Lint/UnifiedInteger
else
expect(first_data['per_page'].class).to eq Integer
end
expect(first_data['per_page'].class).to eq Integer
expect(second_data['threshold'].class).to eq Float

third_data = nested_array[2]
if Gem::Version.create(RUBY_VERSION) <= Gem::Version.create('2.4.0')
expect(first_data['per_page'].class).to eq Fixnum # rubocop:disable Lint/UnifiedInteger
else
expect(first_data['per_page'].class).to eq Integer
end
expect(first_data['per_page'].class).to eq Integer
expect(third_data['threshold'].class).to eq Float

expect(first_data['nested_coercer_object']['update_time'].class).to eq String
Expand Down

0 comments on commit 92caf4c

Please sign in to comment.