Class: Pushlet::APNSGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/pushlet/apns_gateway.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o = {}) ⇒ APNSGateway

Returns a new instance of APNSGateway.



6
7
8
9
10
11
# File 'lib/pushlet/apns_gateway.rb', line 6

def initialize(o={})
  @mode = o[:mode]
  @pass = o[:p12_pass]
  @pem = o[:pem] || self.class.p12_to_pem_text(o[:p12], o[:p12_pass])
  create_pusher
end

Class Method Details

.p12_to_pem_text(p12, p12_pass = '') ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pushlet/apns_gateway.rb', line 70

def self.p12_to_pem_text(p12, p12_pass='')
  p12_pass = '' if p12_pass.nil?

  if Running.jruby?
    tf_p12 = Tempfile.new 'tf_p12'
    tf_p12.write p12
    tf_p12.rewind
    tf_p12.close

    tf_pem = Tempfile.new 'tf_pem'
    tf_pem.close

    cmd = Escape.shell_command(['openssl', 'pkcs12', '-in', tf_p12.path, '-out', tf_pem.path,
                                '-nodes', '-clcerts', '-password', 'pass:'+p12_pass]).to_s + ' &> /dev/null'

    begin
      result = system cmd
      raise CommandFailError if result.nil? || result == false
      pem = File.read tf_pem.path
    ensure
      tf_pem.unlink
      tf_p12.unlink
    end

    pem
  else
    pkcs12 = OpenSSL::PKCS12.new p12, p12_pass
    pkcs12.certificate.to_s + pkcs12.key.to_s
  end
end

Instance Method Details

#create_pusherObject



31
32
33
34
35
36
37
38
39
# File 'lib/pushlet/apns_gateway.rb', line 31

def create_pusher
  @pusher = Grocer.pusher(
    certificate: StringIO.new(@pem),
    passphrase:  @pass,
    gateway:     (@mode == "development" ? gateway_sandbox : gateway_production),
    port:        port,
    retries:     retries
  )
end

#feedbackObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pushlet/apns_gateway.rb', line 13

def feedback
  feedback = Grocer.feedback(
    certificate: StringIO.new(@pem),
    passphrase:  @pass,
    gateway:     (@mode == "development" ? gateway_sandbox : gateway_production),
    port:        feedback_port,
    retries:     retries
  )

  failed_delivery_attempts = []

  feedback.each do |fda|
    failed_delivery_attempts << fda
  end

  failed_delivery_attempts
end

#feedback_portObject



44
# File 'lib/pushlet/apns_gateway.rb', line 44

def feedback_port;      2196 end

#gateway_productionObject



42
# File 'lib/pushlet/apns_gateway.rb', line 42

def gateway_production; 'gateway.push.apple.com' end

#gateway_sandboxObject



41
# File 'lib/pushlet/apns_gateway.rb', line 41

def gateway_sandbox;    'gateway.sandbox.push.apple.com' end

#portObject



43
# File 'lib/pushlet/apns_gateway.rb', line 43

def port;               2195 end

#push(args = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pushlet/apns_gateway.rb', line 47

def push(args={})
  if args[:identifier]
    identifier = args[:identifier]
  else
    # Seriously Apple, a 4 byte identifier? Really guys?
    identifier = SecureRandom.random_bytes(4)
  end

  payload = {}

  [:device_token, :alert, :badge, :sound, :expiry, :identifier].each do |a|
    if a == :expiry
      payload[a] = args[a] || Time.now + 60*10
    else
      payload[a] = args[a] if args[a]
    end
  end

  notification = Grocer::Notification.new payload

  @pusher.push(notification)
end

#retriesObject



45
# File 'lib/pushlet/apns_gateway.rb', line 45

def retries;            3 end