require_relative 'helpers' module Tickethub class Collection < Enumerator attr_accessor :cache attr_reader :count, :endpoint def initialize(endpoint, klass, params = {}) @params = params.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.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.load(@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.load(@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] end def last offset(count - 1).first end def limit(value = nil) if value.nil? reload! if @limit.nil? return @limit else self.class.new @endpoint, @klass, @params.merge(limit: value) end end def offset(value = nil) if value.nil? reload! if @offset.nil? return @offset else self.class.new @endpoint, @klass, @params.merge(offset: value) end end def filter(value) self.class.new @endpoint, @klass, @params.merge(filters: (@params[:filters] || {}).merge(value)) end def filters @params[:filters].dup 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 [:get, :post, :patch, :delete].each do |key| define_method key do |path, params = {}, options = {}| endpoint = (if path.is_a? Hash params, options = [path, params] self.endpoint else self.endpoint[path] end) if (response = endpoint.send(key, @params.merge(params), options)).body.length > 2 return response.decoded end end end def empty? count.zero? end def any?(&block) block_given?? super : ! empty? end def limit self.reload! if @limit.nil? return @limit end def count self.reload! if @count.nil? return @count end def [](*args) case (key = args[0]) when Fixnum self.offset(key).first when Hash self.filter(key).first when Range self.offset(key.min).first(key.max) when String options = { params: args[1] || {} }.merge args[2] || {} @klass.load @klass.endpoint[key, @endpoint[nil, options].options] else raise ArgumentError, 'invalid search value type' end end def create(attributes = {}) @klass.load @endpoint, @endpoint.post(attributes).decoded rescue Tickethub::ResourceInvalid => err @klass.load @endpoint, Tickethub::Response.new(err.response).decoded end def scope(key, params = {}, options = {}) Tickethub::Collection.new @endpoint[key, options.merge(params: params)], @klass, filters: @filters, order: @order end end end