Class: ADCK::Connection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connection

Returns a new instance of Connection.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/adck/connection.rb', line 3

def initialize(opts={})
  @host = opts[:host]||ADCK.host
  @port = opts[:port]||ADCK.port
  @pem  = opts[:pem] ||ADCK.pem
  @pass = opts[:pass]||ADCK.pass

  raise "The path to your pem file is not set. (ADCK.pem = /path/to/cert.pem)" unless @pem
  raise "The path to your pem file does not exist!" unless File.exist?(@pem)

  @context      = OpenSSL::SSL::SSLContext.new
  @context.cert = OpenSSL::X509::Certificate.new(File.read(@pem))
  @context.key  = OpenSSL::PKey::RSA.new(File.read(@pem), @pass)
end

Class Method Details

.feedback(opts = {}) ⇒ Object



72
73
74
75
# File 'lib/adck/connection.rb', line 72

def feedback(opts={})
  opts[:host] ||= ADCK.host.sub('gateway','feedback')
  new(opts)
end

Instance Method Details

#closeObject



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

def close
  sock.close if @sock
  ssl.close if @ssl
  @sock = nil
  @ssl = nil
end

#open {|sock, ssl| ... } ⇒ Object

Yields:



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

def open
  is_open = open?

  _sock, _ssl = sock, ssl

  yield(sock,ssl) if block_given?

  if !is_open && block_given?
    close
  end

  return _sock, _ssl
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  !!(@sock || @ssl)
end

#send_notification(token, message = nil) ⇒ Object



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

def send_notification token, message=nil
  if token.is_a?(Notification)
    n = token
  else
    n = Notification.new(token,message)
  end

  send_notifications([n])
end

#send_notifications(msgs) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/adck/connection.rb', line 63

def send_notifications msgs
  open do |sock,ssl|
    msgs.each do |n|
      ssl.write(n.packaged_notification)
    end
  end
end

#sockObject



17
18
19
# File 'lib/adck/connection.rb', line 17

def sock
  @sock ||= TCPSocket.new(@host, @port)
end

#sslObject



21
22
23
24
25
26
# File 'lib/adck/connection.rb', line 21

def ssl
  @ssl ||= begin
    ssl = OpenSSL::SSL::SSLSocket.new(sock,@context)
    ssl.connect
  end
end