Class: BMO::APNS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bmo/apns/client.rb

Overview

APNS Client Class

Defined Under Namespace

Classes: FeedbackTuple

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway_host, gateway_port, feedback_host, feedback_port, options = {}) ⇒ Client

The constructor of the Client object

it will only use a ssl connection if you pass a cert_path option

Parameters:

  • gateway_host (String)
  • gateway_port (Integer)
  • feedback_host (String)
  • feedback_port (Integer)


28
29
30
31
32
33
34
35
36
37
# File 'lib/bmo/apns/client.rb', line 28

def initialize(gateway_host,  gateway_port,
               feedback_host, feedback_port,
               options = {})
  @gateway_host  = gateway_host
  @gateway_port  = gateway_port
  @feedback_host = feedback_host
  @feedback_port = feedback_port
  @cert_path     = options[:cert_path]
  @cert_pass     = options[:cert_pass]
end

Instance Attribute Details

#cert_passObject

APNS Client Class



12
13
14
15
16
17
18
19
20
21
22
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
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
81
82
# File 'lib/bmo/apns/client.rb', line 12

class Client
  attr_reader :cert_path,     :cert_pass,
              :gateway_host,  :gateway_port,
              :feedback_host, :feedback_port

  # The constructor of the Client object
  #   it will only use a ssl connection if you pass a cert_path option
  #
  # @param gateway_host  [String]
  # @param gateway_port  [Integer]
  # @param feedback_host [String]
  # @param feedback_port [Integer]
  #
  # @options options [String] :cert_pass
  # @options options [String] :cert_path path to certificate file
  #
  def initialize(gateway_host,  gateway_port,
                 feedback_host, feedback_port,
                 options = {})
    @gateway_host  = gateway_host
    @gateway_port  = gateway_port
    @feedback_host = feedback_host
    @feedback_port = feedback_port
    @cert_path     = options[:cert_path]
    @cert_pass     = options[:cert_pass]
  end

  # @param notification [Notification] the notification to send to Apple
  #
  def send_notification(notification)
    connection = APNS::Connection.new(@gateway_host, @gateway_port,
                                      @cert_path,    @cert_pass)
    connection.connect do |socket|
      socket.write(notification.to_package)
    end
  end

  # Get the Feedback from Apple
  #
  # @return <Array[FeedbackTuple]> A feedback tuple contains the time
  #   when Apple determined that the app no longer exists on the device,
  #   and a the token of device token
  def feedback
    connection = APNS::Connection.new(@feedback_host, @feedback_port,
                                      @cert_path,     @cert_pass)
    connection.connect do |socket|
      feedback_tuples = []
      while (data = socket.read(38))
        tuple = data.unpack('N1n1H*')
        feedback_tuples << FeedbackTuple.new(tuple[0], tuple[2])
      end
      feedback_tuples
    end
  end

  private

  # Handle the Feedback Object
  #
  # @!attribute time [Time] Time when the device was notified
  #   but didn't have the app installed
  # @!attribute token [String] The Token of the device notified wrongly
  class FeedbackTuple
    attr_reader :time, :token

    def initialize(timestamp, token)
      @time  = Time.at(timestamp)
      @token = token
    end
  end
end

#cert_pathObject

APNS Client Class



12
13
14
15
16
17
18
19
20
21
22
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
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
81
82
# File 'lib/bmo/apns/client.rb', line 12

