lib/recurly/resource.rb in recurly-2.7.9 vs lib/recurly/resource.rb in recurly-2.8.0.rc1

- old
+ new

@@ -335,12 +335,13 @@ if uuid.nil? # Should we raise an ArgumentError, instead? raise NotFound, "can't find a record with nil identifier" end + uri = uuid =~ /^http/ ? uuid : member_path(uuid) begin - from_response API.get(member_path(uuid), {}, options) + from_response API.get(uri, {}, options) rescue API::NotFound => e raise NotFound, e.description end end @@ -367,21 +368,17 @@ # response attribute in the process. # # @return [Resource] # @param response [Net::HTTPResponse] def from_response(response) - content_type = response['Content-Type'] - - case content_type + case response['Content-Type'] when %r{application/pdf} response.body - when %r{application/xml} + else # when %r{application/xml} record = from_xml response.body record.instance_eval { @etag, @response = response['ETag'], response } record - else - raise Recurly::Error, "Content-Type \"#{content_type}\" is not accepted" end end # Instantiates a record from an XML blob: either a String or XML element. # @@ -391,15 +388,14 @@ # @return [Resource] # @param xml [String, REXML::Element, Nokogiri::XML::Node] # @see from_response def from_xml(xml) xml = XML.new xml + if self != Resource || xml.name == member_name record = new - elsif Recurly.const_defined?( - class_name = Helper.classify(xml.name), false - ) + elsif Recurly.const_defined?(class_name = Helper.classify(xml.name), false) klass = Recurly.const_get class_name, false record = klass.send :new elsif root = xml.root and root.elements.empty? return XML.cast root else @@ -445,17 +441,28 @@ :method => :get, :href => href.value } end else - val = XML.cast(el) - if 'address' == el.name && val.kind_of?(Hash) - address = Address.new val - address.instance_variable_set(:@changed_attributes, {}) - record[el.name] = address + # TODO name tax_type conflicts with the TaxType + # class so if we get to this point was can assume + # it's the string. Will need to refactor this + if el.name == 'tax_type' + record[el.name] = el.text else - record[el.name] = val + val = XML.cast(el) + + # TODO we have to clear changed attributes after + # parsing here or else it always serializes. Need + # a better way of handling changed attributes + if el.name == 'address' && val.kind_of?(Hash) + address = Address.new val + address.changed_attributes.clear + record[el.name] = address + else + record[el.name] = val + end end end end record.persist! if record.respond_to? :persist! @@ -620,16 +627,13 @@ def reload(response = nil) if response return if response.body.to_s.length.zero? fresh = self.class.from_response response else - options = {:etag => (etag unless changed?)} - fresh = if @href - self.class.from_response API.get(@href, {}, options) - else - self.class.find(to_param, options) - end + fresh = self.class.find( + @href || to_param, :etag => (etag unless changed?) + ) end fresh and copy_from fresh persist! true self rescue API::NotModified @@ -744,13 +748,9 @@ # @param attributes [Hash] A hash of attributes. def attributes=(attributes = {}) attributes.each_pair { |k, v| respond_to?(name = "#{k}=") and send(name, v) or self[k] = v } - end - - def as_json(options = nil) - attributes.reject { |k, v| v.is_a?(Recurly::Resource::Pager) } end # @return [Hash] The raw hash of record href links. def links @links ||= {}