Class: Pushing::Adapters::LowdownAdapter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apn_settings) ⇒ LowdownAdapter

Returns a new instance of LowdownAdapter.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pushing/adapters/apn/lowdown_adapter.rb', line 11

def initialize(apn_settings)
  # Don't load lowdown earlier as it may load Celluloid (and start it)
  # before daemonizing the workers spun up by a gem (e,g, delayed_job).
  require 'lowdown' unless defined?(Lodwown)

  cert = File.read(apn_settings.certificate_path)
  @clients = {
    development: Lowdown::Client.production(false, certificate: cert, keep_alive: true),
    production: Lowdown::Client.production(true, certificate: cert, keep_alive: true)
  }
end

Instance Attribute Details

#clientsObject (readonly)

Returns the value of attribute clients.



9
10
11
# File 'lib/pushing/adapters/apn/lowdown_adapter.rb', line 9

def clients
  @clients
end

Instance Method Details

#push!(notification) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pushing/adapters/apn/lowdown_adapter.rb', line 23

def push!(notification)
  if notification.headers[:'apns-id']
    warn("The lowdown gem does not allow for overriding `apns_id'.")
  end

  if notification.headers[:'apns-collapse-id']
    warn("The lowdown gem does not allow for overriding `apns-collapse-id'.")
  end

  lowdown_notification = Lowdown::Notification.new(token: notification.device_token)
  lowdown_notification.payload = notification.payload

  lowdown_notification.expiration = notification.headers[:'apns-expiration'].to_i if notification.headers[:'apns-expiration']
  lowdown_notification.priority   = notification.headers[:'apns-priority']
  lowdown_notification.topic      = notification.headers[:'apns-topic']

  response = nil
  clients[notification.environment].group do |group|
    group.send_notification(lowdown_notification) do |_response|
      response = _response
    end
  end

  raise response.raw_body if !response.success?
  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