class DropletKit::PaginatedResource
Constants
- PER_PAGE
Attributes
action[R]
collection[R]
resource[R]
total[RW]
Public Class Methods
new(action, resource, *args)
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 10 def initialize(action, resource, *args) @current_page = 0 @total = nil @action = action @resource = resource @collection = [] @args = args @options = args.last.kind_of?(Hash) ? args.last : {} end
Public Instance Methods
==(other)
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 57 def ==(other) each_with_index.each.all? {|object, index| object == other[index] } end
[](index)
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 24 def [](index) @collection[index] end
each(start = 0) { |element| ... }
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 28 def each(start = 0, &block) # Start off with the first page if we have no idea of anything yet fetch_next_page if total.nil? return to_enum(:each, start) unless block_given? Array(@collection[start..-1]).each do |element| yield(element) end unless last? start = [@collection.size, start].max fetch_next_page each(start, &block) end self end
last?()
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 46 def last? return true if self.total.nil? @current_page == total_pages || self.total.zero? end
per_page()
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 20 def per_page @options[:per_page] || PER_PAGE end
total_pages()
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 51 def total_pages return nil if self.total.nil? (self.total.to_f / per_page.to_f).ceil end
Private Instance Methods
fetch_next_page()
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 63 def fetch_next_page @current_page += 1 retrieve(@current_page) end
retrieve(page, per_page = self.per_page)
click to toggle source
# File lib/droplet_kit/paginated_resource.rb, line 68 def retrieve(page, per_page = self.per_page) invoker = ResourceKit::ActionInvoker.new(action, resource, *@args) invoker.options[:per_page] ||= per_page invoker.options[:page] = page @collection += invoker.handle_response if total.nil? meta = MetaInformation.extract_single(invoker.response.body, :read) self.total = meta.total.to_i end end