class Client
  attr_reader :cert_path,     :cert_pass,
              :gateway_host,  :gateway_port,
              :feedback_host, :feedback_port

  # The constructor of the Client object
  #   it will only use a ssl connection if you pass a cert_path option
  #
  # @param gateway_host  [String]
  # @param gateway_port  [Integer]
  # @param feedback_host [String]
  # @param feedback_port [Integer]
  #
  # @options options [String] :cert_pass
  # @options options [String] :cert_path path to certificate file
  #
  def initialize(gateway_host,  gateway_port,
                 feedback_host, feedback_port,
                 options = {})
    @gateway_host  = gateway_host
    @gateway_port  = gateway_port
    @feedback_host = feedback_host
    @feedback_port = feedback_port
    @cert_path     = options[:cert_path]
    @cert_pass     = options[:cert_pass]
  end

  # @param notification [Notification] the notification to send to Apple
  #
  def send_notification(notification)
    connection = APNS::Connection.new(@gateway_host, @gateway_port,
                                      @cert_path,    @cert_pass)
    connection.connect do |socket|
      socket.write(notification.to_package)
    end
  end

  # Get the Feedback from Apple
  #
  # @return <Array[FeedbackTuple]> A feedback tuple contains the time
  #   when Apple determined that the app no longer exists on the device,
  #   and a the token of device token
  def feedback
    connection = APNS::Connection.new(@feedback_host, @feedback_port,
                                      @cert_path,     @cert_pass)
    connection.connect do |socket|
      feedback_tuples = []
      while (data = socket.read(38))
        tuple = data.unpack('N1n1H*')
        feedback_tuples << FeedbackTuple.new(tuple[0], tuple[2])
      end
      feedback_tuples
    end
  end

  private

  # Handle the Feedback Object
  #
  # @!attribute time [Time] Time when the device was notified
  #   but didn't have the app installed
  # @!attribute token [String] The Token of the device notified wrongly
  class FeedbackTuple
    attr_reader :time, :token

    def initialize(timestamp, token)
      @time  = Time.at(timestamp)
      @token = token
    end
  end
end

#feedback_hostObject

APNS Client Class



12
13
14
15
16
17
18
19
20
21
22
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
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
81
82
# File 'lib/bmo/apns/client.rb', line 12

class Client
  attr_reader :cert_path,     :cert_pass,
              :gateway_host,  :gateway_port,
              :feedback_host, :feedback_port

  # The constructor of the Client object
  #   it will only use a ssl connection if you pass a cert_path option
  #
  # @param gateway_host  [String]
  # @param gateway_port  [Integer]
  # @param feedback_host [String]
  # @param feedback_port [Integer]
  #
  # @options options [String] :cert_pass
  # @options options [String] :cert_path path to certificate file
  #
  def initialize(gateway_host,  gateway_port,
                 feedback_host, feedback_port,
                 options = {})
    @gateway_host  = gateway_host
    @gateway_port  = gateway_port
    @feedback_host = feedback_host
    @feedback_port = feedback_port
    @cert_path     = options[:cert_path]
    @cert_pass     = options[:cert_pass]
  end

  # @param notification [Notification] the notification to send to Apple
  #
  def send_notification(notification)
    connection = APNS::Connection.new(@gateway_host, @gateway_port,
                                      @cert_path,    @cert_pass)
    connection.connect do |socket|
      socket.write(notification.to_package)
    end
  end

  # Get the Feedback from Apple
  #
  # @return <Array[FeedbackTuple]> A feedback tuple contains the time
  #   when Apple determined that the app no longer exists on the device,
  #   and a the token of device token
  def feedback
    connection = APNS::Connection.new(@feedback_host, @feedback_port,
                                      @cert_path,     @cert_pass)
    connection.connect do |socket|
      feedback_tuples = []
      while (data = socket.read(38))
        tuple = data.unpack('N1n1H*')
        feedback_tuples << FeedbackTuple.new(tuple[0], tuple[2])
      end
      feedback_tuples
    end
  end

  private

  # Handle the Feedback Object
  #
  # @!attribute time [Time] Time when the device was notified
  #   but didn't have the app installed
  # @!attribute token [String] The Token of the device notified wrongly
  class FeedbackTuple
    attr_reader :time, :token

    def initialize(timestamp, token)
      @time  = Time.at(timestamp)
      @token = token
    end
  end
end

#feedback_portObject

APNS Client Class



