How can I make ActiveResource XML parsing more consistent?

I am using ActiveResource to use the REST web service provided by Redmine (error tracking tool). This web service creates XML as follows:

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

However, when ActiveResource parses this, and I repeat the print results, I get:

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

That's right, it discards all attribute information from something with a value, but stores attribute information from empty elements.

, , id 15 ( - ). , , , , . , - ActiveResource , .

( ActiveResource : ActiveResource, URL-, , ).

, - , ActiveResource XML ?

+3
1

ActiveResource, -:

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

, , . , Rails 3 ActiveResource Hash.from_xml , Redmine, :

https://gist.github.com/971598

Update:

, ActiveResource Rails 4 , ORM API REST, Her.

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

and you will get the hash as below:

#<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"}}>
+1
source

Source: https://habr.com/ru/post/1788273/


All Articles