Class: Moped::Protocol::Reply

Inherits:
Object
  • Object
show all
Includes:
Message
Defined in:
lib/moped/protocol/reply.rb

Overview

The Protocol class representing messages received from a mongo connection.

Examples:

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"

Constant Summary collapse

UNAUTHORIZED =
10057

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Message

included, #inspect, #receive_replies, #serialize

Instance Attribute Details

#countNumber

Returns the number of documents returned.

Returns:

  • (Number)

    the number of documents returned



50
# File 'lib/moped/protocol/reply.rb', line 50

int32    :count

#cursor_idNumber

Returns the id of the cursor on the server.

Returns:

  • (Number)

    the id of the cursor on the server



42
# File 'lib/moped/protocol/reply.rb', line 42

int64    :cursor_id

#documentsArray

Returns the returned documents.

Returns:

  • (Array)

    the returned documents



54
# File 'lib/moped/protocol/reply.rb', line 54

document :documents, type: :array

#flagsArray<Symbol>

Returns the flags for this reply.

Returns:



36
37
38
# File 'lib/moped/protocol/reply.rb', line 36

flags    :flags, cursor_not_found:  2 ** 0,
query_failure:     2 ** 1,
await_capable:     2 ** 3

#lengthNumber

Returns the length of the message.

Returns:

  • (Number)

    the length of the message



20
# File 'lib/moped/protocol/reply.rb', line 20

int32 :length

#offsetNumber

Returns the starting position within the cursor.

Returns:

  • (Number)

    the starting position within the cursor



46
# File 'lib/moped/protocol/reply.rb', line 46

int32    :offset

#op_codeNumber

Returns the operation code of this message (always 1).

Returns:

  • (Number)

    the operation code of this message (always 1)



32
# File 'lib/moped/protocol/reply.rb', line 32

int32 :op_code

#request_idNumber

Returns the request id of the message.

Returns:

  • (Number)

    the request id of the message



24
# File 'lib/moped/protocol/reply.rb', line 24

int32 :request_id

#response_toNumber

Returns the id that generated the message.

Returns:

  • (Number)

    the id that generated the message



28
# File 'lib/moped/protocol/reply.rb', line 28

int32 :response_to

Class Method Details

.deserialize(buffer) ⇒ Reply

Consumes a buffer, returning the deserialized Reply message.

reply from.

Examples:

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

Parameters:

  • buffer (#read)

    an IO or IO-like resource to deserialize the

Returns:

  • (Reply)

    the deserialized reply



127
128
129
130
131
132
133
# File 'lib/moped/protocol/reply.rb', line 127

def deserialize(buffer)
  reply = allocate
  fields.each do |field|
    reply.__send__ :"deserialize_#{field}", buffer
  end
  reply
end

Instance Method Details

#command_failure?true, false

Note:

This is when ok is not 1, or “err” or “errmsg” are present.

Is the reply the result of a command failure?

Examples:

Did the command fail?

reply.command_failure?

Returns:

  • (true, false)

    If the command failed.

Since:

  • 1.2.10



68
69
70
71
# File 'lib/moped/protocol/reply.rb', line 68

def command_failure?
  result = documents[0]
  result["ok"] != 1 || result["err"] || result["errmsg"]
end

#cursor_not_found?true, false

Was the provided cursor id not found on the server?

Examples:

Is the cursor not on the server?

reply.cursor_not_found?

Returns:

  • (true, false)

    If the cursor went missing.

Since:

  • 1.2.0



81
82
83
# File 'lib/moped/protocol/reply.rb', line 81

def cursor_not_found?
  flags.include?(:cursor_not_found)
end

#query_failed?true, false

Did the query fail on the server?

Examples:

Did the query fail?

reply.query_failed?

Returns:

  • (true, false)

    If the query failed.

Since:

  • 1.2.0



93
94
95
# File 'lib/moped/protocol/reply.rb', line 93

def query_failed?
  flags.include?(:query_failure)
end

#unauthorized?true, false

Note:

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

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

Examples:

Was the query unauthorized.

reply.unauthorized?

Returns:

  • (true, false)

    If we had an authorization error.

Since:

  • 1.2.10



109
110
111
112
# File 'lib/moped/protocol/reply.rb', line 109

def unauthorized?
  result = documents[0]
  result["code"] == UNAUTHORIZED || result["assertionCode"] == UNAUTHORIZED
end