module Uploadcare::Entity::Decorator::Paginator

provides pagination methods for things in Uploadcare that paginate, namely [FileList] and [Group]

Requirements:

Public Instance Methods

all() click to toggle source

Load and return all objects in list

@return [Array]

# File lib/uploadcare/entity/decorator/paginator.rb, line 75
def all
  load[:results]
end
each() { |obj| ... } click to toggle source

iterate through pages, starting with current one

@yield [Block]

# File lib/uploadcare/entity/decorator/paginator.rb, line 62
def each
  current_page = self
  while current_page
    current_page.results.each do |obj|
      yield obj
    end
    current_page = current_page.next_page
  end
end
load() click to toggle source

Attempts to load the entire list after offset into results of current object

It's possible to avoid loading objects on previous pages by offsetting them first

# File lib/uploadcare/entity/decorator/paginator.rb, line 46
def load
  return if @entity[:next].nil? || @entity[:results].length == @entity[:total]

  np = self
  until np.next.nil?
    np = np.next_page
    @entity[:results].concat(np.results.map(&:to_h))
  end
  @entity[:next] = nil
  @entity[:per_page] = @entity[:total]
  self
end
meta() click to toggle source

meta data of a pagination object

# File lib/uploadcare/entity/decorator/paginator.rb, line 18
def meta
  Hashie::Mash.new(next: @entity[:next], previous: @entity[:previous],
                   total: @entity[:total], per_page: @entity[:per_page])
end
next_page() click to toggle source

Returns new instance of current object on next page

# File lib/uploadcare/entity/decorator/paginator.rb, line 24
def next_page
  url = @entity[:next]
  return unless url

  query = URI.decode_www_form(URI(url).query).to_h
  query = Hash[query.map { |k, v| [k.to_sym, v] }]
  self.class.list(**query)
end
previous_page() click to toggle source

Returns new instance of current object on previous page

# File lib/uploadcare/entity/decorator/paginator.rb, line 34
def previous_page
  url = @entity[:previous]
  return unless url

  query = URI.decode_www_form(URI(url).query).to_h
  query = Hash[query.map { |k, v| [k.to_sym, v] }]
  self.class.list(**query)
end