Module: Rexpro::Message::Base

Included in:
Error, ScriptRequest, ScriptResponse, SessionRequest, SessionResponse
Defined in:
lib/rexpro/message.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#serializer_typeObject (readonly)

Returns the value of attribute serializer_type.



113
114
115
# File 'lib/rexpro/message.rb', line 113

def serializer_type
  @serializer_type
end

Class Method Details

.included(klass) ⇒ Object



70
71
72
73
74
# File 'lib/rexpro/message.rb', line 70

def self.included(klass)
  klass.extend ClassMethods
  klass.define_fields session_uuid: :to_s, request_uuid: :to_s,
                      meta: :to_hash
end

Instance Method Details

#initialize(attrs = {}) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/rexpro/message.rb', line 115

def initialize(attrs = {})
  @serializer_type = attrs.delete(:serializer_type) || SERIALIZER_MSGPACK
  self.meta ||= {}
  attrs.each { |k, v| send("#{k}=", v) }
  self.session_uuid ||= ZERO_UUID
  self.request_uuid ||= Message.generate_uuid
end

#serialize_body(*args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rexpro/message.rb', line 123

def serialize_body(*args)
  if serializer_type != SERIALIZER_MSGPACK
    raise NotImplementedError, 'only MsgPack serialization is supported'
  end

  self.class.fields.map do |field|
    value = send(field)
    field_method = self.class.field_methods[field]
    value = value.send(field_method) if field_method
    value
  end.to_msgpack(*args)
end

#write_to(io) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/rexpro/message.rb', line 136

def write_to(io)
  body = serialize_body
  header = [
    PROTOCOL_VERSION, serializer_type, 0, self.class.type, body.size
    ].pack('CCNCN')
  io.write(header + body)
end