Ruby on rails 从Restful资源到Ruby对象的XML转换,性能考虑

Ruby on rails 从Restful资源到Ruby对象的XML转换,性能考虑,ruby-on-rails,rest,Ruby On Rails,Rest,我正在查看下面的URL,以了解流行的ruby gem如何映射将XML返回到ruby对象的Restful端点: https://github.com/tapajos/highrise/blob/master/lib/highrise/base.rb 我看到他们正在使用ActiveResource::Base,它在幕后神奇地实现了这一点 因此,您可以从URI返回某种xml,如: <person> <id type="integer">1</id> <

我正在查看下面的URL,以了解流行的ruby gem如何映射将XML返回到ruby对象的Restful端点:

https://github.com/tapajos/highrise/blob/master/lib/highrise/base.rb
我看到他们正在使用ActiveResource::Base,它在幕后神奇地实现了这一点

因此,您可以从URI返回某种xml,如:

<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>

1.
约翰
雌鹿
顶替
一个喜欢随机数据的人
http://us.linkedin.com/in/john-doe
5.
能源部公司。
2007-02-27T03:11:52Z
2007-03-10T15:11:52Z
每个人
..
所以使用ActiveResource,它只是将其映射到一个ruby对象或返回一个哈希

它返回的对象的定义在哪里

如标签资源代码所示:

模块高层
类标记<基
def==(对象)
(object.instance_of?(self.class)和&object.id==self.id和&object.name==self.name)
结束
结束
结束

另外,如果性能是一个大问题,您是否还会使用activeresource,或者是否有更快的方法来解析xml?

activeresource正在处理所有xml转换。您会注意到在/lib/highrise/base.rb中,格式设置为XML。签出ActiveResource文档:

设置从mime类型引用发送和接收属性的格式

Person.format=ActiveResource::Formats::XmlFormat

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

ActiveResource本身负责将任何RESTful资源映射到模型。因此highrise gem指向highrise中的RESTful资源,ActiveResource将其转换为rails模型

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