Sha256: a73aac5c54b998e4da07b58a1ce09cf785c8b2a5e6c8e2d1bc362a60e97ed942

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module MultiSafePay
  class List < Base
    include Enumerable

    attr_accessor :klass, :items, :pager

    def initialize(list_attributes, klass)
      @klass = klass
      @pager = list_attributes['pager'] || {}

      list_attributes['items'] ||= list_attributes['data'] || []
      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?
      !pager['after'].nil?
    end

    def previous?
      !pager['before'].nil?
    end

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

      href = URI.parse(pager['after'])
      query = URI.decode_www_form(href.query).to_h

      response = MultiSafePay::Client.instance.perform_http_call('GET', pager['after'], nil, {}, options.merge(query))
      self.class.new(response, klass)
    end

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

      href = URI.parse(pager['before'])
      query = URI.decode_www_form(href.query).to_h

      response = MultiSafePay::Client.instance.perform_http_call('GET', pager['before'], nil, {}, options.merge(query))
      self.class.new(response, klass)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
multisafepay-api-ruby-0.1.0 lib/multisafepay/list.rb