Class: QiwiPay::Api::Response

Inherits:
OpenStruct
  • Object
show all
Includes:
MessagesForCodes
Defined in:
lib/qiwi-pay/api/response.rb

Overview

QiwiPay API response

Constant Summary collapse

INTEGER_PARAMS =

Parameters of integer type

%w[
  txn_id
  txn_status
  txn_type
  error_code
  currency
]

Constants included from MessagesForCodes

MessagesForCodes::ERROR_MESSAGES, MessagesForCodes::TXN_STATUS_MESSAGES, MessagesForCodes::TXN_TYPE_MESSAGES

Instance Method Summary collapse

Methods included from MessagesForCodes

#error_message, #txn_status_message, #txn_type_message

Constructor Details

#initialize(response_code, response_body) ⇒ Response

Returns a new instance of Response.

Parameters:

  • response_code (Integer)

    HTTP response status code

  • response_body (String)

    Response body in JSON



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/qiwi-pay/api/response.rb', line 22

def initialize(response_code, response_body)
  begin
    params = JSON.parse(response_body)
    (INTEGER_PARAMS & params.keys).each do |p|
      params[p] = params[p] && params[p].to_i
    end
    super params
  rescue JSON::ParserError
    super error_code: -1
    define_singleton_method :error_message, ->{ response_body }
  end
  send(:http_code=, response_code)
end

Instance Method Details

#success?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/qiwi-pay/api/response.rb', line 36

def success?
  http_code == 200 && error_code == 0
end