class ResponseHandler

Attributes

response[R]
single_from_list[R]

Public Class Methods

new(response, single_from_list: false) click to toggle source
# File lib/response_handler.rb, line 3
def initialize(response, single_from_list: false)
  @response = response
  @single_from_list = single_from_list
end

Public Instance Methods

parse!() click to toggle source
# File lib/response_handler.rb, line 10
def parse!
  success? ? handle_success : handle_error
end

Private Instance Methods

handle_error() click to toggle source
# File lib/response_handler.rb, line 24
def handle_error
  payload.success = false

  payload
end
handle_success() click to toggle source
# File lib/response_handler.rb, line 16
def handle_success
  @payload = payload.data.first if single_from_list

  payload.success = true

  payload
end
payload() click to toggle source
# File lib/response_handler.rb, line 30
def payload
  @payload ||= JSON.parse(response.body, object_class: OpenStruct)
end
success?() click to toggle source
# File lib/response_handler.rb, line 34
def success?
  single_from_list ? payload.data&.first.present? : (200..204).include?(response.status)
end