12
13
14
15
16
17
18
19
20
21
22
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
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
81
82
# File 'lib/bmo/apns/client.rb', line 12

class Client
  attr_reader :cert_path,     :cert_pass,
              :gateway_host,  :gateway_port,
              :feedback_host, :feedback_port

  # The constructor of the Client object
  #   it will only use a ssl connection if you pass a cert_path option
  #
  # @param gateway_host  [String]
  # @param gateway_port  [Integer]
  # @param feedback_host [String]
  # @param feedback_port [Integer]
  #
  # @options options [String] :cert_pass
  # @options options [String] :cert_path path to certificate file
  #
  def initialize(gateway_host,  gateway_port,
                 feedback_host, feedback_port,
                 options = {})
    @gateway_host  = gateway_host
    @gateway_port  = gateway_port
    @feedback_host = feedback_host
    @feedback_port = feedback_port
    @cert_path     = options[:cert_path]
    @cert_pass     = options[:cert_pass]
  end

  # @param notification [Notification] the notification to send to Apple
  #
  def send_notification(notification)
    connection = APNS::Connection.new(@gateway_host, @gateway_port,
                                      @cert_path,    @cert_pass)
    connection.connect do |socket|
      socket.write(notification.to_package)
    end
  end

  # Get the Feedback from Apple
  #
  # @return <Array[FeedbackTuple]> A feedback tuple contains the time
  #   when Apple determined that the app no longer exists on the device,
  #   and a the token of device token
  def feedback
    connection = APNS::Connection.new(@feedback_host, @feedback_port,
                                      @cert_path,     @cert_pass)
    connection.connect do |socket|
      feedback_tuples = []
      while (data = socket.read(38))
        tuple = data.unpack('N1n1H*')
        feedback_tuples << FeedbackTuple.new(tuple[0], tuple[2])
      end
      feedback_tuples
    end
  end

  private

  # Handle the Feedback Object
  #
  # @!attribute time [Time] Time when the device was notified
  #   but didn't have the app installed
  # @!attribute token [String] The Token of the device notified wrongly
  class FeedbackTuple
    attr_reader :time, :token

    def initialize(timestamp, token)
      @time  = Time.at(timestamp)
      @token = token
    end
  end
end

#gateway_hostObject

APNS Client Class



12
13
14
15
16
17
18
19
20
21
22
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
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
81
82
# File 'lib/bmo/apns/client.rb', line 12

class Client
  attr_reader :cert_path,     :cert_pass,
              :gateway_host,  :gateway_port,
              :feedback_host, :feedback_port

  # The constructor of the Client object
  #   it will only use a ssl connection if you pass a cert_path option
  #
  # @param gateway_host  [String]
  # @param gateway_port  [Integer]
  # @param feedback_host [String]
  # @param feedback_port [Integer]
  #
  # @options options [String] :cert_pass
  # @options options [String] :cert_path path to certificate file
  #
  def initialize(gateway_host,  gateway_port,
                 feedback_host, feedback_port,
                 options = {})
    @gateway_host  = gateway_host
    @gateway_port  = gateway_port
    @feedback_host = feedback_host
    @feedback_port = feedback_port
    @cert_path     = options[:cert_path]
    @cert_pass     = options[:cert_pass]
  end

  # @param notification [Notification] the notification to send to Apple
  #
  def send_notification(notification)
    connection = APNS::Connection.new(@gateway_host, @gateway_port,
                                      @cert_path,    @cert_pass)
    connection.connect do |socket|
      socket.write(notification.to_package)
    end
  end

  # Get the Feedback from Apple
  #
  # @return <Array[FeedbackTuple]> A feedback tuple contains the time
  #   when Apple determined that the app no longer exists on the device,
  #   and a the token of device token
  def feedback
    connection = APNS::Connection.new(@feedback_host, @feedback_port,
                                      @cert_path,     @cert_pass)
    connection.connect do |socket|
      feedback_tuples = []
      while (data = socket.read(38))
        tuple = data.unpack('N1n1H*')
        feedback_tuples << FeedbackTuple.new(tuple[0], tuple[2])
      end
      feedback_tuples
    end
  end

  private

  # Handle the Feedback Object
  #
  # @!attribute time [Time] Time when the device was notified
  #   but didn't have the app installed
  # @!attribute token [String] The Token of the device notified wrongly
  class FeedbackTuple
    attr_reader :time, :token

    def initialize(timestamp, token)
      @time  = Time.at(timestamp)
      @token = token
    end
  end
