Class: ESM::Connection::Request
- Inherits:
-
Object
- Object
- ESM::Connection::Request
- Defined in:
- lib/esm/connection/request.rb
Constant Summary collapse
- TYPES =
These numbers MUST match the associated RequestType enum value in esm_arma/src/esm/src/request.rs
{ 0 => :noop, 1 => :error, 2 => :heartbeat, 3 => :identification, 4 => :initialize, 5 => :handshake, 6 => :message }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(type:, id: nil, content: nil) ⇒ Request
constructor
A new instance of Request.
- #to_h ⇒ Object
Constructor Details
#initialize(type:, id: nil, content: nil) ⇒ Request
Returns a new instance of Request.
24 25 26 27 28 29 30 31 32 |
# File 'lib/esm/connection/request.rb', line 24 def initialize(type:, id: nil, content: nil) id ||= SecureRandom.uuid id = id.delete("-") raise ArgumentError, "ID must be 32 bytes" if id.size != 32 # The size of a UUID without the dashes raise ArgumentError, "Invalid type #{type}" unless TYPES.value?(type.to_sym) super(id: id, type: type, content: content) end |
Class Method Details
.from_client(data) ⇒ Object
18 19 20 |
# File 'lib/esm/connection/request.rb', line 18 def self.from_client(data) new(id: data[:i], type: TYPES[data[:t]], content: data[:c]&.pack("C*")) end |
Instance Method Details
#to_h ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/esm/connection/request.rb', line 34 def to_h { i: id, t: TYPES.key(type), c: case content when NilClass [] when Array content when Symbol content.to_s.bytes when ->(c) { c.respond_to?(:bytes) } content.bytes else raise ArgumentError, "Content must be nil, Symbol, Array, or must respond to #bytes" end } end |