class Moped::Protocol::Reply
The Protocol class representing messages received from a mongo connection.
@example
socket = TCPSocket.new "127.0.0.1", 27017 command = Moped::Protocol::Command.new "admin", buildinfo: 1 socket.write command.serialize reply = Moped::Protocol::Reply.deserialize(socket) reply.documents[0]["version"] # => "2.0.0"
Constants
- UNAUTHORIZED
Unauthorized assertion errors.
Public Class Methods
Consumes a buffer, returning the deserialized Reply message.
@example
socket = TCPSocket.new "localhost", 27017 socket.write Moped::Protocol::Command.new(:admin, ismaster: 1).serialize reply = Moped::Protocol::Reply.deserialize(socket) reply.documents[0]['ismaster'] # => 1
@param [#read] buffer an IO or IO-like resource to deserialize the reply from. @return [Reply] the deserialized reply
# File lib/moped/protocol/reply.rb, line 133 def deserialize(buffer) reply = allocate fields.each do |field| reply.__send__ :"deserialize_#{field}", buffer end reply end
Public Instance Methods
Is the reply the result of a command failure?
@example Did the command fail?
reply.command_failure?
@note This is when ok is not 1, or “err” or “errmsg” are present.
@return [ true, false ] If the command failed.
@since 1.2.10
# File lib/moped/protocol/reply.rb, line 69 def command_failure? result = documents[0] result["ok"] != 1 || error_message(result) end
Was the provided cursor id not found on the server?
@example Is the cursor not on the server?
reply.cursor_not_found?
@return [ true, false ] If the cursor went missing.
@since 1.2.0
# File lib/moped/protocol/reply.rb, line 82 def cursor_not_found? flags.include?(:cursor_not_found) end
Did the query fail on the server?
@example Did the query fail?
reply.query_failed?
@return [ true, false ] If the query failed.
@since 1.2.0
# File lib/moped/protocol/reply.rb, line 94 def query_failed? result = documents[0] flags.include?(:query_failure) || (result && (result["err"] || result["errmsg"] || result["$err"])) end
Private Instance Methods
# File lib/moped/protocol/reply.rb, line 144 def deserialize_documents(buffer) documents = [] count.times do documents << BSON::Document.deserialize(buffer) end @documents = documents end
# File lib/moped/protocol/reply.rb, line 152 def error_message(result) result["err"] || result["errmsg"] || result["$err"] end