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 =

Unauthorized assertion errors.

[ 10057, 16550 ]

Constants included from Message

Message::INT32_DECODE_STR, Message::INT64_DECODE_ARRAY_STR, Message::INT64_DECODE_STR

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



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

int32    :count

#cursor_idNumber

Returns the id of the cursor on the server.

Returns:

  • (Number)

    the id of the cursor on the server



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

int64    :cursor_id

#documentsArray

Returns the returned documents.

Returns:

  • (Array)

    the returned documents



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

document :documents, type: :array

#flagsArray<Symbol>

Returns the flags for this reply.

Returns:

  • (Array<Symbol>)

    the flags for this reply



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

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



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

int32 :length

#offsetNumber

Returns the starting position within the cursor.

Returns:

  • (Number)

    the starting position within the cursor



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

int32    :offset

#op_codeNumber

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

Returns:

  • (Number)

    the operation code of this message (always 1)



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

int32 :op_code

#request_idNumber

Returns the request id of the message.

Returns:

  • (Number)

    the request id of the message



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

int32 :request_id

#response_toNumber

Returns the id that generated the message.

Returns:

  • (Number)

    the id that generated the message



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

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



145
146
147
148
149
150
151
# File 'lib/moped/protocol/reply.rb', line 145

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



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

def command_failure?
  result = documents.first
  (result["ok"] != 1.0 && result["ok"] != true) || error?
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



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

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

#error?true, false

Check if the first returned document in the reply is an error result.

Examples:

Is the first document in the reply an error?

reply.error?

Returns:

  • (true, false)

    If the first document is an error.

Since:

  • 2.0.0



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

def error?
  result = documents.first
  result && error_message(result)
end

#query_failure?true, false

Did the query fail on the server?

Examples:

Did the query fail?

reply.query_failure?

Returns:

  • (true, false)

    If the query failed.

Since:

  • 1.2.0



107
108
109
# File 'lib/moped/protocol/reply.rb', line 107

def query_failure?
  flags.include?(:query_failure) || error?
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



123
124
125
126
127
128
129
130
# File 'lib/moped/protocol/reply.rb', line 123

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/ || err =~ /not authorized/))
end