class EasybillRestClient::Response

Attributes

response[R]

Public Class Methods

new(response) click to toggle source
# File lib/easybill_rest_client/response.rb, line 5
def initialize(response)
  @response = response
end

Public Instance Methods

body() click to toggle source
# File lib/easybill_rest_client/response.rb, line 9
def body
  body = extract_response_body(response)
  unless response.is_a?(Net::HTTPSuccess)
    raise ApiError, body.inspect
  end
  body && !body.empty? ? body : nil
end

Private Instance Methods

extract_response_body(response) click to toggle source
# File lib/easybill_rest_client/response.rb, line 21
def extract_response_body(response)
  return unless response.class.body_permitted?
  case response.content_type
  when 'application/json'
    JSON.parse(response.body, symbolize_names: true)
  when 'application/pdf'
    /\Wfilename="(?<filename>.*?)"/ =~ response.fetch('content-disposition')
    Pdf.new(filename: filename, content: response.body)
  else
    response.body
  end
end