Class: Vultr::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:, next_cursor:, prev_cursor:) ⇒ Collection

Returns a new instance of Collection.



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

def initialize(data:, total:, next_cursor:, prev_cursor:)
  @data = data
  @total = total
  @next_cursor = next_cursor.empty? ? nil : next_cursor
  @prev_cursor = prev_cursor.empty? ? nil : prev_cursor
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#next_cursorObject (readonly)

Returns the value of attribute next_cursor.



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

def next_cursor
  @next_cursor
end

#prev_cursorObject (readonly)

Returns the value of attribute prev_cursor.



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

def prev_cursor
  @prev_cursor
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Class Method Details

.from_response(response, key:, type:) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/vultr/collection.rb', line 7

def self.from_response(response, key:, type:)
  body = response.body
  new(
    data: body[key].map { |attrs| type.new(attrs) },
    total: body.dig("meta", "total"),
    next_cursor: body.dig("meta", "links", "next"),
    prev_cursor: body.dig("meta", "links", "prev")
  )
end

Instance Method Details

#each(&block) ⇒ Object



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

def each(&block)
  return enum_for(:each) unless block_given?
  @data.each(&block)
end