Module: Redd::Clients::Base::Stream

Included in:
Redd::Clients::Base
Defined in:
lib/redd/clients/base/stream.rb

Overview

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

Defined Under Namespace

Classes: PRAWBoundedQueueSet

Instance Method Summary collapse

Instance Method Details

#stream(meth = :get_new, *args, **kwargs) { ... } ⇒ Object

Stream the results of a method call to the given block. rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Parameters:

  • meth (Symbol) (defaults to: :get_new)

    A method that returns a listing and has a keyword parameter named ‘:before`.

  • args (Array)

    The arguments supplied to the method.

  • kwargs (Hash)

    The keyword arguments supplied to the method.

Yields:

  • An element of the returned listing.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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