module Redd::Clients::Base::Stream

Methods that stream delicious content into your bot's lazy mouth.

Public Instance Methods

stream(meth = :get_new, *args, **kwargs) { |thing| ... } click to toggle source

Stream the results of a method call to the given block. @param [Symbol] meth A method that returns a listing and has a

keyword parameter named `:before`.

@param [Array] args The arguments supplied to the method. @param [Hash] kwargs The keyword arguments supplied to the method. @yield An element of the returned listing. rubocop:disable Metrics/AbcSize, Metrics/MethodLength

# File lib/redd/clients/base/stream.rb, line 56
def stream(meth = :get_new, *args, **kwargs)
  bset = PRAWBoundedQueueSet.new(10)
  before = ''
  loop do
    begin
      # Get the latest comments from the subreddit.
      params = kwargs.merge(before: before)
      listing = send(meth, *args, **params)
      # Run the loop for each of the item in the listing
      listing.reverse_each do |thing|
        yield thing if bset.enqueue?(thing.fullname)
      end
      # Set the latest comment.
      before = listing.first.fullname unless listing.empty?
    rescue Redd::Error::RateLimited => error
      # If this error pops up, you probably have an issue with your bot.
      sleep(error.time)
    rescue Redd::Error => error
      # 5-something errors are usually errors on reddit's end.
      raise error unless (500...600).cover?(error.code)
    end
  end
end