update result parsing for nil, array, hash responses.

This commit is contained in:
ZippyDev 2020-07-28 20:25:27 -06:00
parent 4303a2797f
commit 22da8eff6b
2 changed files with 35 additions and 7 deletions

View File

@ -52,6 +52,20 @@ module BtcPay
request(uri, method: :post, payload: data, options: options, headers: headers)
end
# DELETE request
#
# @param uri [String]
# @param options [Hash]
# @param headers [Hash]
# @return [Result]
def delete(uri, options: {}, headers: {})
request(uri, method: :delete, options: options, headers: headers)
end
def api_keys
@api_keys ||= Api::ApiKeys.new(client: self)
end
def users
@users ||= Api::Users.new(client: self)
end
@ -83,7 +97,7 @@ module BtcPay
params = {
method: method,
url: url.to_s,
payload: payload,
payload: payload.presence,
headers: default_headers.merge(headers),
open_timeout: open_timeout,
timeout: timeout
@ -110,7 +124,7 @@ module BtcPay
def handle_error(error)
logger.error(error: 'Request Exception', code: error.response.code, message: error.message)
Result.failed(response)
Result.failed(error.response)
end
# @return [Integer]

View File

@ -22,12 +22,12 @@ module BtcPay
attr_reader :body, :code, :headers, :raw, :status
def initialize(status, response)
@raw = raw_parse(response.body)
@body = rubify_body
@code = response.code
@headers = response.headers # e.g. "Content-Type" will become :content_type.
@status = status
@raw = raw_parse(response.body)
@body = rubify_body
end
def success?
@ -60,13 +60,27 @@ module BtcPay
end
# @param body [JSON] Raw JSON body
def raw_parse(body)
MultiJson.load(body).with_indifferent_access
def raw_parse(response)
return if response.blank?
body = MultiJson.load(response)
case body
when Array
key = success? ? :data : :errors
{
key => body.map(&:with_indifferent_access)
}
else
body.with_indifferent_access
end
rescue StandardError => e
raise ResponseBodyParseError.new(error: 'JSON parse error', message: e.message, body: body)
end
def rubify_body
return if raw.blank?
raw.deep_transform_keys { |key| key.to_s.underscore }.with_indifferent_access
end