Class: Twitch::Collection
- Inherits:
-
Object
- Object
- Twitch::Collection
- Includes:
- Enumerable
- Defined in:
- lib/twitch/collection.rb
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #first ⇒ Object
-
#initialize(data:, total:, cursor:) ⇒ Collection
constructor
A new instance of Collection.
- #last ⇒ Object
Constructor Details
#initialize(data:, total:, cursor:) ⇒ Collection
Returns a new instance of Collection.
17 18 19 20 21 |
# File 'lib/twitch/collection.rb', line 17 def initialize(data:, total:, cursor:) @data = data @total = total @cursor = cursor.nil? ? nil : cursor end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
5 6 7 |
# File 'lib/twitch/collection.rb', line 5 def cursor @cursor end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/twitch/collection.rb', line 5 def data @data end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
5 6 7 |
# File 'lib/twitch/collection.rb', line 5 def total @total end |
Class Method Details
.from_response(response, type:) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/twitch/collection.rb', line 7 def self.from_response(response, type:) body = response.body new( data: body["data"].map { |attrs| type.new(attrs) }, total: body["data"].count, cursor: body.dig("pagination", "cursor") ) end |
Instance Method Details
#each(&block) ⇒ Object
23 24 25 |
# File 'lib/twitch/collection.rb', line 23 def each(&block) data.each(&block) end |
#first ⇒ Object
27 28 29 |
# File 'lib/twitch/collection.rb', line 27 def first data.first end |
#last ⇒ Object
31 32 33 |
# File 'lib/twitch/collection.rb', line 31 def last data.last end |