Class: P8push::Client
- Inherits:
-
Object
- Object
- P8push::Client
- Defined in:
- lib/p8push/client.rb
Instance Attribute Summary collapse
-
#jwt_uri ⇒ Object
Returns the value of attribute jwt_uri.
Class Method Summary collapse
- .development(private_key: nil, team_id: nil, key_id: nil, timeout: nil) ⇒ Object
- .production(private_key: nil, team_id: nil, key_id: nil, timeout: nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(private_key: nil, team_id: nil, key_id: nil, timeout: nil) ⇒ Client
constructor
A new instance of Client.
- #jwt_http2_post(topic, payload, token) ⇒ Object
- #push(*notifications) ⇒ Object
Constructor Details
#initialize(private_key: nil, team_id: nil, key_id: nil, timeout: nil) ⇒ Client
Returns a new instance of Client.
26 27 28 29 30 31 |
# File 'lib/p8push/client.rb', line 26 def initialize(private_key: nil, team_id: nil, key_id: nil, timeout: nil) @private_key = private_key || File.read(ENV['APN_PRIVATE_KEY']) @team_id = team_id || ENV['APN_TEAM_ID'] @key_id = key_id || ENV['APN_KEY_ID'] @timeout = Float(timeout || ENV['APN_TIMEOUT'] || 2.0) rescue 2.0 end |
Instance Attribute Details
#jwt_uri ⇒ Object
Returns the value of attribute jwt_uri.
11 12 13 |
# File 'lib/p8push/client.rb', line 11 def jwt_uri @jwt_uri end |
Class Method Details
.development(private_key: nil, team_id: nil, key_id: nil, timeout: nil) ⇒ Object
13 14 15 16 17 |
# File 'lib/p8push/client.rb', line 13 def development(private_key: nil, team_id: nil, key_id: nil, timeout: nil) client = self.new(private_key: private_key, team_id: team_id, key_id: key_id, timeout: timeout) client.jwt_uri = APPLE_DEVELOPMENT_JWT_URI client end |
.production(private_key: nil, team_id: nil, key_id: nil, timeout: nil) ⇒ Object
19 20 21 22 23 |
# File 'lib/p8push/client.rb', line 19 def production(private_key: nil, team_id: nil, key_id: nil, timeout: nil) client = self.new(private_key: private_key, team_id: team_id, key_id: key_id, timeout: timeout) client.jwt_uri = APPLE_PRODUCTION_JWT_URI client end |
Instance Method Details
#jwt_http2_post(topic, payload, token) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/p8push/client.rb', line 33 def jwt_http2_post(topic, payload, token) ec_key = OpenSSL::PKey::EC.new(@private_key) jwt_token = JWT.encode({iss: @team_id, iat: Time.now.to_i}, ec_key, 'ES256', {kid: @key_id}) client = NetHttp2::Client.new(@jwt_uri) h = {} h['apns-expiration'] = '0' h['apns-priority'] = '10' h['apns-topic'] = topic h['scheme'] = 'https' h['authorization'] = "bearer #{jwt_token}" h['content-type'] = 'application/json' res = client.call(:post, '/3/device/'+token, body: payload.to_json, timeout: @timeout, headers: h) client.close return nil if res.status.to_i == 200 [res.body, res.status.to_i] end |
#push(*notifications) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/p8push/client.rb', line 52 def push(*notifications) return if notifications.empty? notifications.flatten! notifications.each_with_index do |notification, index| next unless notification.kind_of?(Notification) next if notification.sent? next unless notification.valid? notification.id = index err, status = jwt_http2_post(notification.topic, notification.payload, notification.token) if err == nil notification.mark_as_sent! else puts err notification.apns_error_code = status notification.mark_as_unsent! end end end |