class BtcPay::Client::Result
Status object to capture result from an HTTP request
Gives callers context of the result and allows them to implement successful strategies to handle success/failure
Attributes
body[R]
code[R]
headers[R]
raw[R]
status[R]
Public Class Methods
failed(response)
click to toggle source
# File lib/btcpay/client/result.rb, line 18 def self.failed(response) new(:failed, response) end
new(status, response)
click to toggle source
# File lib/btcpay/client/result.rb, line 24 def initialize(status, response) @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
success(response)
click to toggle source
# File lib/btcpay/client/result.rb, line 14 def self.success(response) new(:success, response) end
Public Instance Methods
failure?()
click to toggle source
# File lib/btcpay/client/result.rb, line 37 def failure? !success? end
success?()
click to toggle source
# File lib/btcpay/client/result.rb, line 33 def success? status == :success end
to_h()
click to toggle source
# File lib/btcpay/client/result.rb, line 41 def to_h { status: status, headers: headers, code: code, body: body } end
Also aliased as: to_hash
Private Instance Methods
method_missing(method, *args, &blk)
click to toggle source
Calls superclass method
# File lib/btcpay/client/result.rb, line 54 def method_missing(method, *args, &blk) to_h.send(method, *args, &blk) || super end
raw_parse(response)
click to toggle source
@param body [JSON] Raw JSON body
# File lib/btcpay/client/result.rb, line 63 def raw_parse(response) return if response.blank? body = MultiJson.load(response) return body.with_indifferent_access if body.respond_to?(:with_indifferent_access) raise NotImplemented.new('Unknown response type') unless body.is_a?(Array) key = success? ? :data : :errors { key => body.map(&:with_indifferent_access) } rescue MultiJson::ParseError response rescue StandardError => e raise ResponseBodyParseError.new(error: 'JSON parse error', message: e.message, body: response) end
respond_to_missing?(method, include_private = false)
click to toggle source
Calls superclass method
# File lib/btcpay/client/result.rb, line 58 def respond_to_missing?(method, include_private = false) to_h.respond_to?(method) || super end
rubify_body()
click to toggle source
# File lib/btcpay/client/result.rb, line 80 def rubify_body return if raw.blank? return unless raw.respond_to?(:deep_transform_keys) raw.deep_transform_keys { |key| key.to_s.underscore }.with_indifferent_access end