class Urbanairship::Common::PageIterator

Attributes

data_attribute[RW]

Public Class Methods

new(client: required('client')) click to toggle source
# File lib/urbanairship/common.rb, line 160
def initialize(client: required('client'))
  @client = client
  @count = 0
  @data_attribute = nil
  @data_list = nil
  @next_page_path = nil
  @next_page_url = nil
end

Public Instance Methods

count() click to toggle source
# File lib/urbanairship/common.rb, line 180
def count
  @count
end
each() { |value| ... } click to toggle source
# File lib/urbanairship/common.rb, line 169
def each
  while @next_page_path || @next_page_url
    load_page

    @data_list.each do |value|
      @count += 1
      yield value
    end
  end
end

Private Instance Methods

extract_next_page_url(response) click to toggle source
# File lib/urbanairship/common.rb, line 200
def extract_next_page_url(response)
  response['body']['next_page']
end
get_new_data(response) click to toggle source
# File lib/urbanairship/common.rb, line 204
def get_new_data(response)
  potential_next_page_url = extract_next_page_url(response)

  # if potential_next_page_url is the same as the current page, we have
  # repeats in the response and we don't want to load them
  return [] if potential_next_page_url && get_next_page_url(response).nil?

  response['body'][@data_attribute]
end
get_next_page_url(response) click to toggle source
# File lib/urbanairship/common.rb, line 214
def get_next_page_url(response)
  potential_next_page_url = extract_next_page_url(response)
  return nil if potential_next_page_url.nil?

  # if potential_next_page_url is the same as the current page, we have
  # repeats in the response and we don't want to check the next pages
  return potential_next_page_url if @next_page_url && potential_next_page_url != @next_page_url
  return potential_next_page_url if @next_page_path && !potential_next_page_url.end_with?(@next_page_path)
  nil
end
load_page() click to toggle source
# File lib/urbanairship/common.rb, line 186
def load_page
  logger.info("Retrieving data from: #{@next_page_url || @next_page_path}")
  params = {
      method: 'GET',
      path: @next_page_path,
      url: @next_page_url
    }.select { |k, v| !v.nil? }
  response = @client.send_request(params)

  @data_list = get_new_data(response)
  @next_page_url = get_next_page_url(response)
  @next_page_path = nil
end