lib/tickethub/collection.rb in tickethub-0.1.4 vs lib/tickethub/collection.rb in tickethub-0.2.0
- old
+ new
@@ -1,50 +1,52 @@
+require 'cgi'
require_relative 'helpers'
module Tickethub
class Collection < Enumerator
attr_accessor :cache
- attr_reader :count, :endpoint
+ attr_reader :count, :endpoint, :params
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]))
+ 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?
+ self.reload! if cache.nil?
- @cache.each do |row|
- yielder << @klass.load(@endpoint, row)
+ 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)
+ 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
+ @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
@@ -52,56 +54,61 @@
def limit(value = nil)
if value.nil?
reload! if @limit.nil?
return @limit
else
- self.class.new @endpoint, @klass,
- @params.merge(limit: value)
+ 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)
+ 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))
+ self.class.new endpoint, @klass,
+ params.merge(filters: filters.merge(value))
end
def filters
- @params[:filters].dup
+ (params[:filters] || {}).dup
end
def order(value = nil)
- return @params[:order].dup if value.nil?
- order = (@params[:order] || []) + (case value
+ 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)
+ 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 = {}, options = {}|
+ 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, @params.merge(params), options)).body.length > 2
+ if (response = endpoint.send(key, self.params.merge(params))).body.length > 2
return response.decoded
end
end
end
@@ -121,33 +128,28 @@
def count
self.reload! if @count.nil?
return @count
end
- def [](*args)
- case (key = args[0])
+ def [](search)
+ case search
when Fixnum
- self.offset(key).first
+ self.offset(search).first
when Hash
- self.filter(key).first
+ self.filter(search).first
when Range
- self.offset(key.min).first(key.max)
+ self.offset(search.min).first(search.max)
when String
- options = { params: args[1] || {} }.merge args[2] || {}
- @klass.load @klass.endpoint[key, @endpoint[nil, options].options]
+ endpoint = self.endpoint[@klass.path, CGI::escape(search)]
+ @klass.load endpoint, endpoint.get(params)
else
raise ArgumentError, 'invalid search value type'
end
end
def create(attributes = {})
- @klass.load @endpoint, @endpoint.post(attributes).decoded
+ @klass.load endpoint, post(attributes)
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
+ @klass.load endpoint, Tickethub::Response.new(err.response).decoded
end
end
end
\ No newline at end of file