class Pushr::Daemon::FcmSupport::ResponseHandler

Attributes

data[RW]
response[RW]
retry_count[RW]

Public Class Methods

new(response, data, retry_count) click to toggle source
# File lib/pushr/daemon/fcm_support/response_handler.rb, line 6
def initialize(response, data, retry_count)
  @response = response
  @data = data
  @retry_count = retry_count
end

Public Instance Methods

handle() click to toggle source
# File lib/pushr/daemon/fcm_support/response_handler.rb, line 12
def handle
  case @response.code.to_i
  when 200
    handle_success_response
  when 400
    # Pushr::Daemon.logger.error("[#{@name}] JSON formatting exception received.")
    Pushr::Daemon::DeliveryError.new(@response.code, @data, 'JSON formatting exception', 'FCM', false)
  when 401
    # Pushr::Daemon.logger.error("[#{@name}] Authentication exception received.")
    Pushr::Daemon::DeliveryError.new(@response.code, @data, 'Authentication exception', 'FCM', false)
  when 500..599
    # internal error FCM server || service unavailable: exponential back-off
    handle_error_5xx_response()
  else
    # Pushr::Daemon.logger.error("[#{@name}] Unknown error: #{@response.code} #{response.message}")
    Pushr::Daemon::DeliveryError.new(@response.code, @data, "Unknown error: #{response.message}", 'FCM', false)
  end
end
handle_error_5xx_response() click to toggle source

sleep if there is a Retry-After header

# File lib/pushr/daemon/fcm_support/response_handler.rb, line 32
def handle_error_5xx_response
  value = @response.header['Retry-After']
  if value && value.to_i.positive?
    sleep value.to_i # Retry-After: 3600
  elsif value && Date.rfc2822(value) # Retry-After: Fri, 31 Dec 1999 23:59:59 GMT
    sleep Time.now.utc - Date.rfc2822(value).to_time.utc
  else
    sleep 2**@retry_count
  end
end
handle_success_response() click to toggle source
# File lib/pushr/daemon/fcm_support/response_handler.rb, line 43
def handle_success_response()
  puts @response.body.inspect
end