lib/urbanairship/common.rb in urbanairship-3.0.2 vs lib/urbanairship/common.rb in urbanairship-3.1.0

- old
+ new

@@ -1,7 +1,8 @@ require 'urbanairship/loggable' + module Urbanairship # Features mixed in to all classes module Common SERVER = 'go.urbanairship.com' BASE_URL = 'https://go.urbanairship.com/api' @@ -11,12 +12,13 @@ DEVICE_PIN_URL = BASE_URL + '/device_pins/' PUSH_URL = BASE_URL + '/push/' DT_FEEDBACK_URL = BASE_URL + '/device_tokens/feedback/' APID_FEEDBACK_URL = BASE_URL + '/apids/feedback/' SCHEDULES_URL = BASE_URL + '/schedules/' - TAGS_URL = BASE_URL + '/tags/' SEGMENTS_URL = BASE_URL + '/segments/' + NAMED_USER_URL = BASE_URL + '/named_users/' + REPORTS_URL = BASE_URL + '/reports/' # Helper method for required keyword args in Ruby 2.0 that is compatible with 2.1+ # @example # def say(greeting: required('greeting')) # puts greeting @@ -90,16 +92,58 @@ class Response # Parse Response Codes and trigger appropriate actions. def self.check_code(response_code, response) if response_code == 401 - raise Unauthorized, "Client is not authorized to make this request. The authorization credentials are incorrect or missing." + raise Unauthorized, 'Client is not authorized to make this request. The authorization credentials are incorrect or missing.' elsif response_code == 403 - raise Forbidden, "Client is not forbidden from making this request. The application does not have the proper entitlement to access this feature." + raise Forbidden, 'Client is forbidden from making this request. The application does not have the proper entitlement to access this feature.' elsif !((200...300).include?(response_code)) raise AirshipFailure.new.from_response(response) end end end + class PageIterator + include Urbanairship::Common + include Urbanairship::Loggable + include Enumerable + attr_accessor :data_attribute + + def initialize(client: required('client')) + @client = client + @next_page = nil + @data_list = nil + @data_attribute = nil + end + + def load_page + return false unless @next_page + response = @client.send_request( + method: 'GET', + url: @next_page + ) + logger.info("Retrieving data from: #{@next_page}") + check_next_page = response['body']['next_page'] + if check_next_page != @next_page + @next_page = check_next_page + elsif check_next_page + # if check_page = next_page, we have repeats in the response. + # and we don't want to load them + return false + else + @next_page = nil + end + @data_list = response['body'][@data_attribute] + true + end + + def each + while load_page + @data_list.each do | value | + yield value + end + end + end + end end end