HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsMinor

Improving Rails feed parser

Submitted by: @import:stackexchange-codereview··
0
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?

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
end


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

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
end

Context

StackExchange Code Review Q#13591, answer score: 2

Revisions (0)

No revisions yet.