Sha256: 31dad66770ed5283541b1cffe8a4f9bddc9d488b24f897606a899da73f044d1d

Contents?: true

Size: 1015 Bytes

Versions: 3

Compression:

Stored size: 1015 Bytes

Contents

# frozen_string_literal: true

require 'housecanary/error'
require 'housecanary/utils'

module Housecanary
  class ResponseParser # :nodoc:
    class << self
      def perform(response)
        response_body = parse_body(response)
        code = parse_code(response.code, response_body)
        api_error_filter(code, response_body)
      end

      private

      def parse_body(response)
        response.body.empty? ? '' : utils.deep_symbolize_keys(response.parse(:json))
      end

      def parse_code(code, body = {})
        return code unless code.to_s.to_i == 200
        api_code = body.dig(:api_code) if body.is_a?(Hash)
        return api_code unless api_code.to_s.to_i.zero?
        code
      end

      def utils
        Housecanary::Utils
      end

      def error(code, body)
        Housecanary::Error::ERRORS_MAP[code]&.from_response(body)
      end

      def api_error_filter(code, body)
        error = error(code, body)
        raise(error) if error
        body
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
housecanary-ruby-0.2.2 lib/housecanary/response_parser.rb
housecanary-ruby-0.2.1 lib/housecanary/response_parser.rb
housecanary-ruby-0.2.0 lib/housecanary/response_parser.rb