Class: SSDB::Client
- Inherits:
-
Object
- Object
- SSDB::Client
- Defined in:
- lib/ssdb/client.rb
Constant Summary collapse
- NL =
"\n".freeze
- OK =
"ok".freeze
- NOT_FOUND =
"not_found".freeze
Instance Attribute Summary collapse
-
#reconnect ⇒ Object
Returns the value of attribute reconnect.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#call(opts) ⇒ Object
Calls a single command.
-
#connected? ⇒ Boolean
True if connected.
-
#disconnect ⇒ Object
Disconnects the client.
-
#id ⇒ String
URL string.
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#perform(commands) ⇒ Object
Performs multiple commands.
-
#port ⇒ Integer
Port.
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
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
#reconnect ⇒ Object
Returns the value of attribute reconnect.
11 12 13 |
# File 'lib/ssdb/client.rb', line 11 def reconnect @reconnect end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/ssdb/client.rb', line 10 def timeout @timeout end |
#url ⇒ Object (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
52 53 54 |
# File 'lib/ssdb/client.rb', line 52 def call(opts) perform([opts])[0] end |
#connected? ⇒ Boolean
Returns true if connected.
34 35 36 |
# File 'lib/ssdb/client.rb', line 34 def connected? !!@sock end |
#disconnect ⇒ Object
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 |
#id ⇒ String
Returns URL string.
24 25 26 |
# File 'lib/ssdb/client.rb', line 24 def id url.to_s end |
#perform(commands) ⇒ Object
Performs multiple commands
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) = "" commands.each do |hash| hash[:cmd].each do |c| << c.bytesize.to_s << NL << c << NL end << NL end results = [] ensure_connected do io(:write, ) 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 |
#port ⇒ Integer
Returns port.
29 30 31 |
# File 'lib/ssdb/client.rb', line 29 def port @port ||= url.port || 8888 end |