lib/oxblood/session.rb in oxblood-0.1.0.dev2 vs lib/oxblood/session.rb in oxblood-0.1.0.dev3

- old
+ new

@@ -1,6 +1,5 @@ -require 'oxblood/connection' require 'oxblood/command' module Oxblood class Session def initialize(connection) @@ -183,10 +182,32 @@ # ------------------ Strings --------------------- # ------------------ Connection --------------------- + # Authenticate to the server + # @see http://redis.io/commands/auth + # + # @param [String] password + # + # @return [String] 'OK' + def auth(password) + run(cmd.auth(password)) + end + + # Like {#auth}, except that if error returned, raises it. + # + # @param [String] password + # + # @raise [Protocol::RError] if error returned + # + # @return [String] 'OK' + def auth!(password) + response = auth(password) + error?(response) ? (raise response) : response + end + # Returns PONG if no argument is provided, otherwise return a copy of # the argument as a bulk # @see http://redis.io/commands/ping # # @param [String] message to return @@ -194,22 +215,29 @@ # @return [String] message passed as argument def ping(message = nil) run(cmd.ping(message)) end + # Change the selected database for the current connection + # @see http://redis.io/commands/select + # + # @param [Integer] index database to switch + # + # @return [String] 'OK' + def select(index) + run(cmd.select(index)) + end + # ------------------ Server --------------------- # Returns information and statistics about the server in a format that is # simple to parse by computers and easy to read by humans # @see http://redis.io/commands/info # # @param [String] section used to select a specific section of information def info(section = nil) - command = [:INFO] - command << section if section - - response = run(cmd.info(section)) + run(cmd.info(section)) # FIXME: Parse response end # ------------------ Keys ------------------------ @@ -301,8 +329,14 @@ end def run(command) @connection.write(command) @connection.read_response + end + + private + + def error?(response) + Protocol::RError === response end end end