class Cubits::ResourceCollection
Attributes
Public Class Methods
# File lib/cubits/resource_collection.rb, line 7 def initialize(params = {}) @path = params[:path] @resource = params[:resource] @exposed_methods = params[:expose_methods] end
Public Instance Methods
Loads collection of resources
@return [Array<Resource>]
# File lib/cubits/resource_collection.rb, line 36 def all fail NoMethodError, "Resource #{name} does not expose .all" unless exposed_method?(:all) list = [] page_count.times do |i| list += page(i + 1) end list rescue NotFound [] end
By convention collection name for the resource is the last part of the path
# File lib/cubits/resource_collection.rb, line 15 def collection_name path.split('/').last end
Returns number of elements in the collection
# File lib/cubits/resource_collection.rb, line 27 def count pagination.total_count end
Iterates through the collection, yielding the giving block for each resource.
# File lib/cubits/resource_collection.rb, line 84 def each(&_block) return to_enum unless block_given? page_count.times do |i| page(i + 1).each do |r| yield r end end end
@return true if the method is exposed by this resource
# File lib/cubits/resource_collection.rb, line 21 def exposed_method?(method_name) (@exposed_methods || []).include?(method_name) end
Loads resource
@param id [String]
@return nil if resource is not found
# File lib/cubits/resource_collection.rb, line 65 def find(id) fail NoMethodError, "Resource #{name} does not expose .find" unless exposed_method?(:find) resource.new Cubits.connection.get(path_to(id)) rescue NotFound nil end
Returns first element of the collection
# File lib/cubits/resource_collection.rb, line 49 def first first_page.first end
Returns last element of the collection
# File lib/cubits/resource_collection.rb, line 55 def last last_page.last end
# File lib/cubits/resource_collection.rb, line 106 def name "Collection of #{resource.name}" end
Returns API path to resource
# File lib/cubits/resource_collection.rb, line 95 def path_to(resource_or_id = nil) fail ArgumentError, "Resource path is not set for #{self}" unless path if resource_or_id.is_a?(Resource) "#{path}/#{resource_or_id.id}" elsif resource_or_id "#{path}/#{resource_or_id}" else path end end
Reloads collection
@return [self]
# File lib/cubits/resource_collection.rb, line 76 def reload @pagination = nil @pages = nil self end
# File lib/cubits/resource_collection.rb, line 110 def to_s "<#{self.class.name} of #{resource.name}, #{path}>" end
Private Instance Methods
Returns first page of the collection
# File lib/cubits/resource_collection.rb, line 158 def first_page page(1) end
Returns last page of the collection
# File lib/cubits/resource_collection.rb, line 164 def last_page page(page_count) end
Loads i-th page
# File lib/cubits/resource_collection.rb, line 150 def load_page(i) response = Cubits.connection.get(path_to, page: i) @pagination = Hashie::Mash.new(response['pagination']) response[collection_name].map { |r| resource.new r } end
Returns i-th page
# File lib/cubits/resource_collection.rb, line 144 def page(i) pages[i] ||= load_page(i) end
Returns number of pages
# File lib/cubits/resource_collection.rb, line 127 def page_count pagination.page_count end
Returns collection of pages as a Hash: {
<page_number> => [<resource>, ...], <page_number> => [<resource>, ...], ...
}
# File lib/cubits/resource_collection.rb, line 138 def pages @pages ||= {} end
Returns current pagination object. If pagination was not requested yet, does a one page request
# File lib/cubits/resource_collection.rb, line 119 def pagination return @pagination if @pagination first_page @pagination end