class Lorkhan::Connection

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

Attributes

host[R]
token[R]

Public Class Methods

new(token:, production: true, timeout: 30) click to toggle source
# 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

Public Instance Methods

close() click to toggle source

Closes the connection to apple.

# File lib/lorkhan/connection.rb, line 31
def close
  @client.close
end
push(notification) click to toggle source

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

# 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() click to toggle source

Closes the connection and resets the authentication token.

This should happen automatically 55 minutes after the original token was generated.

# File lib/lorkhan/connection.rb, line 40
def refresh_token
  @auth_token = nil
  close
end

Private Instance Methods

auth_token() click to toggle source
# File lib/lorkhan/connection.rb, line 70
def auth_token
  @auth_token ||= begin
    @refresh_token_at = Time.now.to_i + TOKEN_EXPIRE_TIME_SEC
    token.encode
  end
end
check_token_should_refresh() click to toggle source
# File lib/lorkhan/connection.rb, line 91
def check_token_should_refresh
  return if @refresh_token_at.nil?

  refresh_token if Time.now.to_i >= @refresh_token_at
end
handle_http_error(response) click to toggle source
# File lib/lorkhan/connection.rb, line 81
def handle_http_error(response)
  if response.body
    if (reason = response.body['reason'])
      klass = Lorkhan::Errors::Apple::MAPPINGS[reason]
      raise klass, response if klass
    end
  end
  raise Errors::HTTPError, response
end
url() click to toggle source
# File lib/lorkhan/connection.rb, line 77
def url
  "https://#{host}:443"
end