Class: RAWG::Collection
- Inherits:
-
Object
- Object
- RAWG::Collection
- Includes:
- Enumerable
- Defined in:
- lib/rawg/collection.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#items_class ⇒ Object
readonly
Returns the value of attribute items_class.
Instance Method Summary collapse
- #count(*args) ⇒ Object
- #each ⇒ Object
- #from_api_response(response) ⇒ Object
-
#initialize(klass, client: RAWG::Client.new) ⇒ Collection
constructor
A new instance of Collection.
Constructor Details
#initialize(klass, client: RAWG::Client.new) ⇒ Collection
Returns a new instance of Collection.
9 10 11 12 13 14 15 |
# 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 |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
7 8 9 |
# File 'lib/rawg/collection.rb', line 7 def client @client end |
#items_class ⇒ Object (readonly)
Returns the value of attribute items_class.
7 8 9 |
# File 'lib/rawg/collection.rb', line 7 def items_class @items_class end |
Instance Method Details
#count(*args) ⇒ Object
42 43 44 45 46 |
# File 'lib/rawg/collection.rb', line 42 def count(*args) return super if !args.empty? || block_given? @count end |
#each ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# 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 |