class Fattura24::Api::Response

An instance of this class will be returned on every api call, wrapping the content of the response and providing helper methods to navigate it's content.

Attributes

http_response[R]

When needed, you can directly access the underlying Net::HTTP response by calling this method.

Public Class Methods

new(http_response) click to toggle source
# File lib/fattura24/api/response.rb, line 16
def initialize(http_response)
  @http_response = http_response
end

Public Instance Methods

code() click to toggle source

Returns the Integer value of the underlying http request. It does not mean the request performed it's intended purpose. Make sure you use to_s or to_h to explore the actual body of the response.

# File lib/fattura24/api/response.rb, line 32
def code
  http_response&.code.to_i
end
pdf?() click to toggle source

Returns true when the body of the request contains a pdf file.

# File lib/fattura24/api/response.rb, line 38
def pdf?
  http_response&.content_type&.underscore == 'application/pdf'
end
success?() click to toggle source

Returns true when the http response is 200, false otherwise.

# File lib/fattura24/api/response.rb, line 23
def success?
  code == 200
end
to_h() click to toggle source

Returns an hash representation of the xml body of the response. Raises NotSerializable in case of a binary (pdf file) content.

# File lib/fattura24/api/response.rb, line 46
def to_h
  if pdf?
    raise(
      Fattura24::NotSerializable,
      'Cannot create hash from binary file'
    )
  end

  Hash.from_xml(http_response&.body)
    &.deep_transform_keys do |key|
      key.to_s.underscore.to_sym
    end
end
to_s() click to toggle source

Returns the String body of this response. This can be both the original xml on most of the calls or the content of the pdf file when get_file is called.

# File lib/fattura24/api/response.rb, line 65
def to_s
  http_response&.body.to_s
end