zas-client 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/zas/client.rb CHANGED
@@ -4,6 +4,11 @@ require 'yajl'
4
4
  require 'hashie'
5
5
 
6
6
  module Zas
7
+ # A TimeoutError may be raised if the Zas authentication service fails to respond
8
+ # within a timeout limit.
9
+ class TimeoutError < RuntimeError
10
+ end
11
+
7
12
  class Client
8
13
  require 'zas/client_configuration'
9
14
  require 'zas/credentials'
@@ -15,6 +20,7 @@ module Zas
15
20
  def initialize(config=ClientConfiguration.new)
16
21
  self.host = config.host
17
22
  self.port = config.port
23
+ self.timeout = config.timeout
18
24
  self.context = ZMQ::Context.new
19
25
  self.logger = Syslogger.new(config.name, Syslog::LOG_PID, Syslog::LOG_LOCAL0)
20
26
 
@@ -26,12 +32,19 @@ module Zas
26
32
  # credentials - The credentials
27
33
  #
28
34
  # Returns the results of the authentication.
35
+ #
36
+ # Raises a Zas::TimeoutError if the authentication service does not respond
37
+ # within the timeout as specified the configuration.
29
38
  def authenticate(credentials)
30
39
  begin
31
40
  socket.send credentials.to_wire
32
- Hashie::Mash.new(Yajl::Parser.parse(socket.recv))
41
+ if ZMQ.select(socket, nil, nil, timeout)
42
+ Hashie::Mash.new(Yajl::Parser.parse(socket.recv))
43
+ else
44
+ raise Zas::TimeoutError, "Response from authentication service not received in time"
45
+ end
33
46
  rescue IOError => e
34
- logger.error "Shutting down: #{e.message}"
47
+ logger.error "IO error occurred: #{e.message}"
35
48
  end
36
49
  end
37
50
 
@@ -50,6 +63,7 @@ module Zas
50
63
  attr_accessor :context
51
64
  attr_accessor :host
52
65
  attr_accessor :port
66
+ attr_accessor :timeout
53
67
  attr_accessor :logger
54
68
 
55
69
  def socket
@@ -4,7 +4,8 @@ module Zas
4
4
  # host - The IP address of the service. Defaults to 127.0.0.1
5
5
  # port - The port number for the service.
6
6
  # name - The name of the client for logging.
7
- class ClientConfiguration < Struct.new(:host, :port, :name)
7
+ # timeout - The timeout for responses. Defaults to 1 second.
8
+ class ClientConfiguration < Struct.new(:host, :port, :name, :timeout)
8
9
  def initialize(attributes={})
9
10
  attributes.each do |k, v|
10
11
  self[k] = v
@@ -19,5 +20,8 @@ module Zas
19
20
  def name
20
21
  self[:name] || 'zas-client'
21
22
  end
23
+ def timeout
24
+ self[:timeout] || 1
25
+ end
22
26
  end
23
27
  end
@@ -1,9 +1,16 @@
1
1
  module Zas
2
+ # Handle credentials as a Hash
2
3
  class Credentials
4
+ # Initialize the credentials with the given Hash.
5
+ #
6
+ # credentials - A Hash with the keys :username and :password
3
7
  def initialize(credentials)
4
8
  self.credentials = credentials
5
9
  end
6
10
 
11
+ # Construct a wire representation of the credentials
12
+ #
13
+ # Returns the encoded JSON
7
14
  def to_wire
8
15
  Yajl::Encoder.encode({:strategy => :auth, :credentials => credentials})
9
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zas-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-22 00:00:00.000000000 Z
12
+ date: 2012-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: zmq