lib/relax/response.rb in relax-0.0.2 vs lib/relax/response.rb in relax-0.0.3
- old
+ new
@@ -9,54 +9,69 @@
#
# A response is in essence an object used to facilitate XML parsing. It
# stores an XML document, and provides access to it through methods like
# #element and #attribute.
class Response
+ attr_accessor :raw
attr_accessor :xml
# New takes in the XML from the response. For the initial response, this
# will be the root element, but child elements may also be passed into
# Response objects.
+ #
+ # This will raise a MissingParameter error if a parameterd marked as
+ # required is not present in the XML response.
def initialize(xml)
+ @raw = xml
@xml = Hpricot.XML(xml.to_s)
- if parameter = self.class.instance_variable_get('@parameters')
- parameter.each do |parameter, options|
- element = options[:element] ? options[:element] : parameter
+ if parameters = self.class.instance_variable_get('@parameters')
+ parameters.each do |parameter, options|
+ begin
+ element = options[:element] || parameter
- if attribute = options[:attribute] and attribute == true
- node = attribute(root, element)
- elsif attribute
- node = attribute(element(element), attribute)
- elsif options[:collection]
- node = elements(element)
- else
- node = element(element)
- end
-
- if options[:collection]
- value = node.collect do |element|
- options[:collection].new(element)
+ if attribute = options[:attribute] and attribute == true
+ node = attribute(root, element)
+ elsif attribute
+ node = attribute(element(element), attribute)
+ elsif options[:collection]
+ node = elements(element)
+ else
+ node = element(element)
end
- else
- case type = options[:type]
- when Response
- value = type.new(node)
- when :float
- value = float_value(node)
+ if options[:collection]
+ value = node.collect do |element|
+ options[:collection].new(element)
+ end
+ else
+ case type = options[:type]
+ when Response
+ value = type.new(node)
- when :integer
- value = integer_value(node)
+ when :date
+ value = date_value(node)
- when :text
- else
- value = text_value(node)
+ when :time
+ value = time_value(node)
+
+ when :float
+ value = float_value(node)
+
+ when :integer
+ value = integer_value(node)
+
+ when :text
+ else
+ value = text_value(node)
+ end
end
- end
- instance_variable_set("@#{parameter}", value)
+ instance_variable_set("@#{parameter}", value)
+ rescue Hpricot::Error
+ raise MissingParameter if options[:required]
+ end
end
end
end
# Returns the root of the XML document.
@@ -108,10 +123,26 @@
# Gets a date value.
def date_value(value)
Date.parse(value(value))
end
+ # Gets a time value.
+ def time_value(value)
+ Time.parse(value(value))
+ end
+
class << self
+ # When a Response is extended, the superclasses parameters are copied
+ # into the new class. This behavior has the following side-effect: if
+ # parameters are added to the superclass after it has been extended,
+ # those new paramters won't be passed on to its children. This shouldn't
+ # be a problem in most cases.
+ def inherited(subclass)
+ @parameters.each do |name, options|
+ subclass.parameter(name, options)
+ end if @parameters
+ end
+
# Specifes a parameter that will be automatically parsed when the
# Response is instantiated.
#
# Options:
# - <tt>:attribute</tt>: An attribute name to use, or <tt>true</tt> to