Class: Paddle::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:) ⇒ Collection

Returns a new instance of Collection.



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

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#totalObject (readonly)

Returns the value of attribute total.



5
6
7
# File 'lib/paddle/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
16
17
18
19
20
21
22
# File 'lib/paddle/collection.rb', line 7

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

  data  = body["data"].map { |attrs| type.new(attrs) }

  if body["meta"]["pagination"]
    total = body["meta"]["pagination"]["estimated_total"]
  else
    total = body["data"].count
  end

  new(
    data: data,
    total: total
  )
end

Instance Method Details

#each(&block) ⇒ Object



29
30
31
# File 'lib/paddle/collection.rb', line 29

def each(&block)
  data.each(&block)
end

#firstObject



33
34
35
# File 'lib/paddle/collection.rb', line 33

def first
  data.first
end

#lastObject



37
38
39
# File 'lib/paddle/collection.rb', line 37

def last
  data.last
end