class RAWG::Collection

Attributes

client[R]
items_class[R]

Public Class Methods

new(klass, client: RAWG::Client.new) click to toggle source
# File lib/rawg/collection.rb, line 9
def initialize(klass, client: RAWG::Client.new)
  @items_class = klass
  @client = client
  @count = 0
  @items = []
  @next_page_url = nil
end

Public Instance Methods

count(*args) click to toggle source
Calls superclass method
# File lib/rawg/collection.rb, line 42
def count(*args)
  return super if !args.empty? || block_given?

  @count
end
each() { |items| ... } click to toggle source
# File lib/rawg/collection.rb, line 27
def each
  i = 0
  loop do
    raise StopIteration if i == @count - 1

    if !@items[i] && @next_page_url
      response = @client.get(@next_page_url)
      from_api_response(response)
    end

    yield @items[i]
    i += 1
  end
end
from_api_response(response) click to toggle source
# File lib/rawg/collection.rb, line 17
def from_api_response(response)
  @next_page_url = response[:next]
  @count = response[:count]
  response[:results].each do |attrs|
    new_item = @items_class.new(client: @client).from_api_response(attrs)
    @items.push(new_item)
  end
  self
end

Private Instance Methods

fetch_next_page() click to toggle source
# File lib/rawg/collection.rb, line 50
def fetch_next_page
  # response = @client.get(@next_page_url)
  # fetched_items = response[:results].map do |item|
  #   @items_class.new(client: @client).from_api_response(item)
  # end
  # @items.push(*fetched_items)
end