Class: Rcon::Response
- Inherits:
-
Object
- Object
- Rcon::Response
- Defined in:
- lib/rcon/response.rb
Overview
wraps the response we receive from the server. It might not be obvious at first why have this additional datastructure. There are two main motivations.
First, to separate how we deal with an AuthResponse vs a CommandResponse.
Secondly, when we are dealing with segmented responses, instead of modifying the first packet in place to add subsequent parts of the body, we modify the Response object corresponding with the total response. i.e. a Response is a sum of Packets.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ String
the response body, which may be the concatenation of the bodies of several packets.
-
#id ⇒ Integer
readonly
the initial request packet id corresponding to the response (except maybe for AuthResponse, see AuthResponse#success?.
-
#type ⇒ Symbol
readonly
the type of response; see Packet::RESPONSE_PACKET_TYPE.
Class Method Summary collapse
-
.from_packet(packet) ⇒ AuthResponse, CommandResponse
instantiate an instance of a Response subclass given a packet.
Instance Method Summary collapse
-
#initialize(id:, type:, body:) ⇒ Response
constructor
instantiate a new Response.
Constructor Details
#initialize(id:, type:, body:) ⇒ Response
instantiate a new Rcon::Response
42 43 44 45 46 |
# File 'lib/rcon/response.rb', line 42 def initialize(id:, type:, body:) @id = id @type = type @body = body end |
Instance Attribute Details
#body ⇒ String
the response body, which may be the concatenation of the bodies of several packets.
19 20 21 |
# File 'lib/rcon/response.rb', line 19 def body @body end |
#id ⇒ Integer (readonly)
the initial request packet id corresponding to the response (except maybe for AuthResponse, see AuthResponse#success?
19 20 21 |
# File 'lib/rcon/response.rb', line 19 def id @id end |
#type ⇒ Symbol (readonly)
the type of response; see Packet::RESPONSE_PACKET_TYPE
19 20 21 |
# File 'lib/rcon/response.rb', line 19 def type @type end |
Class Method Details
.from_packet(packet) ⇒ AuthResponse, CommandResponse
instantiate an instance of a Rcon::Response subclass given a packet.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rcon/response.rb', line 24 def self.from_packet(packet) params = { id: packet.id, type: packet.type, body: packet.body } case packet.type when :SERVERDATA_AUTH_RESPONSE AuthResponse.new(**params) when :SERVERDATA_RESPONSE_VALUE CommandResponse.new(**params) else raise Error::UnsupportedResponseTypeError.new(packet.type) end end |