Class: Lorkhan::Connection
- Inherits:
-
Object
- Object
- Lorkhan::Connection
- Defined in:
- lib/lorkhan/connection.rb
Overview
The Connection class manages the HTTP/2 connection to Apple’s servers
token: (Lorkhan::ProviderToken) Used to authenticate with Apple’s servers production: (Boolean) Used to determine which endpoint to establish the connection timeout: (Int) Number of seconds to wait before timing out opening the connection
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the connection to apple.
-
#initialize(token:, production: true, timeout: 30) ⇒ Connection
constructor
A new instance of Connection.
-
#push(notification) ⇒ Object
Deliver a Lorkhan::Notification to Apple’s servers.
-
#refresh_token ⇒ Object
Closes the connection and resets the authentication token.
Constructor Details
#initialize(token:, production: true, timeout: 30) ⇒ Connection
Returns a new instance of Connection.
19 20 21 22 23 24 25 26 |
# File 'lib/lorkhan/connection.rb', line 19 def initialize(token:, production: true, timeout: 30) raise ArgumentError, 'Token must be a Lorkhan::ProviderToken' unless token.is_a?(Lorkhan::ProviderToken) @host = production ? APNS_PRODUCTION_HOST : APNS_DEVELOPMENT_HOST @timeout = timeout @token = token @client = NetHttp2::Client.new(url, connect_timeout: @connect_timeout) end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
17 18 19 |
# File 'lib/lorkhan/connection.rb', line 17 def host @host end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
17 18 19 |
# File 'lib/lorkhan/connection.rb', line 17 def token @token end |
Instance Method Details
#close ⇒ Object
Closes the connection to apple.
31 32 33 |
# File 'lib/lorkhan/connection.rb', line 31 def close @client.close end |
#push(notification) ⇒ Object
Deliver a Lorkhan::Notification to Apple’s servers.
If the connection is not open, this will attempt to establish the connection.
notification: (Lorkhan::Notification) The notification to deliver.
return: The HTTP response from Apple
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/lorkhan/connection.rb', line 54 def push(notification) check_token_should_refresh request = Request.new(notification) request.validate! headers = request.headers headers['authorization'] = "bearer #{auth_token}" raw_response = @client.call(:post, request.path, body: request.body, headers: headers, timeout: 5) raise Errors::TimeoutError if raw_response.nil? response = Response.new(raw_response) handle_http_error(response) unless response.ok? response end |
#refresh_token ⇒ Object
Closes the connection and resets the authentication token.
This should happen automatically 55 minutes after the original token was generated.
40 41 42 43 |
# File 'lib/lorkhan/connection.rb', line 40 def refresh_token @auth_token = nil close end |