loyalflow
loyalflow

Reputation: 14919

XML to Ruby object from a Restful resource, performance considerations

I'm looking at the below URL to see how a popular ruby gem maps a Restful endpoint that returns XML to ruby objects:

https://github.com/tapajos/highrise/blob/master/lib/highrise/base.rb

I see they are using ActiveResource::Base which magically does this behind the scenes.

So you get some sort of xml back from the URI like:

<person>
  <id type="integer">1</id>
  <first-name>John</first-name>
  <last-name>Doe</last-name>
  <title>Stand-in</title>
  <background>A popular guy for random data</background>
  <linkedin_url>http://us.linkedin.com/in/john-doe</linkedin_url>
  <company-id type="integer">5</company-id>
  <company-name>Doe Inc.</company-name>
  <created-at type="datetime">2007-02-27T03:11:52Z</created-at>
  <updated-at type="datetime">2007-03-10T15:11:52Z</updated-at>
  <visible-to>Everyone</visible-to>
..
</person>

So using ActiveResource, it just maps this to a ruby object or returns a hash?

Where is the definition of the object it returns?

Like the tag resource code is here: https://github.com/tapajos/highrise/blob/master/lib/highrise/tag.rb

module Highrise
  class Tag < Base  
    def ==(object)
      (object.instance_of?(self.class) && object.id == self.id && object.name == self.name)
    end
  end
end

Also if performance was a big concern, would once still use activeresource or are there faster ways to parse the xml?

Upvotes: 2

Views: 107

Answers (1)

agmin
agmin

Reputation: 9378

ActiveResource is handling all of the XML conversion. You'll notice in /lib/highrise/base.rb the format is set to XML. Checkout the ActiveResource documentation: http://api.rubyonrails.org/classes/ActiveResource/Base.html#method-c-format-3D

Sets the format that attributes are sent and received in from a mime type reference

Person.format = ActiveResource::Formats::XmlFormat

Person.find(1) # => GET /people/1.xml

ActiveResource itself is responsible for the mapping of any RESTful resource to a model. So the highrise gem points to RESTful resources in highrise and ActiveResource translates that into a rails model.

Upvotes: 1

Related Questions