Sha256: 810f39a5deabcfb787c94b866d1d09099f39b7a156fae35bb5ee985dfa0b7106

Contents?: true

Size: 1.19 KB

Versions: 17

Compression:

Stored size: 1.19 KB

Contents

module Mollie
  class List < Base
    include Enumerable

    attr_accessor :klass, :items, :_links
    alias links _links

    def initialize(list_attributes, klass)
      @klass = klass

      list_attributes['items'] ||= if list_attributes['_embedded']
                                     list_attributes['_embedded'].fetch(klass.resource_name, [])
                                   else
                                     []
                                   end
      super list_attributes

      @items = items.map do |attributes|
        klass.new attributes
      end
    end

    def [](index)
      @items[index]
    end

    def size
      @items.size
    end

    def each(&block)
      @items.each(&block)
    end

    def next(options = {})
      return self.class.new({}, klass) if links['next'].nil?

      href = URI.parse(links['next']['href'])
      query = URI.decode_www_form(href.query).to_h

      klass.all(options.merge(query))
    end

    def previous(options = {})
      return self.class.new({}, klass) if links['previous'].nil?

      href = URI.parse(links['previous']['href'])
      query = URI.decode_www_form(href.query).to_h

      klass.all(options.merge(query))
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
mollie-api-ruby-4.7.1 lib/mollie/list.rb
mollie-api-ruby-4.7.0 lib/mollie/list.rb
mollie-api-ruby-4.6.2 lib/mollie/list.rb
mollie-api-ruby-4.6.1 lib/mollie/list.rb
mollie-api-ruby-4.6.0 lib/mollie/list.rb
mollie-api-ruby-4.5.0 lib/mollie/list.rb
mollie-api-ruby-4.4.1 lib/mollie/list.rb
mollie-api-ruby-4.4.0 lib/mollie/list.rb
mollie-api-ruby-4.3.1 lib/mollie/list.rb
mollie-api-ruby-4.3.0 lib/mollie/list.rb
mollie-api-ruby-4.2.0 lib/mollie/list.rb
mollie-api-ruby-4.1.5 lib/mollie/list.rb
mollie-api-ruby-4.1.4 lib/mollie/list.rb
mollie-api-ruby-4.1.3 lib/mollie/list.rb
mollie-api-ruby-4.1.2 lib/mollie/list.rb
mollie-api-ruby-4.1.1 lib/mollie/list.rb
mollie-api-ruby-4.1.0 lib/mollie/list.rb