patternrubyrailsMinor
Improving Rails feed parser
Viewed 0 times
parserrailsimprovingfeed
Problem
This is my feed parser method using feedzirra. It works, but I feel dirty because I can't figure out how to improve this code. Any suggestions?
This is the object returned from the fetch_and_parse method: with just the first URL
I've tried everything, but only that solution works.
class Feed entry.entry_id
create!(
:name => entry.title,
:summary => entry.summary,
:url => entry.url,
:published_at => entry.published,
:guid => entry.entry_id
)
end
end
end
end
endThis is the object returned from the fetch_and_parse method: with just the first URL
{"http://www.agichina24.it/home/agenzia-nuova-cina/rss2"=>#, #, #, #, #, #, #, #, #, #], @feed_url="http://www.agichina24.it/home/agenzia-nuova-cina/rss2", @etag=nil, @last_modified=nil>}I've tried everything, but only that solution works.
Solution
Slight cleanup:
-
No need to store a local variable called feeds anymore.
-
If you call .each on a hash, you can use |key, value| in the block.
2a. Since url isn't getting used anymore, |url, feeds| becomes |_, feeds|.
Thoughts?
class Feed < ActiveRecord::Base
# ...
def self.update_from_feed
feed_urls = ["url1", "url2", "ulr3", "url4"]
Feedzirra::Feed.fetch_and_parse(feed_urls).each do |_, feeds|
feeds.entries.each do |entry|
# ...
end
end
end
end-
No need to store a local variable called feeds anymore.
-
If you call .each on a hash, you can use |key, value| in the block.
2a. Since url isn't getting used anymore, |url, feeds| becomes |_, feeds|.
Thoughts?
Code Snippets
class Feed < ActiveRecord::Base
# ...
def self.update_from_feed
feed_urls = ["url1", "url2", "ulr3", "url4"]
Feedzirra::Feed.fetch_and_parse(feed_urls).each do |_, feeds|
feeds.entries.each do |entry|
# ...
end
end
end
endContext
StackExchange Code Review Q#13591, answer score: 2
Revisions (0)
No revisions yet.