lib/gibson/gibson.rb in gibson-1.0.4 vs lib/gibson/gibson.rb in gibson-1.0.5

- old
+ new

@@ -12,26 +12,36 @@ end end module Gibson class Client - ## # Create a new Gibson::Client instance, the options are passed to # Gibson::Connection initialize method. + # + # ==== Examples: + # + # Gibson::Client.new # will create a connection to the default /var/run/gibson.sock UNIX socket. + # Gibson::Client.new :address => '127.0.0.1' # will connect to localhost:10128 + # + # ==== Options: + # + # - :socket - The UNIX socket path, if this option is set a UNIX socket connection will be used. Default: /var/run/gibson.sock + # - :address - The ip address to connect to, if this option is set a TCP socket connection will be used. Default: nil + # - :port - The tcp port to connect to. Default: 10128 + # - :timeout - The connection and I/O timeout in milliseconds. Default: 100 + # - :keepalive - If a TCP connection will be used, set this to true to use the SO_KEEPALIVE flag on the socket. Default: false def initialize(opts = {}) @connection = nil @options = opts end - ## # Create the connection. def connect @connection = Connection.new( @options ) @connection.connect end - ## # Decode a REPL_VAL reply. def decode_val( encoding, size, io ) # plain string if encoding == Protocol::ENCODINGS[:plain] io.read_unpacked size, 'Z' + size.to_s @@ -42,11 +52,10 @@ else raise 'Unknown data encoding.' end end - ## # Decode a REPL_KVAL reply. def decode_kval( io, size ) count = io.read_unpacked 4, 'L<' obj = {} @@ -60,11 +69,10 @@ end obj end - ## # Reply decoding wrapper. def decode( code, encoding, size, io ) if code == Protocol::REPLIES[:val] decode_val encoding, size, io @@ -80,11 +88,10 @@ else io end end - ## # Send a query to the server given its opcode and arguments payload. # Return the decoded data, or raise one of the RuntimeErrors defined # inside Gibson::Protocol. def query( opcode, payload = '' ) connect if @connection == nil or not @connection.connected? @@ -98,21 +105,13 @@ data = @connection.read size decode code, encoding, size, StringIO.new(data) end - ## - # This method will be called for every undefined method call - # of Gibson::Client mapping the method to its opcode and creating - # its argument payload. - # For instance a call to: - # client = Gibson::Client.new - # client.set 0, 'foo', 'bar' - # Will be executed as: - # client.query Protocol::COMMANDS[:set], '0 foo bar' - def method_missing(name, *arguments) - if Protocol::COMMANDS.has_key? name - query Protocol::COMMANDS[name], arguments.join(' ') + # Map every command => opcode to an instance method. + Protocol::COMMANDS.each do |name,opcode| + define_method(name) do |*args| + query opcode, args.join(' ') end end private :decode_val, :decode_kval, :decode end