VegaStudios
VegaStudios

Reputation: 324

Rails: Fetch feed entries from certain date with Feedjira

How's it going SO'ers?

I've appended a Recent News section to the top of Users Dashboard, where it fetches news stories through Feedjira, saves the NewsEntry to the database (postgres) and then picks three random NewsEntries to display in the Dashboard#index view.

I've also created a rake task NewsEntry.update_from_feed(feed_url) to update the table with new NewsEntries scheduled to run once every 24 hours.

My problem, because it's a Recent News section I would like to fetch the NewsEntries that were published today (or yesterday; this could be a time as well). Eventually I'll add to the rake task to remove old articles and update the feed with newer articles. Right now I am getting an error trying to fetch entries on a certain date:

NoMethodError: undefined method `where' for #<Array:0x00007fcab2910d00>

NewsEntry

class NewsEntry < ApplicationRecord
  def self.update_from_feed(feed_url)
    feed = Feedjira::Feed.fetch_and_parse(feed_url)
    add_entries(feed.entries)
  end

  private

  def self.add_entries(entries)
    today = Date.today
    entries.where("published_at = ?", today) do |entry|
      unless exists? :guid => entry.id
        create!(
          :title         => entry.title,
          :url          => entry.url,
          :published_at => entry.published,
          :guid         => entry.id
        )
      end
    end
  end
end

Thanks in advance for any help!

Upvotes: 0

Views: 185

Answers (0)

Related Questions