Sha256: dd9d379c205dc456d2a71c242c5e486a34bca41f42bd371633370c3c7ba5e435

Contents?: true

Size: 1.98 KB

Versions: 43

Compression:

Stored size: 1.98 KB

Contents

module LHS::Problems
  class Errors < Base

    attr_reader :status_code, :message

    def initialize(response = nil, record = nil)
      @raw = response.body if response
      @record = record
      @codes = {}.with_indifferent_access
      @messages = messages_from_response(response).with_indifferent_access
      @message = message_from_response(response)
      @status_code = response.code if response
    rescue JSON::ParserError
      @messages = (messages || {}).with_indifferent_access
      @codes = (codes || {}).with_indifferent_access
      @message = 'parse error'
      add_error(@messages, 'body', 'parse error')
    end

    private

    def parse_messages(json)
      messages = {}
      fields_to_errors(json, messages) if json['fields']
      field_errors_to_errors(json, messages) if json['field_errors']
      fallback_errors(json, messages) if messages.empty? && !json['fields'] && !json['field_errors']
      messages
    end

    def fallback_errors(json, messages)
      if json.present?
        json.each do |key, value|
          add_error(messages, key, value)
        end
      else
        add_error(messages, 'unknown', 'error')
      end
    end

    def field_errors_to_errors(json, messages)
      json['field_errors'].each do |field_error|
        add_error(messages, field_error['path'].join('.').to_sym, field_error['code'])
      end
    end

    def fields_to_errors(json, messages)
      json['fields'].each do |field|
        field['details'].each do |detail|
          add_error(messages, field['name'].to_sym, detail['code'])
        end
      end
    end

    def messages_from_response(response = nil)
      return {} if !response || !response.body.is_a?(String) || response.body.length.zero?
      json = JSON.parse(response.body)
      parse_messages(json)
    end

    def message_from_response(response = nil)
      return if response.blank?
      raise JSON::ParserError if response.body.blank?
      json = JSON.parse(response.body)
      json['message']
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
lhs-16.1.5 lib/lhs/problems/errors.rb
lhs-16.1.4 lib/lhs/problems/errors.rb
lhs-16.1.3 lib/lhs/problems/errors.rb
lhs-16.1.2 lib/lhs/problems/errors.rb
lhs-16.1.1 lib/lhs/problems/errors.rb
lhs-16.1.0 lib/lhs/problems/errors.rb
lhs-16.0.1 lib/lhs/problems/errors.rb
lhs-16.0.0 lib/lhs/problems/errors.rb
lhs-15.7.0 lib/lhs/problems/errors.rb
lhs-15.6.1 lib/lhs/problems/errors.rb
lhs-15.6.0 lib/lhs/problems/errors.rb
lhs-15.5.1 lib/lhs/problems/errors.rb
lhs-15.5.0 lib/lhs/problems/errors.rb
lhs-15.4.1 lib/lhs/problems/errors.rb
lhs-15.4.0 lib/lhs/problems/errors.rb
lhs-15.4.0.pre.hasone.1 lib/lhs/problems/errors.rb
lhs-15.3.3 lib/lhs/problems/errors.rb
lhs-15.3.3.pre.fixoptions.1 lib/lhs/problems/errors.rb
lhs-15.3.2 lib/lhs/problems/errors.rb
lhs-15.3.1 lib/lhs/problems/errors.rb