Class: Quickbooks::Qbxml::ResponseSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/quickbooks/qbxml/response.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_or_hash) ⇒ ResponseSet

Returns a new instance of ResponseSet.



27
28
29
30
31
32
33
34
35
# File 'lib/quickbooks/qbxml/response.rb', line 27

def initialize(xml_or_hash)
  if(xml_or_hash.is_a?(Hash))
    self.append_from_hash(xml_or_hash)
  elsif(xml_or_hash.is_a?(String))
    self.append_from_xml(xml_or_hash)
  else
    raise ArgumentError, "Quickbooks::Qbxml::ResponseSet must be initialized with either a Hash or an xml-formatted String."
  end
end

Class Method Details

.from_hash(hsh) ⇒ Object



57
58
59
# File 'lib/quickbooks/qbxml/response.rb', line 57

def from_hash(hsh)
  new.append_from_hash(hsh)
end

.from_xml(xml) ⇒ Object



54
55
56
# File 'lib/quickbooks/qbxml/response.rb', line 54

def from_xml(xml)
  new.append_from_xml(xml)
end

Instance Method Details

#<<(qbxml_response) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/quickbooks/qbxml/response.rb', line 15

def <<(qbxml_response)
  if qbxml_response.is_a?(Quickbooks::Qbxml::Response)
    set << qbxml_response
  elsif qbxml_response.respond_to?(:each)
    qbxml_response.each do |response|
      self << response
    end
  else
    raise ArgumentError, "Cannot add object of type #{qbxml_response.class.name} to a Quickbooks::Qbxml::ResponseSet"
  end
end

#append_from_hash(hsh) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/quickbooks/qbxml/response.rb', line 40

def append_from_hash(hsh)
  to_append = []
  hsh = hsh['QBXML'] if hsh.has_key?('QBXML')
  hsh = hsh['QBXMLMsgsRs'] if hsh.has_key?('QBXMLMsgsRs')
  # responses will contain one or more keys.
  hsh.each_key do |name|
    # response_type is either a single response object, or an array of response objects. Force it into an array:
    responses = hsh[name].is_a?(Array) ? hsh[name] : [hsh[name]]
    responses.each { |response| to_append << Response.new(name => response) }
  end
  self << to_append
end

#append_from_xml(xml) ⇒ Object



37
38
39
# File 'lib/quickbooks/qbxml/response.rb', line 37

def append_from_xml(xml)
  self.append_from_hash(Hash.from_xml(xml))
end

#setObject



7
8
9
10
11
12
13
# File 'lib/quickbooks/qbxml/response.rb', line 7

def set
  unless @set.is_a?(Array)
    @set = []
    @set.extend(Quickbooks::Qbxml::ResponseSetArrayExt)
  end
  @set
end