Class: Rapleaf::Response

Inherits:
OpenStruct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rapleaf/responses.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rapleaf/responses.rb', line 23

def self.parse(options = {})
  options = {
    :xml => "",
    :parse_options => { 'ForceArray' => ['item'], 'SuppressEmpty' => nil }
  }.merge(options)
  response = Response.new(XmlSimple.xml_in(options[:xml], options[:parse_options]))

  # set the xml attribute of the response object to contain the original XML that was
  # returned.  This allows anyone to bypass our parsing if desired and just
  # get right at the raw XML response.
  response.xml = options[:xml]
  return response
end

Instance Method Details

#[](member) ⇒ Object

Alternative method for getting members.



65
66
67
# File 'lib/rapleaf/responses.rb', line 65

def [](member)
  send(member)
end

#[]=(member, value) ⇒ Object

Alternative method for setting members.



70
71
72
# File 'lib/rapleaf/responses.rb', line 70

def []=(member, value)
  send("#{member}=", value)
end

#eachObject

Required by the Enumerable module. Iterate over each item in the members array and pass as a value the block passed to each using yield.



46
47
48
49
50
51
# File 'lib/rapleaf/responses.rb', line 46

def each
  members.each do |method|
    yield send(method)
  end
  self
end

#each_pairObject

Same as the each method, but with both key and value.

Sample Use: obj.each_pair { |k,v| puts “key: #k, value: #v” }



57
58
59
60
61
62
# File 'lib/rapleaf/responses.rb', line 57

def each_pair
  members.each do |method|
    yield method, send(method)
  end
  self
end

#membersObject

Every member of an OpenStruct object has getters and setters, the latter of which has a method ending in “=”. Find all of these methods, excluding those defined on parent classes.



40
41
42
# File 'lib/rapleaf/responses.rb', line 40

def members
  methods(false).sort.grep(/=/).map { |m| m[0...-1] }
end

#to_sObject

Override of to string method.



97
98
99
# File 'lib/rapleaf/responses.rb', line 97

def to_s
  return to_string
end

#to_string(short = false) ⇒ Object

Helper for converting to string which support a long and short version to avoid recursion problems with parents.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rapleaf/responses.rb', line 76

def to_string(short=false)
  s = "#<#{self.class}:0x#{(2 ** 32 + object_id).to_s(16).upcase}"
  if (short)
    s += " ..."
  else
    each_pair { |k,v|
      if (v == self.parent && v.kind_of?(Response))
        v = v.to_string(true)
      elsif (v.kind_of?(String))
        v = "\"#{v.gsub("\"", "\\\"")}\""
      elsif (v.kind_of?(NilClass))
        v = "nil"
      end
      s += " #{k}=#{v}"
    }
  end
  s += ">"
  return s
end