Sha256: 48919c40a6982213e64f13fc5fa7d553ea928d25e17569ea057f98a37e8c72c6

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module PayuAPI
  class Response
    extend Dry::Initializer::Mixin

    option :http_response

    SUCCESS_HTTP_STATUSES = [200].freeze
    SUCCESS_STATUSES = ['SUCCESS'].freeze

    def success?
      http_success? && status_success?
    end

    def error?
      !success?
    end

    def error_code
      body[:error] || status_code
    end

    def error_message
      body[:error_description] || status_description
    end

    private

    def http_success?
      SUCCESS_HTTP_STATUSES.include?(http_status)
    end

    def http_status
      http_response.status
    end

    def status_success?
      SUCCESS_STATUSES.include?(status_code)
    end

    def status_code
      status[:statusCode]
    end

    def status_description
      status[:statusDesc]
    end

    def status
      body[:status] || {}
    end

    def body
      return unless http_response.body
      @body ||=
        begin
          JSON.parse(http_response.body, symbolize_names: true)
        rescue => e
          raise InvalidResponseError, e.message
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
payu_api-0.1.0 lib/payu_api/response.rb