Class: Xeroizer::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/xeroizer/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse



56
57
58
# File 'lib/xeroizer/response.rb', line 56

def initialize
  @response_items = []
end

Instance Attribute Details

#date_timeObject

Returns the value of attribute date_time



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def date_time
  @date_time
end

#errorsObject

Returns the value of attribute errors



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def errors
  @errors
end

#idObject

Returns the value of attribute id



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def id
  @id
end

#providerObject

Returns the value of attribute provider



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def provider
  @provider
end

#request_paramsObject

Returns the value of attribute request_params



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def request_params
  @request_params
end

#request_xmlObject

Returns the value of attribute request_xml



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def request_xml
  @request_xml
end

#response_itemsObject

Returns the value of attribute response_items



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def response_items
  @response_items
end

#response_xmlObject

Returns the value of attribute response_xml



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def response_xml
  @response_xml
end

#statusObject

Returns the value of attribute status



18
19
20
# File 'lib/xeroizer/response.rb', line 18

def status
  @status
end

Class Method Details

.parse(raw_response, request = {}, options = {}, &block) ⇒ Object

Parse the response retreived during any request.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

Instance Method Details

#errorObject



64
65
66
# File 'lib/xeroizer/response.rb', line 64

def error
  errors.blank? ? nil : errors[0]
end

#success?Boolean



60
61
62
# File 'lib/xeroizer/response.rb', line 60

def success?
  status == 'OK'
end