module Mailchimp::Collection::Paging

Constants

DEFAULT_PAGE_SIZE

Public Instance Methods

fetch_options() click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 13
def fetch_options
  links_delim = self.class::DATA_KEY.empty? ? '' : '._links,'

  {
    'exclude_fields' => "#{self.class::DATA_KEY}#{links_delim}_links",
    'offset' => offset,
    'count' => page_size
  }
end
find_each() { |child| ... } click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 55
def find_each
  find_in_pages do |p|
    p.each { |child| yield child }
  end
end
find_in_pages(options = {}) { |page_children| ... } click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 39
def find_in_pages(options = {})
  parse_options(options)

  loop do
    yield page_children
    @offset += page_size

    if offset > count
      @offset = 0
      break
    end

    invalidate_current_page
  end
end
invalidate_current_page() click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 70
def invalidate_current_page
  @page = @page_array = @page_children = nil
end
offset() click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 31
def offset
  @offset ||= 0
end
page() click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 8
def page
  return @page if @page
  @page = @client.get(path, fetch_options)
end
page_array() click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 23
def page_array
  @page_array ||= page[self.class::DATA_KEY]
end
page_children() click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 27
def page_children
  @page_children ||= page_array.map { |d| self.class::CHILD_CLASS.new @client, d, path }
end
page_size() click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 35
def page_size
  @page_size ||= DEFAULT_PAGE_SIZE
end
parse_options(options = {}) click to toggle source
# File lib/mailchimp_api_v3/collection/paging.rb, line 61
def parse_options(options = {})
  if options
    @offset = options['start'] if options.key? 'start'
    @page_size = options['page_size'] if options.key? 'page_size'
  end

  invalidate_current_page
end