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

deserialize(buffer) click to toggle source

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

command_failure?() click to toggle source

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
cursor_not_found?() click to toggle source

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
query_failed?() click to toggle source

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
unauthorized?() click to toggle source

Is the reply an error message that we are not authorized for the query or command?

@example Was the query unauthorized.

reply.unauthorized?

@note So far this can be a “code” of 10057 in the error message or an

"assertionCode" of 10057.

@return [ true, false ] If we had an authorization error.

@since 1.2.10

# File lib/moped/protocol/reply.rb, line 111
def unauthorized?
  result = documents[0]
  return false if result.nil?
  err = error_message(result)
  UNAUTHORIZED.include?(result["code"]) ||
    UNAUTHORIZED.include?(result["assertionCode"]) ||
    (err && err =~ /unauthorized/)
end

Private Instance Methods

deserialize_documents(buffer) click to toggle source
# 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
error_message(result) click to toggle source
# File lib/moped/protocol/reply.rb, line 152
def error_message(result)
  result["err"] || result["errmsg"] || result["$err"]
end