diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f0e0752..63b4ed9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,15 +1,15 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-04-12 14:06:44 UTC using RuboCop version 1.62.1. +# on 2024-04-20 21:25:22 UTC using RuboCop version 1.62.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 13 +# Offense count: 14 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: - Max: 47 + Max: 46 # Offense count: 1 # Configuration parameters: CountComments, CountAsOne. @@ -19,7 +19,7 @@ Metrics/ClassLength: # Offense count: 9 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: - Max: 12 + Max: 13 # Offense count: 22 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. @@ -29,7 +29,7 @@ Metrics/MethodLength: # Offense count: 5 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/PerceivedComplexity: - Max: 12 + Max: 13 # Offense count: 1 # Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. diff --git a/lib/rspec/openapi/extractors.rb b/lib/rspec/openapi/extractors.rb index c8322ae..52c686a 100644 --- a/lib/rspec/openapi/extractors.rb +++ b/lib/rspec/openapi/extractors.rb @@ -2,4 +2,11 @@ # Create namespace module RSpec::OpenAPI::Extractors + # @param [String, Symbol] path_parameter + # @return [Integer, nil] + def self.number_or_nil(path_parameter) + Integer(path_parameter.to_s || '') + rescue ArgumentError + nil + end end diff --git a/lib/rspec/openapi/extractors/hanami.rb b/lib/rspec/openapi/extractors/hanami.rb index 6cb0fc7..26844b2 100644 --- a/lib/rspec/openapi/extractors/hanami.rb +++ b/lib/rspec/openapi/extractors/hanami.rb @@ -66,7 +66,7 @@ def request_attributes(request, example) deprecated = metadata[:deprecated] path = request.path - raw_path_params = route.params.filter { |_key, value| number_or_nil(value) } + raw_path_params = route.params.filter { |_key, value| RSpec::OpenAPI::Extractors.number_or_nil(value) } result = InspectorAnalyzer.call(request.method, add_id(path, route)) @@ -92,7 +92,7 @@ def add_id(path, route) return path if route.params.empty? route.params.each_pair do |key, value| - next unless number_or_nil(value) + next unless RSpec::OpenAPI::Extractors.number_or_nil(value) path = path.sub("/#{value}", "/:#{key}") end @@ -104,17 +104,11 @@ def add_openapi_id(path, route) return path if route.params.empty? route.params.each_pair do |key, value| - next unless number_or_nil(value) + next unless RSpec::OpenAPI::Extractors.number_or_nil(value) path = path.sub("/#{value}", "/{#{key}}") end path end - - def number_or_nil(string) - Integer(string || '') - rescue ArgumentError - nil - end end diff --git a/lib/rspec/openapi/extractors/rails.rb b/lib/rspec/openapi/extractors/rails.rb index 5346aa0..6165505 100644 --- a/lib/rspec/openapi/extractors/rails.rb +++ b/lib/rspec/openapi/extractors/rails.rb @@ -65,17 +65,11 @@ def find_rails_route(request, app: Rails.application, path_prefix: '') def add_id(path, parameters) parameters.each_pair do |key, value| - next unless number_or_nil(value) + next unless RSpec::OpenAPI::Extractors.number_or_nil(value) path = path.sub("/#{value}", "/:#{key}") end path end - - def number_or_nil(string) - Integer(string || '') - rescue ArgumentError - nil - end end diff --git a/spec/apps/rails/config/routes.rb b/spec/apps/rails/config/routes.rb index 1c6ff97..a448bbc 100644 --- a/spec/apps/rails/config/routes.rb +++ b/spec/apps/rails/config/routes.rb @@ -4,7 +4,9 @@ get '/my_engine/test' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['ANOTHER TEST']] } - get '/pages' => 'pages#get' + defaults format: :html do + get '/pages' => 'pages#get' + end defaults format: 'json' do resources :tables, only: [:index, :show, :create, :update, :destroy]