class Pushing::Adapters::ApnoticAdapter

Constants

APS_DICTIONARY_KEYS
DEFAULT_ADAPTER_OPTIONS

Attributes

connection_pool[R]

Public Class Methods

new(apn_settings) click to toggle source
# File lib/pushing/adapters/apn/apnotic_adapter.rb, line 25
def initialize(apn_settings)
  options = case apn_settings.connection_scheme.to_sym
            when :token
              {
                auth_method: :token,
                cert_path: apn_settings.certificate_path,
                key_id: apn_settings.key_id,
                team_id: apn_settings.team_id
              }
            when :certificate
              {
                cert_path: apn_settings.certificate_path,
                cert_pass: apn_settings.certificate_password
              }
            else
              raise "Unknown connection scheme #{apn_settings.connection_scheme.inspect}. " \
                    "The connection scheme should either be :token or :certificate."
            end

  @connection_pool = {
    development: Apnotic::ConnectionPool.development(options, DEFAULT_ADAPTER_OPTIONS),
    production: Apnotic::ConnectionPool.new(options, DEFAULT_ADAPTER_OPTIONS),
  }
end

Public Instance Methods

push!(notification) click to toggle source
# File lib/pushing/adapters/apn/apnotic_adapter.rb, line 50
def push!(notification)
  message = Apnotic::Notification.new(notification.device_token)
  json    = notification.payload.dup

  if aps = json.delete(:aps)
    APS_DICTIONARY_KEYS.each {|key| message.instance_variable_set(:"@#{key}", aps[key]) }
  end

  message.custom_payload = json

  message.apns_id          = notification.headers[:'apns-id'] || message.apns_id
  message.expiration       = notification.headers[:'apns-expiration'].to_i
  message.priority         = notification.headers[:'apns-priority']
  message.topic            = notification.headers[:'apns-topic']
  message.apns_collapse_id = notification.headers[:'apns-collapse-id']

  response = connection_pool[notification.environment].with {|connection| connection.push(message) }

  if !response
    raise "Timeout sending a push notification"
  elsif response.status != '200'
    raise response.body.to_s
  end

  ApnResponse.new(response)
rescue => cause
  response = response ? ApnResponse.new(response) : nil
  error    = Pushing::ApnDeliveryError.new("Error while trying to send push notification: #{cause.message}", response, notification)

  raise error, error.message, cause.backtrace
end