Class: Vultr::Collection

Inherits:
Object
  • Object
show all
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.



15
16
17
18
19
20
# File 'lib/vultr/collection.rb', line 15

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.



3
4
5
# File 'lib/vultr/collection.rb', line 3

def data
  @data
end

#next_cursorObject (readonly)

Returns the value of attribute next_cursor.



3
4
5
# File 'lib/vultr/collection.rb', line 3

def next_cursor
  @next_cursor
end

#prev_cursorObject (readonly)

Returns the value of attribute prev_cursor.



3
4
5
# File 'lib/vultr/collection.rb', line 3

def prev_cursor
  @prev_cursor
end

#totalObject (readonly)

Returns the value of attribute total.



3
4
5
# File 'lib/vultr/collection.rb', line 3

def total
  @total
end

Class Method Details

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



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

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