class RDStation::Error::Formatter

Public Class Methods

new(error_response, headers = {}) click to toggle source
# File lib/rdstation/error/formatter.rb, line 8
def initialize(error_response, headers = {})
  @error_response = error_response
  @headers = headers
end

Public Instance Methods

error_format() click to toggle source
# File lib/rdstation/error/formatter.rb, line 74
def error_format
  @error_format ||= RDStation::Error::Format.new(errors)
end
errors() click to toggle source
# File lib/rdstation/error/formatter.rb, line 78
def errors
  @errors ||= @error_response.fetch('errors', @error_response)
end
from_flat_hash() click to toggle source
# File lib/rdstation/error/formatter.rb, line 45
def from_flat_hash
  [errors]
end
from_hash_of_hashes() click to toggle source
# File lib/rdstation/error/formatter.rb, line 64
def from_hash_of_hashes
  array_of_errors = []
  errors.each do |attribute_name, errors|
    result = build_error_from_multilingual_hash(attribute_name, errors)
    array_of_errors.push(*result)
  end

  array_of_errors
end
from_hash_of_multiple_types() click to toggle source
# File lib/rdstation/error/formatter.rb, line 49
def from_hash_of_multiple_types
  array_of_errors = []
  errors.each do |attribute_name, errors|
    if errors.is_a? Array
      result = build_error_from_array(attribute_name, errors)
    end
    if errors.is_a? Hash
      result = build_error_from_multilingual_hash(attribute_name, errors)
    end
    array_of_errors.push(*result)
  end

  array_of_errors
end
from_single_hash() click to toggle source
# File lib/rdstation/error/formatter.rb, line 32
def from_single_hash
  error_hash = @error_response.dup
  error_message = error_hash.delete('error') || error_hash.delete('message')

  [
    {
      'error_type' => 'TOO_MANY_REQUESTS',
      'error_message' => error_message,
      'details' => error_hash.key?('max') ? error_hash : error_details
    }
  ]
end
to_array() click to toggle source
# File lib/rdstation/error/formatter.rb, line 13
def to_array
  return @error_response unless @error_response.is_a?(Hash)

  case error_format.format
  when RDStation::Error::Format::SINGLE_HASH
    return from_single_hash
  when RDStation::Error::Format::FLAT_HASH
    return from_flat_hash
  when RDStation::Error::Format::HASH_OF_ARRAYS
    return from_hash_of_arrays
  when RDStation::Error::Format::HASH_OF_HASHES
    return from_hash_of_hashes
  when RDStation::Error::Format::HASH_OF_MULTIPLE_TYPES
    return from_hash_of_multiple_types
  end

  errors
end

Private Instance Methods

build_error_from_array(attribute_name, attribute_errors) click to toggle source
# File lib/rdstation/error/formatter.rb, line 84
def build_error_from_array(attribute_name, attribute_errors)
  path = { 'path' => "body.#{attribute_name}" }
  attribute_errors.map { |error| error.merge(path) }
end
build_error_from_multilingual_hash(attribute_name, errors_by_language) click to toggle source
# File lib/rdstation/error/formatter.rb, line 89
def build_error_from_multilingual_hash(attribute_name, errors_by_language)
  array_of_errors = []
  errors_by_language.each do |language, errors|
    result = build_error_from_array("#{attribute_name}.#{language}", errors)
    array_of_errors.push(*result)
  end
  array_of_errors
end
error_details() click to toggle source
# File lib/rdstation/error/formatter.rb, line 107
def error_details
  {
    max: @headers.fetch('ratelimit-limit-quotas', 0),
    usage: @headers.fetch('ratelimit-limit-quotas', 0),
    remaining_time: @headers.fetch('retry-after-quotas', 0)
  }
end
from_hash_of_arrays() click to toggle source
# File lib/rdstation/error/formatter.rb, line 98
def from_hash_of_arrays
  errors.each_with_object([]) do |errors, array_of_errors|
    attribute_name = errors.first
    attribute_errors = errors.last
    errors = build_error_from_array(attribute_name, attribute_errors)
    array_of_errors.push(*errors)
  end
end