How to get custom element attributes not values in Feedzirra

131 Views Asked by At

I have an RSS feed that looks like

<item>
<title>Blah</title>
<enc:enclosure resource="http://hello.jpg" type="image/jpeg"/>
</item>

Using Feedzirra I am trying to retrieve the URL of the resource.

I have tried

Feedzirra::Feed.add_common_feed_entry_element('enc:enclosure', :as => :img)
feed.entries.first.img

And that always returns Nil, which I think is because the element has no value.
I also tried

 Feedzirra::Feed.add_common_feed_entry_element('enc:enclosure', :resource => :res, :as => :img)
 feed.entries.first.res

And img is nil and res returns undefined method.

So how to I get the elements attribute value "resource"?

1

There are 1 best solutions below

0
On

After really looking into the rss_entry.rb

https://github.com/feedjira/feedjira/blob/master/lib/feedjira/parser/rss_entry.rb

I figured out how I had it backwards by trying to follow the "media:content" example

So here is the correct code.

Feedzirra::Feed.add_common_feed_entry_element('enc:enclosure', :as => :img, :value=>:resource)
feed.entries.first.img

returns "http://hello.jpg"

If I understand it right, the :as tells the parser what I want to call it. And then sets the value of that to the attribute of interest, in this case resource.

Hope this helps someone.