Class: Apnotic::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/apnotic/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/apnotic/connection.rb', line 19

def initialize(options={})
  @url             = options[:url] || APPLE_PRODUCTION_SERVER_URL
  @cert_path       = options[:cert_path]
  @cert_pass       = options[:cert_pass]
  @connect_timeout = options[:connect_timeout] || 30
  @auth_method     = options[:auth_method] || :cert
  @team_id         = options[:team_id]
  @key_id          = options[:key_id]
  @first_push      = true

  raise "Cert file not found: #{@cert_path}" unless @cert_path && (@cert_path.respond_to?(:read) || File.exist?(@cert_path))

  @client = NetHttp2::Client.new(@url, ssl_context: ssl_context, connect_timeout: @connect_timeout)
end

Instance Attribute Details

#cert_pathObject (readonly)

Returns the value of attribute cert_path.



10
11
12
# File 'lib/apnotic/connection.rb', line 10

def cert_path
  @cert_path
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/apnotic/connection.rb', line 10

def url
  @url
end

Class Method Details

.development(options = {}) ⇒ Object



13
14
15
16
# File 'lib/apnotic/connection.rb', line 13

def development(options={})
  options.merge!(url: APPLE_DEVELOPMENT_SERVER_URL)
  new(options)
end

Instance Method Details

#closeObject



62
63
64
# File 'lib/apnotic/connection.rb', line 62

def close
  @client.close
end

#joinObject



66
67
68
# File 'lib/apnotic/connection.rb', line 66

def join
  @client.join
end

#on(event, &block) ⇒ Object



70
71
72
# File 'lib/apnotic/connection.rb', line 70

def on(event, &block)
  @client.on(event, &block)
end

#prepare_push(notification) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/apnotic/connection.rb', line 53

def prepare_push(notification)
  request       = prepare_request(notification)
  http2_request = @client.prepare_request(:post, request.path,
    body:    request.body,
    headers: request.headers
  )
  Apnotic::Push.new(http2_request)
end

#push(notification, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/apnotic/connection.rb', line 34

def push(notification, options={})
  request  = prepare_request(notification)
  response = @client.call(:post, request.path,
    body:    request.body,
    headers: request.headers,
    timeout: options[:timeout]
  )
  Apnotic::Response.new(headers: response.headers, body: response.body) if response
end

#push_async(push) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/apnotic/connection.rb', line 44

def push_async(push)
  if @first_push
    @client.call_async(push.http2_request)
    @first_push = false
  else
    delayed_push_async(push)
  end
end