Sha256: 1d787a3126d2e7225b9cda1887c2a68008d23bae3c2634fc4c7bbbf7a387a0cf

Contents?: true

Size: 644 Bytes

Versions: 9

Compression:

Stored size: 644 Bytes

Contents

module Paddle
  class Collection
    attr_reader :data, :total

    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

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

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

    def first
      data.first
    end

    def last
      data.last
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
paddle-2.7.0 lib/paddle/collection.rb
paddle-2.6.0 lib/paddle/collection.rb
paddle-2.5.2 lib/paddle/collection.rb
paddle-2.5.1 lib/paddle/collection.rb
paddle-2.5.0 lib/paddle/collection.rb
paddle-2.4.1 lib/paddle/collection.rb
paddle-2.4.0 lib/paddle/collection.rb
paddle-2.3.0 lib/paddle/collection.rb
paddle-2.2.1 lib/paddle/collection.rb