Class: ZaiPayment::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/zai_payment/response.rb

Overview

Wrapper for API responses

Constant Summary collapse

RESPONSE_DATA_KEYS =
%w[
  webhooks users items fees transactions jobs
  batch_transactions batches bpay_accounts bank_accounts card_accounts
  wallet_accounts virtual_accounts disbursements pay_ids
].freeze
ERROR_STATUS_MAP =
{
  400 => Errors::BadRequestError,
  401 => Errors::UnauthorizedError,
  403 => Errors::ForbiddenError,
  404 => Errors::NotFoundError,
  422 => Errors::ValidationError,
  429 => Errors::RateLimitError
}.merge((500..599).to_h { |code| [code, Errors::ServerError] }).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(faraday_response) ⇒ Response



14
15
16
17
18
19
20
21
# File 'lib/zai_payment/response.rb', line 14

def initialize(faraday_response)
  @raw_response = faraday_response
  @status = faraday_response.status
  @body = faraday_response.body
  @headers = faraday_response.headers

  check_for_errors!
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/zai_payment/response.rb', line 6

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/zai_payment/response.rb', line 6

def headers
  @headers
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



6
7
8
# File 'lib/zai_payment/response.rb', line 6

def raw_response
  @raw_response
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/zai_payment/response.rb', line 6

def status
  @status
end

Instance Method Details

#client_error?Boolean

Check if the response was a client error (4xx status)



29
30
31
# File 'lib/zai_payment/response.rb', line 29

def client_error?
  (400..499).cover?(status)
end

#dataObject

Get the data from the response body



39
40
41
42
43
44
45
46
47
# File 'lib/zai_payment/response.rb', line 39

def data
  return body unless body.is_a?(Hash)

  RESPONSE_DATA_KEYS.each do |key|
    return body[key] if body[key]
  end

  body
end

#metaObject

Get pagination or metadata info



50
51
52
# File 'lib/zai_payment/response.rb', line 50

def meta
  body.is_a?(Hash) ? body['meta'] : nil
end

#server_error?Boolean

Check if the response was a server error (5xx status)



34
35
36
# File 'lib/zai_payment/response.rb', line 34

def server_error?
  (500..599).cover?(status)
end

#success?Boolean

Check if the response was successful (2xx status)



24
25
26
# File 'lib/zai_payment/response.rb', line 24

def success?
  (200..299).cover?(status)
end