Class: Zas::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zas/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = ClientConfiguration.new) ⇒ Client

Initialize the client with the given configuration.

config - Configuration spec for the client.



20
21
22
23
24
25
26
27
28
# File 'lib/zas/client.rb', line 20

def initialize(config=ClientConfiguration.new)
  self.host = config.host
  self.port = config.port
  self.timeout = config.timeout
  self.context = ZMQ::Context.new
  self.logger = Syslogger.new(config.name, Syslog::LOG_PID, Syslog::LOG_LOCAL0) 

  at_exit { close }
end

Instance Method Details

#authenticate(credentials) ⇒ Object

Public: Authenticate the given credentials.

credentials - The credentials

Returns the results of the authentication.

Raises a Zas::TimeoutError if the authentication service does not respond within the timeout as specified the configuration.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zas/client.rb', line 38

def authenticate(credentials)
  begin
    socket.send credentials.to_wire
    if ZMQ.select([socket], nil, nil, timeout)
      Hashie::Mash.new(Yajl::Parser.parse(socket.recv))
    else
      disconnect
      raise Zas::TimeoutError, "Response from authentication service not received in time"
    end
  rescue IOError => e
    logger.error "IO error occurred: #{e.message}" 
  end 
end

#closeObject

Public: Close the context.



59
60
61
# File 'lib/zas/client.rb', line 59

def close
  context.close if context
end

#disconnectObject

Public: Disconnect the socket.



53
54
55
56
# File 'lib/zas/client.rb', line 53

def disconnect
  socket.close if socket
  @socket = nil
end