Class: Qbxml::ResponseSet

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

Overview

module ResponseSetArrayExt end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_or_hash) ⇒ ResponseSet

Returns a new instance of ResponseSet.



30
31
32
33
34
35
36
37
38
# File 'lib/qbxml/response.rb', line 30

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, "Qbxml::ResponseSet must be initialized with either a Hash or an xml-formatted String."
  end
end

Class Method Details

.from_hash(hsh) ⇒ Object



62
63
64
# File 'lib/qbxml/response.rb', line 62

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

.from_xml(xml) ⇒ Object



59
60
61
# File 'lib/qbxml/response.rb', line 59

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

Instance Method Details

#<<(qbxml_response) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/qbxml/response.rb', line 18

def <<(qbxml_response)
  if qbxml_response.is_a?(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 Qbxml::ResponseSet"
  end
end

#append_from_hash(hsh) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/qbxml/response.rb', line 45

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



40
41
42
43
44
# File 'lib/qbxml/response.rb', line 40

def append_from_xml(xml)
  # puts "ResponseXML:"
  # puts xml
  self.append_from_hash(xml.formatted(:xml).to_hash)
end

#setObject



10
11
12
13
14
15
16
# File 'lib/qbxml/response.rb', line 10

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