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



83
84
85
86
87
88
89
# File 'lib/moped/protocol/reply.rb', line 83

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

Instance Method Details

#cursor_not_found?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/moped/protocol/reply.rb', line 58

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

#query_failed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/moped/protocol/reply.rb', line 62

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

#unauthorized?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/moped/protocol/reply.rb', line 66

def unauthorized?
  documents.first["code"] == UNAUTHORIZED
end