Class: Prototok::Encoders::Base

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

Direct Known Subclasses

Json, Msgpack, Protobuf

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**encoder_options) ⇒ Base

Returns a new instance of Base.



17
18
19
# File 'lib/prototok/encoders.rb', line 17

def initialize(**encoder_options)
  options.merge!(encoder_options)
end

Class Method Details

.optionsObject



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

def self.options
  @options ||= Prototok.config[:encoder_options].dup
end

Instance Method Details

#decode(str) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/prototok/encoders.rb', line 30

def decode str
  case options[:encoding_mode].to_s
  when 'token'
    decode_token str
  when 'payload'
    decode_payload str
  end
end

#deserialize(data) ⇒ Object



48
49
50
# File 'lib/prototok/encoders.rb', line 48

def deserialize data
  Token.new(Serializers.find(:token).decode (data))
end

#encode(payload, **header) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/prototok/encoders.rb', line 21

def encode payload, **header
  case options[:encoding_mode].to_s
  when 'token'
    encode_token payload, **header
  when 'payload'
    encode_payload payload
  end
end

#optionsObject



9
10
11
# File 'lib/prototok/encoders.rb', line 9

def options
  @options ||= self.class.options.dup
end

#serialize(payload = nil, **header) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/prototok/encoders.rb', line 39

def serialize payload=nil, **header
  if payload.is_a? Token
    token = payload.dup.update!(header)
  else
    token = Token.new.update!(header.merge(:payload => payload))
  end
   Serializers.find(:token).new(token).encode
end