Sha256: 2f89edd59841fa2f04c8a3833c6118af6b6869cabf68e74938173409d9e7ef30

Contents?: true

Size: 1.79 KB

Versions: 7

Compression:

Stored size: 1.79 KB

Contents

module RemoteResource
  class Response

    attr_reader :original_response, :original_request
    private :original_response, :original_request

    def initialize(response, connection_options = {})
      @original_response  = response
      @original_request   = response.request
      @connection_options = connection_options
    end

    def success?
      original_response.success?
    end

    def unprocessable_entity?
      response_code == 422
    end

    def response_body
      original_response.body
    end

    def response_code
      original_response.response_code
    end

    def sanitized_response_body
      return empty_hash if response_body.blank?
      return empty_hash if parsed_response_body.blank?

      unpacked_parsed_response_body
    end

    def sanitized_response_meta
      return empty_hash if response_body.blank?
      return empty_hash if parsed_response_body.blank?

      return parsed_response_body['meta'] if parsed_response_body.try :has_key?, 'meta'

      empty_hash
    end

    def error_messages_response_body
      return empty_hash if response_body.blank?
      return empty_hash if parsed_response_body.blank?

      return parsed_response_body["errors"]          if parsed_response_body.try :has_key?, "errors"
      return unpacked_parsed_response_body["errors"] if unpacked_parsed_response_body.try :has_key?, "errors"

      empty_hash
    end

    def parsed_response_body
      @parsed_response_body ||= JSON.parse response_body
    rescue JSON::ParserError
      nil
    end

    private

    def empty_hash
      {}
    end

    def unpacked_parsed_response_body
      root_element = @connection_options[:root_element].presence

      if root_element
        parsed_response_body[root_element.to_s]
      else
        parsed_response_body
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ddy_remote_resource-0.4.11 lib/remote_resource/response.rb
ddy_remote_resource-0.4.10 lib/remote_resource/response.rb
ddy_remote_resource-0.4.9 lib/remote_resource/response.rb
ddy_remote_resource-0.4.8 lib/remote_resource/response.rb
ddy_remote_resource-0.4.7 lib/remote_resource/response.rb
ddy_remote_resource-0.4.6 lib/remote_resource/response.rb
ddy_remote_resource-0.4.5 lib/remote_resource/response.rb