Class: DiscordRDA::MessageIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/discord_rda/entity/channel.rb

Overview

Iterator for paginating through channel messages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, batch_size: 100, direction: :backwards) ⇒ MessageIterator

Initialize iterator

Parameters:

  • channel (Channel)

    Channel to iterate

  • batch_size (Integer) (defaults to: 100)

    Messages per batch

  • direction (Symbol) (defaults to: :backwards)

    :backwards (older first) or :forwards (newer first)



381
382
383
384
385
386
387
388
# File 'lib/discord_rda/entity/channel.rb', line 381

def initialize(channel, batch_size: 100, direction: :backwards)
  @channel = channel
  @batch_size = batch_size
  @direction = direction
  @last_id = nil
  @buffer = []
  @exhausted = false
end

Instance Attribute Details

#batch_sizeInteger (readonly)

Returns Messages per batch.

Returns:

  • (Integer)

    Messages per batch



372
373
374
# File 'lib/discord_rda/entity/channel.rb', line 372

def batch_size
  @batch_size
end

#channelChannel (readonly)

Returns Channel being iterated.

Returns:

  • (Channel)

    Channel being iterated



369
370
371
# File 'lib/discord_rda/entity/channel.rb', line 369

def channel
  @channel
end

#directionSymbol (readonly)

Returns Direction (:backwards or :forwards).

Returns:

  • (Symbol)

    Direction (:backwards or :forwards)



375
376
377
# File 'lib/discord_rda/entity/channel.rb', line 375

def direction
  @direction
end

Instance Method Details

#each {|Message| ... } ⇒ Object

Iterate over all messages

Yields:



406
407
408
409
410
411
412
413
414
415
# File 'lib/discord_rda/entity/channel.rb', line 406

def each
  return to_enum unless block_given?

  loop do
    message = self.next
    break unless message

    yield message
  end
end

#more?Boolean

Check if there are more messages

Returns:

  • (Boolean)

    True if more messages available



400
401
402
# File 'lib/discord_rda/entity/channel.rb', line 400

def more?
  !@exhausted || !@buffer.empty?
end

#nextMessage?

Get next message

Returns:

  • (Message, nil)

    Next message or nil if exhausted



392
393
394
395
396
# File 'lib/discord_rda/entity/channel.rb', line 392

def next
  fill_buffer if @buffer.empty? && !@exhausted

  @buffer.shift
end

#resetself

Reset the iterator

Returns:

  • (self)


419
420
421
422
423
424
# File 'lib/discord_rda/entity/channel.rb', line 419

def reset
  @last_id = nil
  @buffer = []
  @exhausted = false
  self
end