Class: SSDB::Client

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

Constant Summary collapse

NL =
"\n".freeze
OK =
"ok".freeze
NOT_FOUND =
"not_found".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :url (String|URI)

    the URL to connect to,required

  • :timeout (Numeric)

    socket timeout, defaults to 10s



16
17
18
19
20
21
# File 'lib/ssdb/client.rb', line 16

def initialize(opts = {})
  @timeout   = opts[:timeout] || 10.0
  @sock      = nil
  @url       = parse_url(opts[:url] || ENV["SSDB_URL"] || "ssdb://127.0.0.1:8888/")
  @reconnect = opts[:reconnect] != false
end

Instance Attribute Details

#reconnectObject

Returns the value of attribute reconnect.



11
12
13
# File 'lib/ssdb/client.rb', line 11

def reconnect
  @reconnect
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



10
11
12
# File 'lib/ssdb/client.rb', line 10

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/ssdb/client.rb', line 10

def url
  @url
end

Instance Method Details

#call(opts) ⇒ Object

Calls a single command

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

  • :cmd (Array)

    command parts

  • :multi (Boolean)

    true if multi-response is expected

  • :proc (Proc)

    a proc to apply to the result

  • :args (Array)

    arguments to pass to the :proc



52
53
54
# File 'lib/ssdb/client.rb', line 52

def call(opts)
  perform([opts])[0]
end

#connected?Boolean

Returns true if connected.

Returns:

  • (Boolean)

    true if connected



34
35
36
# File 'lib/ssdb/client.rb', line 34

def connected?
  !!@sock
end

#disconnectObject

Disconnects the client



39
40
41
42
43
44
# File 'lib/ssdb/client.rb', line 39

def disconnect
  @sock.close if connected?
rescue
ensure
  @sock = nil
end

#idString

Returns URL string.

Returns:

  • (String)

    URL string



24
25
26
# File 'lib/ssdb/client.rb', line 24

def id
  url.to_s
end

#perform(commands) ⇒ Object

Performs multiple commands

Parameters:

  • commands (Array<Hash>)

    array of command options

See Also:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ssdb/client.rb', line 59

def perform(commands)
  message = ""

  commands.each do |hash|
    hash[:cmd].each do |c|
      message << c.bytesize.to_s << NL << c << NL
    end
    message << NL
  end

  results = []
  ensure_connected do
    io(:write, message)

    commands.each do |hash|
      part = read_part(hash[:multi])
      if hash[:proc]
        args = [part]
        args.concat(hash[:args]) if hash[:args]
        part = hash[:proc].call(*args)
      end
      results << part
    end
  end

  results
end

#portInteger

Returns port.

Returns:

  • (Integer)

    port



29
30
31
# File 'lib/ssdb/client.rb', line 29

def port
  @port ||= url.port || 8888
end