Reputation: 291
I've been using Feedzirra for a while to grab single feeds and parse them with no issues but I'm trying to run two separate feedburner feeds through it and I know that they work by themselves but running the both of them is producing an error.
NoMethodError in Feed entryController#index
undefined method `title' for #<Array:0x1042f9590>
My model looks like this:
def self.get_feeds
feed_urls = ["feed_1", "feed_2"]
update_from_feeds(feed_urls)
end
def self.update_from_feeds(feed_urls)
feeds = Feedzirra::Feed.fetch_and_parse(feed_urls)
add_entries(feeds.entries)
end
def self.update_from_feeds_continuously(feed_urls, delay_interval = 30.seconds)
feeds = Feedzirra::Feed.fetch_and_parse(feed_urls)
add_entries(feed.entries)
loop do
sleep delay_interval
feeds = Feedzirra::Feed.update(feeds.entries)
add_entries(feeds.new_entries) if feeds.updated?
end
end
private
def self.add_entries(entries)
entries.each do |entry|
unless exists? :guid => entry.id
create!(
:title => entry.title
)
end
end
end
It's probably something I'm doing wrong but I can't find any full code examples or tutorials for parsing multiple feeds, just the two lines of code on the github page.
Thanks in advance!
Upvotes: 1
Views: 993
Reputation: 3529
When feeds
is returned in update_from_feeds
from the fetch_and_parse
, it is an array when multiple feeds are passed in. As such, the each
in the add_entries
method is splitting into individual feeds, not feed entries. To remedy this, you can redefine update_from_feeds
as follows:
def self.update_from_feeds(feed_urls)
feeds = Feedzirra::Feed.fetch_and_parse(feed_urls)
feeds.each do |feed_url, feed|
add_entries(feed.entries)
end
end
This assumes you will always be passing multiple feed url's to update_from_feeds
(or a single url in an array)
Upvotes: 1