3

ActiveResource を使用して、Redmine (バグ追跡ツール) が提供する REST Web サービスを使用しています。その Web サービスは、次のような 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 拡張機能はわずか 5 行です。ActiveResource を拡張し、サービスの URL、ユーザー名、およびパスワードを定義するだけです)。

それで、ActiveResourceがこのXMLを奇妙に解析しないようにする方法を知っている人はいますか?

4

1 に答える 1

1

これは明らかに ActiveResource の既知の問題です。

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

残念ながら、それについて何も行われていないようで、問題はクローズされました. 気が向いたら、すべての属性を保持するために ActiveResource と Hash.from_xml を更新するための Rails 3 コードはすべて以下の要点にあり、Redmine モジュールで調整されたバージョンを作成して修正することができます。

https://gist.github.com/971598

アップデート:

ActiveResource は Rails 4 コアの一部ではなく、別の gem としてスピンアウトされるように見えるため、別の方法として、 Herなどの REST API 用の代替 ORM を使用することになります。

彼女は、XML にカスタム パーサーを使用できるようにします。これは、Redmine::ParseXML と呼ばれるカスタム パーサーの例です。

https://gist.github.com/3879418

config/initializers/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"}}>
于 2012-10-12T12:34:36.610 に答える