Class: ApnsProviderApi::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/apns_provider_api/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, certificate, passphrase) ⇒ Connection

Returns a new instance of Connection.



26
27
28
29
30
31
32
33
# File 'lib/apns_provider_api/connection.rb', line 26

def initialize(uri, certificate, passphrase)
  @uri = URI(uri)
  @certificate = certificate
  @passphrase = passphrase
  @http2client = HTTP2::Client.new
  events
  @http2client
end

Instance Attribute Details

#certificateObject (readonly)

def_delegators :@stream, :headers, :data



11
12
13
# File 'lib/apns_provider_api/connection.rb', line 11

def certificate
  @certificate
end

#http2clientObject (readonly)

def_delegators :@stream, :headers, :data



11
12
13
# File 'lib/apns_provider_api/connection.rb', line 11

def http2client
  @http2client
end

#passphraseObject (readonly)

def_delegators :@stream, :headers, :data



11
12
13
# File 'lib/apns_provider_api/connection.rb', line 11

def passphrase
  @passphrase
end

#socketObject (readonly)

def_delegators :@stream, :headers, :data



11
12
13
# File 'lib/apns_provider_api/connection.rb', line 11

def socket
  @socket
end

#sslObject (readonly)

def_delegators :@stream, :headers, :data



11
12
13
# File 'lib/apns_provider_api/connection.rb', line 11

def ssl
  @ssl
end

Class Method Details

.open(uri, certificate, passphrase) {|connection| ... } ⇒ Object

Yields:

  • (connection)


14
15
16
17
18
19
20
21
22
23
# File 'lib/apns_provider_api/connection.rb', line 14

def open(uri, certificate, passphrase)
  return unless block_given?

  connection = new(uri, certificate, passphrase)
  connection.open

  yield connection

  connection.close
end

Instance Method Details

#closeObject



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

def close
  return false if closed?
  @socket.close
  @socket = nil

  @ssl.close
  @ssl = nil

end

#closed?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/apns_provider_api/connection.rb', line 63

def closed?
  not open?
end

#eventsObject



71
72
73
74
75
76
77
# File 'lib/apns_provider_api/connection.rb', line 71

def events
  @http2client.on(:frame) do |bytes|
    # puts "Sending bytes: #{bytes.unpack("H*").first}"
    ssl.print bytes
    ssl.flush
  end
end

#new_streamObject



67
68
69
# File 'lib/apns_provider_api/connection.rb', line 67

def new_stream
  @http2client.new_stream
end

#openObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/apns_provider_api/connection.rb', line 35

def open
  return false if open?

  @socket = TCPSocket.new(@uri.host, @uri.port)
  context = OpenSSL::SSL::SSLContext.new
  context.key = OpenSSL::PKey::RSA.new(@certificate, @passphrase)
  context.cert = OpenSSL::X509::Certificate.new(certificate)

  @ssl = OpenSSL::SSL::SSLSocket.new(@socket, context)
  @ssl.sync_close = true
  @ssl.hostname = @uri.hostname
  @ssl.connect
end

#open?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/apns_provider_api/connection.rb', line 49

def open?
  not (@ssl and @socket).nil?
end