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.



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

#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
# 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

#firstObject



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

def first
  data.first
end

#lastObject



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

def last
  data.last
end