Class: FreeAgent::Collection
- Inherits:
-
Object
- Object
- FreeAgent::Collection
- Defined in:
- lib/free_agent/collection.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#pagination ⇒ Object
readonly
Returns the value of attribute pagination.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
- #count ⇒ Object
- #each(&block) ⇒ Object
- #first ⇒ Object
-
#initialize(data:, total:, pagination: nil) ⇒ Collection
constructor
A new instance of Collection.
- #last ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/free_agent/collection.rb', line 3 def data @data end |
#pagination ⇒ Object (readonly)
Returns the value of attribute pagination.
3 4 5 |
# File 'lib/free_agent/collection.rb', line 3 def pagination @pagination end |
#total ⇒ Object (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
#count ⇒ Object
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 |
#first ⇒ Object
38 39 40 |
# File 'lib/free_agent/collection.rb', line 38 def first data.first end |
#last ⇒ Object
42 43 44 |
# File 'lib/free_agent/collection.rb', line 42 def last data.last end |