class Bgg::Plays::Iterator

Attributes

iteration[R]
total_count[R]

Public Class Methods

new(username) click to toggle source
# File lib/bgg/plays_iterator.rb, line 8
def initialize(username)
  @iteration = 0
  @page = 1
  @empty = false
  @total_count = 0
  @username = username

  begin
    raw_data = BggApi.plays({username: @username, page: @page})
    @total_count = raw_data.fetch('total', '0').to_i
    @items = raw_data['play']
    @empty = true if @total_count == 0
  rescue REXML::ParseException
    # this will happen if the user does not exist
    raise ArgumentError.new('user does not exist')
  end
end

Public Instance Methods

each() { |play| ... } click to toggle source
# File lib/bgg/plays_iterator.rb, line 26
def each &block
  return if empty?

  while @page <= total_pages
    @items.each do |item|
      @iteration += 1
      yield Bgg::Play.new(item)
      return if @iteration == total_count
    end

    fetch_next_page
  end

ensure
  @empty = true
end
empty?() click to toggle source
# File lib/bgg/plays_iterator.rb, line 43
def empty?
  @empty
end

Private Instance Methods

fetch_next_page() click to toggle source
# File lib/bgg/plays_iterator.rb, line 54
def fetch_next_page
  fetch_page(@page + 1)
  @page = @page + 1
end
fetch_page(page) click to toggle source
# File lib/bgg/plays_iterator.rb, line 49
def fetch_page(page)
  raw_data = BggApi.plays({username: @username, page: page})
  @items = raw_data['play']
end
total_pages() click to toggle source
# File lib/bgg/plays_iterator.rb, line 59
def total_pages
  (total_count / 100.0).ceil.to_i
end