Class: Tiltify::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:, has_prev: nil, has_next: nil) ⇒ Collection

Returns a new instance of Collection.



16
17
18
19
20
21
# File 'lib/tiltify/collection.rb', line 16

def initialize(data:, total:, has_prev: nil, has_next: nil)
  @data = data
  @total = total
  @has_prev = has_prev
  @has_next = has_next
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#has_nextObject (readonly)

Returns the value of attribute has_next.



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

def has_next
  @has_next
end

#has_prevObject (readonly)

Returns the value of attribute has_prev.



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

def has_prev
  @has_prev
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Class Method Details

.from_response(response, type:) ⇒ Object



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

def self.from_response(response, type:)
  body = response.body

  new(
    data: body["data"].map { |attrs| type.new(attrs) },
    total: body["data"].count,
    has_prev: body["links"] && !body["links"]["prev"].blank?,
    has_next: body["links"] && !body["links"]["next"].blank?
  )
end