require 'cgi' require_relative 'helpers' module Tickethub class Collection < Enumerator DEFAULT_LIMIT = 25.freeze DEFAULT_OFFSET = 0.freeze attr_accessor :cache attr_reader :count, :endpoint, :params def initialize(endpoint, klass, params = {}, options = {}) @params = params.dup @options = options.dup @endpoint = endpoint @klass = klass klass.registered_types.each do |type, options| define_singleton_method type do instance_variable_defined?("@#{type}") ? instance_variable_get("@#{type}") : instance_variable_set("@#{type}", Tickethub::Collection.new(endpoint[type], options[:klass])) end end klass.collection_methods.merge(klass.scopes).each do |key, block| define_singleton_method key, &block end super() do |yielder| self.reload! if cache.nil? cache.each do |row| yielder << @klass.call(endpoint, row) end while (offset + cache.length) < count response = endpoint.get params.merge(offset: cache.length) response.decoded.each do |row| cache << row yielder << @klass.call(endpoint, row) end end end end def reload! @cache = (response = endpoint.get params).decoded @count, @offset, @limit = response.status == 206 ? response.headers.values_at(*%w(x-total-count x-offset x-limit)) .collect { |value| value[0].to_i } : [@cache.length, 0, @cache.length] return self end def last offset(count - 1).first end def limit(value = nil) if value.nil? return (@limit || params[:limit] || DEFAULT_LIMIT).to_i else self.class.new endpoint, @klass, params.merge(limit: value) end end def offset(value = nil) if value.nil? return (@offset || params[:offset] || DEFAULT_OFFSET).to_i else self.class.new endpoint, @klass, params.merge(offset: value) end end def filter(value) self.class.new endpoint, @klass, params.merge(filters: filters.merge(value)) end def filters (params[:filters] || {}).dup end def search(value) self.filter(search: value) end def order(value = nil) return params[:order].dup if value.nil? order = (params[:order] || []) + (case value when Symbol, String then [value.to_s] when Hash value.collect do |key, direction| direction.to_s == 'desc' ? "-#{key}" : key end end) self.class.new endpoint, @klass, params.merge(order: order) end def scope(path, params = {}) self.class.new endpoint[path], @klass, self.params.merge(params) end [:get, :post, :patch, :delete].each do |key| define_method key do |path, params = {}| endpoint = (if path.is_a? Hash params, options = [path, params] self.endpoint else self.endpoint[path] end) if (response = endpoint.send(key, self.params.merge(params))).body.length >= 2 return response.decoded end end end def empty? count.zero? end def any?(&block) block_given?? super : ! empty? end def count self.reload! if @count.nil? return @count end def [](search) case search when Fixnum self.offset(search).first when Hash self.filter(search).first when Range self.offset(search.min).first(search.max) when Array self.filter(id: search) when String @klass.call endpoint, CGI::escape(search), @options else raise ArgumentError, 'invalid search value type' end end def create(attributes = {}) @klass.call endpoint, post(attributes), @options rescue Tickethub::ResourceInvalid => err @klass.call endpoint, Tickethub::Response.new(err.response).decoded, @options end end end