end

#gateway_portObject

APNS Client Class



12
13
14
15
16
17
18
19
20
21
22
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
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
81
82
# File 'lib/bmo/apns/client.rb', line 12

class Client
  attr_reader :cert_path,     :cert_pass,
              :gateway_host,  :gateway_port,
              :feedback_host, :feedback_port

  # The constructor of the Client object
  #   it will only use a ssl connection if you pass a cert_path option
  #
  # @param gateway_host  [String]
  # @param gateway_port  [Integer]
  # @param feedback_host [String]
  # @param feedback_port [Integer]
  #
  # @options options [String] :cert_pass
  # @options options [String] :cert_path path to certificate file
  #
  def initialize(gateway_host,  gateway_port,
                 feedback_host, feedback_port,
                 options = {})
    @gateway_host  = gateway_host
    @gateway_port  = gateway_port
    @feedback_host = feedback_host
    @feedback_port = feedback_port
    @cert_path     = options[:cert_path]
    @cert_pass     = options[:cert_pass]
  end

  # @param notification [Notification] the notification to send to Apple
  #
  def send_notification(notification)
    connection = APNS::Connection.new(@gateway_host, @gateway_port,
                                      @cert_path,    @cert_pass)
    connection.connect do |socket|
      socket.write(notification.to_package)
    end
  end

  # Get the Feedback from Apple
  #
  # @return <Array[FeedbackTuple]> A feedback tuple contains the time
  #   when Apple determined that the app no longer exists on the device,
  #   and a the token of device token
  def feedback
    connection = APNS::Connection.new(@feedback_host, @feedback_port,
                                      @cert_path,     @cert_pass)
    connection.connect do |socket|
      feedback_tuples = []
      while (data = socket.read(38))
        tuple = data.unpack('N1n1H*')
        feedback_tuples << FeedbackTuple.new(tuple[0], tuple[2])
      end
      feedback_tuples
    end
  end

  private

  # Handle the Feedback Object
  #
  # @!attribute time [Time] Time when the device was notified
  #   but didn't have the app installed
  # @!attribute token [String] The Token of the device notified wrongly
  class FeedbackTuple
    attr_reader :time, :token

    def initialize(timestamp, token)
      @time  = Time.at(timestamp)
      @token = token
    end
  end
end

Instance Method Details

#feedbackArray[FeedbackTuple]

Get the Feedback from Apple

Returns:

  • (Array[FeedbackTuple])

    A feedback tuple contains the time when Apple determined that the app no longer exists on the device, and a the token of device token



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bmo/apns/client.rb', line 54

def feedback
  connection = APNS::Connection.new(@feedback_host, @feedback_port,
                                    @cert_path,     @cert_pass)
  connection.connect do |socket|
    feedback_tuples = []
    while (data = socket.read(38))
      tuple = data.unpack('N1n1H*')
      feedback_tuples << FeedbackTuple.new(tuple[0], tuple[2])
    end
    feedback_tuples
  end
end

#send_notification(notification) ⇒ Object

Parameters:

  • notification (Notification)

    the notification to send to Apple



41
42
43
44
45
46
47
# File 'lib/bmo/apns/client.rb', line 41

def send_notification(notification)
  connection = APNS::Connection.new(@gateway_host, @gateway_port,
                                    @cert_path,    @cert_pass)
  connection.connect do |socket|
    socket.write(notification.to_package)
  end
end