class MyApiClient::ErrorHandling::Generator
Generates an error handler proc (or symbol)
Constants
- ARGUMENTS
Public Class Methods
new(**options)
click to toggle source
@param options [Hash]
Options for this generator
@option instance [MyApiClient::Base]
The API client class.
@option response [Sawyer::Response]
The target of verifying
@option status_code [String, Range, Integer, Regexp]
Verifies response HTTP status code and raises error if matched
@option json [Hash, Symbol]
Verifies response body as JSON and raises error if matched. If specified `:forbid_nil`, it forbid `nil` on response_body.
@option with [Symbol]
Calls specified method when error detected
@option raise [MyApiClient::Error]
Raises specified error when error detected. default: MyApiClient::Error
@option block [Proc]
Executes the block when error detected
@return [Proc, nil]
Returns the error handler as "Proc". If no error occurs, return `nil`.
# File lib/my_api_client/error_handling/generator.rb, line 28 def initialize(**options) options[:raise] ||= MyApiClient::Error verify_and_set_arguments(**options) end
Private Instance Methods
block_caller()
click to toggle source
# File lib/my_api_client/error_handling/generator.rb, line 54 def block_caller lambda { |params, logger| _block.call(params, logger) error_raiser.call(params, logger) } end
call()
click to toggle source
# File lib/my_api_client/error_handling/generator.rb, line 37 def call return unless match?(_status_code, _response.status) return unless match_all?(_json, _response.body) generate_error_handler end
error_raiser()
click to toggle source
# File lib/my_api_client/error_handling/generator.rb, line 68 def error_raiser ->(params, _) { raise _raise, params } end
generate_error_handler()
click to toggle source
# File lib/my_api_client/error_handling/generator.rb, line 44 def generate_error_handler if _block block_caller elsif _with method_caller else error_raiser end end
match?(operator, target)
click to toggle source
# File lib/my_api_client/error_handling/generator.rb, line 88 def match?(operator, target) case operator when nil true when String, Integer, TrueClass, FalseClass operator == target when Range operator.include?(target) when Regexp operator =~ target.to_s when Symbol target.respond_to?(operator) && target.public_send(operator) else raise "Unexpected operator type was given: #{operator.inspect}" end end
match_all?(json, response_body)
click to toggle source
# File lib/my_api_client/error_handling/generator.rb, line 105 def match_all?(json, response_body) return true if json.nil? return response_body.nil? if json == :forbid_nil return false if response_body.blank? json.all? do |path, operator| target = JsonPath.new(path.to_s).first(response_body) match?(operator, target) end rescue MultiJson::ParseError false end
method_caller()
click to toggle source
# File lib/my_api_client/error_handling/generator.rb, line 61 def method_caller lambda { |params, logger| _instance.send(_with, params, logger) error_raiser.call(params, logger) } end
verify_and_set_arguments(**options)
click to toggle source
Verify given options and raise error if they are incorrect. If not, set them to instance variables.
@param options [Hash] @raise [RuntimeError]
# File lib/my_api_client/error_handling/generator.rb, line 77 def verify_and_set_arguments(**options) options.each do |k, v| if ARGUMENTS.exclude? k raise "Specified an incorrect option: `#{k}`\n" \ "You can use options that: #{ARGUMENTS}" end instance_variable_set("@_#{k}", v) end end