passworks/passworks-ruby

View on GitHub
lib/passworks/collection_proxy.rb

Summary

Maintainability
A
25 mins
Test Coverage
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