Class: ApnsProviderApi::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



34
35
36
37
38
39
40
# File 'lib/apns_provider_api/client.rb', line 34

def initialize
  @gateway_uri = ENV['APN_GATEWAY_URI']
  @certificate = File.read(ENV['APN_CERTIFICATE']) if ENV['APN_CERTIFICATE']
  @passphrase = ENV['APN_CERTIFICATE_PASSPHRASE']
  @pid = Process.pid
  @failed_notifications, @failed_streams, @notifications = [], [], []
end

Instance Attribute Details

#certificateObject

Returns the value of attribute certificate.



12
13
14
# File 'lib/apns_provider_api/client.rb', line 12

def certificate
  @certificate
end

#feedback_uriObject

Returns the value of attribute feedback_uri.



12
13
14
# File 'lib/apns_provider_api/client.rb', line 12

def feedback_uri
  @feedback_uri
end

#gateway_uriObject

Returns the value of attribute gateway_uri.



12
13
14
# File 'lib/apns_provider_api/client.rb', line 12

def gateway_uri
  @gateway_uri
end

#passphraseObject

Returns the value of attribute passphrase.



12
13
14
# File 'lib/apns_provider_api/client.rb', line 12

def passphrase
  @passphrase
end

#split_sizeObject

Returns the value of attribute split_size.



12
13
14
# File 'lib/apns_provider_api/client.rb', line 12

def split_size
  @split_size
end

#timeoutObject

Returns the value of attribute timeout.



12
13
14
# File 'lib/apns_provider_api/client.rb', line 12

def timeout
  @timeout
end

Class Method Details

.developmentObject



15
16
17
18
19
# File 'lib/apns_provider_api/client.rb', line 15

def development
  client = self.new
  client.gateway_uri = APPLE_DEVELOPMENT_GATEWAY_URI
  client
end

.mockObject



27
28
29
30
31
# File 'lib/apns_provider_api/client.rb', line 27

def mock
  client = self.new
  client.gateway_uri = "apn://127.0.0.1:2195"
  client
end

.productionObject



21
22
23
24
25
# File 'lib/apns_provider_api/client.rb', line 21

def production
  client = self.new
  client.gateway_uri = APPLE_PRODUCTION_GATEWAY_URI
  client
end

Instance Method Details

#enqueue(notifications) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/apns_provider_api/client.rb', line 42

def enqueue(notifications)
 @split_size ||= 500
  notifications.each_slice(@split_size) do |group|
    push(group)
  end
  @failed_notifications
end

#increase_counterObject



125
126
127
# File 'lib/apns_provider_api/client.rb', line 125

def increase_counter
  @counter += 1
end

#push(notifications) ⇒ 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/apns_provider_api/client.rb', line 50

def push(notifications)
  return if notifications.empty?

  @notifications = notifications.flatten
  @counter = 0

  head = {
    ':scheme' => 'https',
    ':method' => 'POST'
    # ':apns-expiration' => 0 #do not store for sending late
    #'content-length' => notification.message.size
  }

  Connection.open(@gateway_uri, @certificate, @passphrase) do |connection|
    ssl_sock = connection.ssl
    # events(connection.http2client, ssl_sock)

    @notifications.each_with_index do |notification, index|
      connection.open?
      head[':path'] = "/3/device/#{notification.token}"
      head[':apns-id'] = notification.uuid

      stream = connection.new_stream

      # because apple is not returning the uuid when push fails,
      # i'm replacing uuid to stream id
      notification.uuid = stream.id

      stream.on(:headers) do |h|
       # puts "headers: #{h} - stream_id: #{stream.id}"
        read_headers(Hash[*h.flatten], stream)
      end
      stream.on(:data) do |d|
        # puts "data: #{d}"
        # puts "#{JSON.parse(d)} - #{stream.id.to_s}"
        read_body(JSON.parse(d), stream)
      end
      stream.headers(head, end_stream: false)
      stream.data(notification.payload.to_json)
    end

    # keep_reading = true

    while !ssl_sock.closed? && !ssl_sock.eof?
      data = ssl_sock.read_nonblock(1024)
      # puts "Received bytes: #{data.unpack("H*").first}"
      begin
        connection.http2client << data
        return if @counter == @notifications.size
      rescue => e
        puts "Exception: #{e}, #{e.message} - closing socket."
        ssl_sock.close
      end
    end
  end
end

#read_body(response, stream) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/apns_provider_api/client.rb', line 117

def read_body(response, stream)
  increase_counter
  return unless @failed_streams.include?(stream)
  notification = @notifications.select{|n| n.uuid == stream.id}[0]
  notification.error_message = response['reason']
  @failed_notifications << notification
end

#read_headers(headers, stream) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/apns_provider_api/client.rb', line 107

def read_headers(headers, stream)
  # puts headers[':status']
  if headers[':status'].to_i == 200
    # when notification succeeds, we wont receive body
    increase_counter
  else
    @failed_streams << stream
  end
end