class Xeroizer::Response

Attributes

date_time[RW]
errors[RW]
id[RW]
provider[RW]
request_params[RW]
request_xml[RW]
response_items[RW]
response_xml[RW]
status[RW]

Public Class Methods

new() click to toggle source
# File lib/xeroizer/response.rb, line 56
def initialize
  @response_items = []
end
parse(raw_response, request = {}, options = {}) { |response, children, children.name| ... } click to toggle source

Parse the response retreived during any request.

# File lib/xeroizer/response.rb, line 23
def parse(raw_response, request = {}, options = {}, &block)
  response = Xeroizer::Response.new
  response.response_xml = raw_response

  doc = Nokogiri::XML(raw_response) { | cfg | cfg.noblanks }

  # check for responses we don't understand
  raise Xeroizer::UnparseableResponse.new(doc.root.name) unless doc.root.name == 'Response'

  doc.root.elements.each do | element |
              
    # Text element
    if element.children && element.children.size == 1 && element.children.first.text?
      case element.name
        when 'Id'           then response.id = element.text
        when 'Status'       then response.status = element.text
        when 'ProviderName' then response.provider = element.text
        when 'DateTimeUTC'  then response.date_time = Time.parse(element.text)
      end
    
    # Records in response
    elsif element.children && element.children.size > 0
      yield(response, element.children, element.children.first.name)
    end
  end

  response
end

Public Instance Methods

error() click to toggle source
# File lib/xeroizer/response.rb, line 64
def error
  errors.blank? ? nil : errors[0]
end
success?() click to toggle source
# File lib/xeroizer/response.rb, line 60
def success?
  status == 'OK'
end