Class: FreeAgent::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:, pagination: nil) ⇒ Collection

Returns a new instance of Collection.



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

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#paginationObject (readonly)

Returns the value of attribute pagination.



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

def pagination
  @pagination
end

#totalObject (readonly)

Returns the value of attribute total.



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

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

  data = body.values.first.map { |attrs| type.new(attrs) }

  total = response.headers["X-total-count"]

  pagination = response.headers["Link"]&.split(", ")&.map do |link|
    url, rel = link.match(/<(.+)>; rel='(.+)'/)&.captures
    [ rel, url ]
  end&.to_h

  new(
    data: data,
    total: total,
    pagination: pagination
  )
end

Instance Method Details

#countObject



30
31
32
# File 'lib/free_agent/collection.rb', line 30

def count
  data.count
end

#each(&block) ⇒ Object



34
35
36
# File 'lib/free_agent/collection.rb', line 34

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

#firstObject



38
39
40
# File 'lib/free_agent/collection.rb', line 38

def first
  data.first
end

#lastObject



42
43
44
# File 'lib/free_agent/collection.rb', line 42

def last
  data.last
end