Skip to content

Commit

Permalink
rubocop autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Feb 19, 2025
1 parent b059a66 commit 0d35b76
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 71 deletions.
5 changes: 1 addition & 4 deletions lib/apipie/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,7 @@ def json_schema_for_method_response(version, controller_name, method_name, retur
.json_schema_for_method_response(method, return_code, allow_nulls)
end

def json_schema_for_self_describing_class(cls, allow_nulls)
Apipie::SwaggerGenerator
.json_schema_for_self_describing_class(cls, allow_nulls)
end
delegate :json_schema_for_self_describing_class, to: :'Apipie::SwaggerGenerator'

def to_swagger_json(version, resource_id, method_name, language, clear_warnings = false)
return unless valid_search_args?(version, resource_id, method_name)
Expand Down
84 changes: 41 additions & 43 deletions lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,63 +230,61 @@ def tags(*args)
def _apipie_define_validators(description)

# [re]define method only if validation is turned on
if description && (Apipie.configuration.validate == true ||
Apipie.configuration.validate == :implicitly ||
Apipie.configuration.validate == :explicitly)

_apipie_save_method_params(description.method, description.params)

unless instance_methods.include?(:apipie_validations)
define_method(:apipie_validations) do
method_params = self.class._apipie_get_method_params(action_name)

if Apipie.configuration.validate_presence?
Validator::BaseValidator.raise_if_missing_params do |missing|
method_params.each_value do |param|
# check if required parameters are present
missing << param if param.required && !params.key?(param.name)
end
end
end
return unless description && [true, :implicitly, :explicitly].include?(Apipie.configuration.validate)

_apipie_save_method_params(description.method, description.params)

if Apipie.configuration.validate_value?
unless instance_methods.include?(:apipie_validations)
define_method(:apipie_validations) do
method_params = self.class._apipie_get_method_params(action_name)

if Apipie.configuration.validate_presence?
Validator::BaseValidator.raise_if_missing_params do |missing|
method_params.each_value do |param|
# params validations
param.validate(params[:"#{param.name}"]) if params.key?(param.name)
# check if required parameters are present
missing << param if param.required && !params.key?(param.name)
end
end
end

# Only allow params passed in that are defined keys in the api
# Auto skip the default params (format, controller, action)
if Apipie.configuration.validate_key?
params.reject{|k,_| %w[format controller action].include?(k.to_s) }.each_pair do |param, _|
# params allowed
if method_params.none? {|_,p| p.name.to_s == param.to_s}
self.class._apipie_handle_validate_key_error params, param
end
end
if Apipie.configuration.validate_value?
method_params.each_value do |param|
# params validations
param.validate(params[:"#{param.name}"]) if params.key?(param.name)
end
end

return unless Apipie.configuration.process_value?
@api_params ||= {}
method_params.each_value do |param|
# params processing
@api_params[param.as] = param.process_value(params[:"#{param.name}"]) if params.key?(param.name)
# Only allow params passed in that are defined keys in the api
# Auto skip the default params (format, controller, action)
if Apipie.configuration.validate_key?
params.reject{|k,_| %w[format controller action].include?(k.to_s) }.each_pair do |param, _|
# params allowed
if method_params.none? {|_,p| p.name.to_s == param.to_s}
self.class._apipie_handle_validate_key_error params, param
end
end
end

return unless Apipie.configuration.process_value?
@api_params ||= {}
method_params.each_value do |param|
# params processing
@api_params[param.as] = param.process_value(params[:"#{param.name}"]) if params.key?(param.name)
end
end
end

if Apipie.configuration.validate == :implicitly || Apipie.configuration.validate == true
old_method = instance_method(description.method)
return unless [:implicitly, true].include?(Apipie.configuration.validate)
old_method = instance_method(description.method)

define_method(description.method) do |*args|
apipie_validations
define_method(description.method) do |*args|
apipie_validations

# run the original method code
old_method.bind(self).call(*args)
end
end
# run the original method code
old_method.bind(self).call(*args)
end


end

def _apipie_handle_validate_key_error params, param
Expand Down
4 changes: 1 addition & 3 deletions lib/apipie/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def finish
end
end

def logger
Rails.logger
end
delegate :logger, to: :Rails

def call_recorder
Thread.current[:apipie_call_recorder] ||= Recorder.new
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def initialize(method_description, name, validator, desc_or_options = nil, optio
action_awareness

if validator
if (validator != Hash) && (validator.is_a? Hash) && (validator[:array_of])
if (validator != Hash) && (validator.is_a? Hash) && validator[:array_of]
@is_array = true
validator = validator[:array_of]
raise "an ':array_of =>' validator is allowed exclusively on response-only fields" unless @response_only
Expand Down
8 changes: 2 additions & 6 deletions lib/apipie/response_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,13 @@ def is_array?
@is_array_of != false
end

def typename
@response_object.typename
end
delegate :typename, to: :@response_object

def param_description
nil
end

def params_ordered
@response_object.params_ordered
end
delegate :params_ordered, to: :@response_object

def additional_properties
!!@response_object.additional_properties
Expand Down
4 changes: 1 addition & 3 deletions lib/apipie/static_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def match?(path)
end
end

def call(env)
@file_server.call(env)
end
delegate :call, to: :@file_server

def ext
@ext ||= begin
Expand Down
12 changes: 3 additions & 9 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,7 @@ def description

class DecimalValidator < BaseValidator

def validate(value)
self.class.validate(value)
end
delegate :validate, to: :class

def self.build(param_description, argument, options, block)
if argument == :decimal
Expand All @@ -456,9 +454,7 @@ def self.validate(value)

class NumberValidator < BaseValidator

def validate(value)
self.class.validate(value)
end
delegate :validate, to: :class

def self.build(param_description, argument, options, block)
if argument == :number
Expand Down Expand Up @@ -549,9 +545,7 @@ def description
"Must be an Array of nested elements"
end

def params_ordered
@validator.params_ordered
end
delegate :params_ordered, to: :@validator
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/config/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
Bundler.setup
end

require "logger" # Fix concurrent-ruby removing logger dependency which Rails itself does not have
require 'logger' # Fix concurrent-ruby removing logger dependency which Rails itself does not have

$:.unshift File.expand_path('../../../lib', __dir__)
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def fail(msg)

match do |unresolved|
actual = resolve_refs(unresolved)
return fail("expected schema to have type 'object' (got '#{actual[:type]}')") if (actual[:type]) != 'object'
return fail("expected schema to have type 'object' (got '#{actual[:type]}')") if actual[:type] != 'object'
return fail("expected schema to include param named '#{name}' (got #{actual[:properties].keys})") if (prop = actual[:properties][name]).nil?
return fail("expected param '#{name}' to have type '#{type}' (got '#{prop[:type]}')") if prop[:type] != type
return fail("expected param '#{name}' to have description '#{opts[:description]}' (got '#{prop[:description]}')") if opts[:description] && prop[:description] != opts[:description]
Expand Down

0 comments on commit 0d35b76

Please sign in to comment.