How to make feed_entries downloaded every 3 minutes in mongoid

72 views Asked by At

I have two models source.rb and feed_entries.rb. How to make feed_entries downloaded every 3 minutes. I've tried through gem 'whenever' and received an error NoMethodError: private method `select'

source.rb

class Source
  include Mongoid::Document
  include Mongoid::Timestamps

  field :url, type: String
  field :title, type: String
  field :site, type: String

  after_save :load_feed_entries

  has_many :feed_entries, dependent: :destroy

  def load_feed_entries
    last_entry = self.feed_entries.last
    if last_entry.nil?
      feed = Feedjira::Feed.fetch_and_parse(self.url)
    else
      feed = Feedjira::Feed.fetch_and_parse(self.url,
        :if_modified_since => last_entry.published_at.to_time)
    end

    Source.add_entries(feed.entries, self.id)
  end

  def self.update_all_feeds(urls)
    Feedjira::Feed.fetch_and_parse(urls,
      :on_success => lambda { |url, feed|
        rss = Source.select("sources.id").where(:url => url).first
        Source.add_entries(feed.entries, rss.id)
      }
    )
  end

  private
    def self.add_entries(entries, stream_id)
      entries.each do |entry|
          unless  FeedEntry.where(guid: entry.id).exists?
            FeedEntry.create!(
              :name          => entry.title,
              :summary       => entry.summary,
              :url           => entry.url,
              :content       => entry.content,
              :published_at  => entry.published,
              :guid          => entry.id,
              :source_id => stream_id,
            )
          end
      end
    end
end

feed.rake

task :collect => :environment do
  urls = Source.select("sources.url").all.map { |v| v[:url] }
  Source.update_all_feeds(urls)
end
1

There are 1 answers

0
Anton Ipatov On BEST ANSWER

source.rb

  def self.update_all_feeds(urls)
    Feedjira::Feed.fetch_and_parse(urls,
      :on_success => lambda { |url, feed|
        rss = Source.only(:id).where(:url => url).first
        Source.add_entries(feed.entries, rss.id)
      }
    )
  end

feed.rake

task :collect => :environment do
  urls = Source.only(:url).all.map { |v| v[:url] }
  Source.update_all_feeds(urls)
end