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

- (Response) initialize



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

def initialize
  @response_items = []
end

Instance Attribute Details

- (Object) date_time

Returns the value of attribute date_time



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

def date_time
  @date_time
end

- (Object) errors

Returns the value of attribute errors



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

def errors
  @errors
end

- (Object) id

Returns the value of attribute id



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

def id
  @id
end

- (Object) provider

Returns the value of attribute provider



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

def provider
  @provider
end

- (Object) request_params

Returns the value of attribute request_params



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

def request_params
  @request_params
end

- (Object) request_xml

Returns the value of attribute request_xml



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

def request_xml
  @request_xml
end

- (Object) response_items

Returns the value of attribute response_items



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

def response_items
  @response_items
end

- (Object) response_xml

Returns the value of attribute response_xml



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

def response_xml
  @response_xml
end

- (Object) status

Returns the value of attribute status



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

def status
  @status
end

Class Method Details

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

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

- (Object) error



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

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

- (Boolean) success?



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

def success?
  status == 'OK'
end