Class: Bgg::Plays::Iterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bgg/plays_iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username) ⇒ Iterator

Returns a new instance of Iterator.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#iterationObject (readonly)

Returns the value of attribute iteration.



6
7
8
# File 'lib/bgg/plays_iterator.rb', line 6

def iteration
  @iteration
end

#total_countObject (readonly)

Returns the value of attribute total_count.



6
7
8
# File 'lib/bgg/plays_iterator.rb', line 6

def total_count
  @total_count
end

Instance Method Details

#each(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/bgg/plays_iterator.rb', line 43

def empty?
  @empty
end