Class: Prototok::Encoders::Protobuf

Inherits:
Base
  • Object
show all
Defined in:
lib/prototok/encoders/protobuf.rb

Constant Summary collapse

PROTOBUF_DEFAULTS =
{
  payload_class: '::Prototok::Protobuf::Payload'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#decode, #deserialize, #encode, #initialize, #options, #serialize

Constructor Details

This class inherits a constructor from Prototok::Encoders::Base

Class Method Details

.optionsObject



34
35
36
# File 'lib/prototok/encoders/protobuf.rb', line 34

def self.options
  @options ||= super.merge!(PROTOBUF_DEFAULTS)
end

Instance Method Details

#decode_payload(str) ⇒ Object



30
31
32
# File 'lib/prototok/encoders/protobuf.rb', line 30

def decode_payload str
  payload_class.decode(str)
end

#decode_token(str) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/prototok/encoders/protobuf.rb', line 17

def decode_token str
  decoded_token = Prototok::Protobuf::Token.decode(str)
  payload = decoded_token.payload.unpack(payload_class)
  token = deserialize(decoded_token.to_h)
  token.payload = payload
  token
end

#encode_payload(payload) ⇒ Object



25
26
27
28
# File 'lib/prototok/encoders/protobuf.rb', line 25

def encode_payload payload
  payload = payload_class.new(payload.to_h.reject { |_, v| v.nil? })
  payload_class.encode(payload)
end

#encode_token(payload, **header) ⇒ Object



11
12
13
14
15
# File 'lib/prototok/encoders/protobuf.rb', line 11

def encode_token payload, **header
  serialized = serialize(payload, **header)
  prepared_token = prepare_token(serialized)
  prepared_token.class.encode(prepared_token)
end