Class: Pushing::Adapters::ApnoticAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/pushing/adapters/apn/apnotic_adapter.rb

Constant Summary collapse

APS_DICTIONARY_KEYS =
%i[
  alert
  badge
  sound
  content_available
  category
  url_args
  mutable_content
].freeze
DEFAULT_ADAPTER_OPTIONS =
{
  size: Process.getrlimit(Process::RLIMIT_NOFILE).first / 8
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apn_settings) ⇒ ApnoticAdapter

Returns a new instance of ApnoticAdapter.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#connection_poolObject (readonly)

Returns the value of attribute connection_pool.



23
24
25
# File 'lib/pushing/adapters/apn/apnotic_adapter.rb', line 23

def connection_pool
  @connection_pool
end

Instance Method Details

#push!(notification) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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