Class: Twitch::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/twitch/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:, cursor:) ⇒ Collection

Returns a new instance of Collection.



18
19
20
21
22
# File 'lib/twitch/collection.rb', line 18

def initialize(data:, total:, cursor:)
  @data = data
  @total = total
  @cursor = cursor
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



5
6
7
# File 'lib/twitch/collection.rb', line 5

def cursor
  @cursor
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/twitch/collection.rb', line 5

def data
  @data
end

#totalObject (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
16
# File 'lib/twitch/collection.rb', line 7

def self.from_response(response, type:)
  body = response.body
  data = body["data"] || []

  new(
    data: data.map { |attrs| type.new(attrs) },
    total: body["total"] || data.count,
    cursor: body.dig("pagination", "cursor")
  )
end

Instance Method Details

#each(&block) ⇒ Object



24
25
26
# File 'lib/twitch/collection.rb', line 24

def each(&block)
  data.each(&block)
end

#firstObject



28
29
30
# File 'lib/twitch/collection.rb', line 28

def first
  data.first
end

#lastObject



32
33
34
# File 'lib/twitch/collection.rb', line 32

def last
  data.last
end