Class: Versafleet::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/versafleet/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, page:, per_page:, total:) ⇒ Collection

Returns a new instance of Collection.



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

def initialize(data:, page:, per_page:, total:)
  @data = data
  @page = page
  @per_page = per_page
  @total = total
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



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

def per_page
  @per_page
end

#totalObject (readonly)

Returns the value of attribute total.



3
4
5
# File 'lib/versafleet/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/versafleet/collection.rb', line 5

def self.from_response(response, key:, type:)
  body = response.body
  new(
    data: body[key].map { |attrs| type.new(attrs) },
    page: body.dig("meta", "page"),
    per_page: body.dig("meta", "per_page"),
    total: body.dig("meta", "total")
  )
end