Sha256: 4b17c592159247511914e7624ae5d7b3d2d55fa50400fa7bd2cc5a560cb60535

Contents?: true

Size: 1.17 KB

Versions: 9

Compression:

Stored size: 1.17 KB

Contents

require 'passworks/inflector'

module Passworks
  class CollectionProxy

    include Enumerable
    include Passworks::Inflector

    attr_reader :collection_name, :collection_uuid, :client, :options

    def initialize(client, collection_name, collection_uuid=nil, options={})
      @collection_name  = collection_name
      @collection_uuid  = collection_uuid
      @client           = client
      @options          = options
    end

    def each(&block)
      next_page = nil
      loop do
        if next_page
          response = client.get(collection_url, options.merge({query: {page: next_page}}))
        else
          response = client.get(collection_url, options)
        end
        response.data.each do |item_data|
          yield resource_class.new(client, collection_name, item_data)
        end
        # Kaminari returns next_page as an empty string if there aren't more pages
        next_page = response.next_page.to_s.to_i
        break if next_page == 0
      end
      self
    end

    private

      def collection_url
        if collection_uuid
          "#{collection_name}/#{collection_uuid}/passes"
        else
          collection_name
        end
      end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
passworks-2.0.7 lib/passworks/collection_proxy.rb
passworks-2.0.6 lib/passworks/collection_proxy.rb
passworks-2.0.5 lib/passworks/collection_proxy.rb
passworks-2.0.4 lib/passworks/collection_proxy.rb
passworks-2.0.3 lib/passworks/collection_proxy.rb
passworks-2.0.2 lib/passworks/collection_proxy.rb
passworks-2.0.1 lib/passworks/collection_proxy.rb
passworks-2.0.0 lib/passworks/collection_proxy.rb
passworks-0.0.4 lib/passworks/collection_proxy.rb