Class: Rlite::Connection::Hirlite

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Hirlite

Returns a new instance of Hirlite.



27
28
29
# File 'lib/hirlite/connection.rb', line 27

def initialize(connection)
  @connection = connection
end

Class Method Details

.connect(config) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hirlite/connection.rb', line 10

def self.connect(config)
  connection = ::Hirlite::Rlite.new
  connect_timeout = (config.fetch(:connect_timeout, 0) * 1_000_000).to_i

  if config[:scheme] == "unix"
    connection.connect_unix(config[:path], connect_timeout)
  else
    connection.connect(config[:host], config[:port], connect_timeout)
  end

  instance = new(connection)
  instance.timeout = config[:timeout]
  instance
rescue Errno::ETIMEDOUT
  raise TimeoutError
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/hirlite/connection.rb', line 31

def connected?
  @connection && @connection.connected?
end

#disconnectObject



40
41
42
43
# File 'lib/hirlite/connection.rb', line 40

def disconnect
  @connection.disconnect
  @connection = nil
end

#readObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hirlite/connection.rb', line 51

def read
  reply = @connection.read
  if reply == nil
      write(['__rlite_poll'])
      reply = @connection.read
  end
  reply = CommandError.new(reply.message) if reply.is_a?(RuntimeError)
  reply
rescue Errno::EAGAIN
  raise TimeoutError
rescue RuntimeError => err
  raise ProtocolError.new(err.message)
end

#timeout=(timeout) ⇒ Object



35
36
37
38
# File 'lib/hirlite/connection.rb', line 35

def timeout=(timeout)
  # Hirlite works with microsecond timeouts
  @connection.timeout = Integer(timeout * 1_000_000)
end

#write(command) ⇒ Object



45
46
47
48
49
# File 'lib/hirlite/connection.rb', line 45

def write(command)
  @connection.write(command.flatten(1))
rescue Errno::EAGAIN
  raise TimeoutError
end