Class: LemonSqueezy::Collection
- Inherits:
-
Object
- Object
- LemonSqueezy::Collection
- Defined in:
- lib/lemon_squeezy/collection.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(data:, meta:, total:) ⇒ Collection
constructor
A new instance of Collection.
Constructor Details
#initialize(data:, meta:, total:) ⇒ Collection
Returns a new instance of Collection.
36 37 38 39 40 |
# File 'lib/lemon_squeezy/collection.rb', line 36 def initialize(data:, meta:, total:) @data = data @meta = @total = total end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/lemon_squeezy/collection.rb', line 3 def data @data end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
3 4 5 |
# File 'lib/lemon_squeezy/collection.rb', line 3 def @meta end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
3 4 5 |
# File 'lib/lemon_squeezy/collection.rb', line 3 def total @total end |
Class Method Details
.from_response(response, type:, key: nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/lemon_squeezy/collection.rb', line 5 def self.from_response(response, type:, key: nil) body = response.body if key.is_a?(String) data = body["data"][key].map { |attrs| type.new(attrs) } total = body["data"]["total"] else data = body["data"].map { |attrs| type.new(attrs) } total = body["data"].count end # Extract pagination metadata if available = body["meta"] pagination = &.dig("page") new( data: data, meta: { page: { total: pagination&.dig("total"), current_page: pagination&.dig("currentPage"), from: pagination&.dig("from"), to: pagination&.dig("to"), last_page: pagination&.dig("lastPage"), per_page: pagination&.dig("perPage") } }, total: total ) end |