我怎样才能使的ActiveResource XML解析更稳定?

问题描述:

我使用的ActiveResource消耗由管理平台提供的REST Web服务(一个bug跟踪工具)。这webservice的类似下面生成XML:我怎样才能使的ActiveResource XML解析更稳定?

<custom_field name="Issue Owner" id="15">Fred Fake</custom_field> 
<custom_field name="Needs Printing" id="16">0</custom_field> 
<custom_field name="Review Assignee" id="17">Fran Fraud</custom_field> 
<custom_field name="Released On" id="20"></custom_field> 
<custom_field name="Client Facing" id="21">0</custom_field> 
<custom_field name="Type" id="22">Bug</custom_field> 
<custom_field name="QA Assignee" id="23"></custom_field> 
<custom_field name="Company Name" id="26"></custom_field> 
<custom_field name="QA Notes" id="27"></custom_field> 
<custom_field name="Failed QA Attempts" id="28">2</custom_field> 

然而,当的ActiveResource分析这个问题,我遍历这些结果打印出来,我得到:

Fred Fake 
0 
Fran Fraud 
#<Redmine::Issue::CustomFields::CustomField:0x5704e95d> 
0 
Bug 
#<Redmine::Issue::CustomFields::CustomField:0x32fd963> 
#<Redmine::Issue::CustomFields::CustomField:0x3a68f437> 
#<Redmine::Issue::CustomFields::CustomField:0x407964d6> 
2 

这是正确的,它抛出了所有的来自任何具有值的属性信息,但保留来自空元素的属性信息。

不用说,这使得当你试图找到ID 15(或其他)的有价值的东西相当困难。现在我可以通过他们的位置来引用事情,但这非常脆弱,因为这些元素将来可能会发生变化。我认为必须有一些方法让ActiveResource保留属性信息,但是因为我没有做任何特别的事情。 (我的ActiveResource扩展只有五行:它扩展了ActiveResource,定义了服务的URL,用户名和密码,就是这样)。

那么,有没有人知道我可以如何让ActiveResource不这么奇怪地解析这个XML?

+0

这将是很酷,如果你可以发布的代码片做印刷和解析。 – karlcow 2011-02-05 13:16:38

这是一个已知的问题明显的ActiveResource:

https://github.com/rails/rails/issues/588

不幸的是,没有出现关于它&问题被关闭工作要做。如果你对此感到满意,Rails 3代码用于更新ActiveResource和Hash.from_xml以保留所有属性,都在下面的要点中,你可以在你的Redmine模块中创建一个定制版本来修复它:

https://gist.github.com/971598

更新:

的替代,因为它出现ActiveResource will not be part of Rails 4 core并作为一个独立的创业板将被剥离出来,将使用替代的ORM的REST API,像Her

她的允许您为XML使用自定义分析器。这就是所谓的管理平台:: ParseXML一个例子定制解析器:

https://gist.github.com/3879418

所以,那么所有你需要做的是创造这样的配置/初始化/ her.rb文件:

Her::API.setup :url => "https://api.xxxxx.org" do |connection| 
    connection.use Faraday::Request::UrlEncoded 
    connection.use Redmine::ParseXML 
    connection.use Faraday::Adapter::NetHttp 
end 

和你得到如下的哈希:

#<Redmine::Issue(issues) issues={:attributes=>{:type=>"array", :count=>1640}, 
:issue=>{:id=>4326, 
    :project=>{:attributes=>{:name=>"Redmine", :id=>1}}, 
    :tracker=>{:attributes=>{:name=>"Feature", :id=>2}}, 
    :status=>{:attributes=>{:name=>"New", :id=>1}}, 
    :priority=>{:attributes=>{:name=>"Normal", :id=>4}}, 
    :author=>{:attributes=>{:name=>"John Smith", :id=>10106}}, 
    :category=>{:attributes=>{:name=>"Email notifications", :id=>9}}, 
    :subject=>"\n  Aggregate Multiple Issue Changes for Email Notifications\n ", 
    :description=>"\n  This is not to be confused with another useful proposed feature that\n  would do digest emails for notifications.\n ", 
    :start_date=>"2009-12-03", 
    :due_date=>{}, 
    :done_ratio=>0, 
    :estimated_hours=>{}, 
    :custom_fields=>{ 
     :custom_field=>[ 
      {:attributes=>{:name=>"Issue Owner", :id=>15}, "value"=>"Fred Fake"}, 
      {:attributes=>{:name=>"Needs Printing", :id=>16}, "value"=>0}, 
      {:attributes=>{:name=>"Review Assignee", :id=>17}, "value"=>"Fran Fraud"}, 
      {:attributes=>{:name=>"Released On", :id=>20}}, 
      {:attributes=>{:name=>"Client Facing", :id=>21}, "value"=>0}, 
      {:attributes=>{:name=>"Type", :id=>22}, "value"=>"Bug"}, 
      {:attributes=>{:name=>"QA Assignee", :id=>23}}, 
      {:attributes=>{:name=>"Company Name", :id=>26}}, 
      {:attributes=>{:name=>"QA Notes", :id=>27}}, 
      {:attributes=>{:name=>"Failed QA Attempts", :id=>28}, "value"=>2}]}, 
    :created_on=>"Thu Dec 03 15:02:12 +0100 2009", 
    :updated_on=>"Sun Jan 03 12:08:41 +0100 2010"}}>