Class: Deimos::Transcoder

Inherits:
Object
  • Object
show all
Defined in:
lib/deimos/transcoder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, namespace:, key_field: nil, use_schema_classes: nil, topic: nil) ⇒ Transcoder

Returns a new instance of Transcoder.

Parameters:

  • schema (String)
  • namespace (String)
  • key_field (Symbol) (defaults to: nil)
  • use_schema_classes (Boolean) (defaults to: nil)
  • topic (String) (defaults to: nil)


11
12
13
14
15
16
17
# File 'lib/deimos/transcoder.rb', line 11

def initialize(schema:, namespace:, key_field: nil, use_schema_classes: nil, topic: nil)
  @schema = schema
  @namespace = namespace
  self.key_field = key_field
  @use_schema_classes = use_schema_classes
  @topic = topic
end

Instance Attribute Details

#backendClass < Deimos::SchemaBackends::Base]

Returns Class < Deimos::SchemaBackends::Base].

Returns:



20
21
22
# File 'lib/deimos/transcoder.rb', line 20

def backend
  @backend
end

#key_fieldObject

Returns the value of attribute key_field.



4
5
6
# File 'lib/deimos/transcoder.rb', line 4

def key_field
  @key_field
end

Instance Method Details

#call(message) ⇒ Object

Parameters:

  • message (Karafka::Messages::Message)

Returns:

  • (Object)


71
72
73
74
75
76
77
78
79
# File 'lib/deimos/transcoder.rb', line 71

def call(message)
  if self.key_field
    decode_key(message.raw_key)
  elsif message.respond_to?(:raw_payload)
    decode_message(message.raw_payload)
  else
    decode_message(message.raw_key)
  end
end

#decode_key(key) ⇒ Object

Parameters:

  • key (String)

Returns:

  • (Object)


37
38
39
40
41
42
43
44
45
46
# File 'lib/deimos/transcoder.rb', line 37

def decode_key(key)
  return nil if key.nil? || self.key_field.nil?

  decoded_key = self.backend.decode_key(key, self.key_field)
  return decoded_key unless @use_schema_classes

  Utils::SchemaClass.instance(decoded_key,
                              "#{@schema}_key",
                              @namespace)
end

#decode_message(payload) ⇒ Object

Parameters:

  • payload (String)

Returns:

  • (Object)


50
51
52
53
54
55
56
57
58
59
# File 'lib/deimos/transcoder.rb', line 50

def decode_message(payload)
  return nil if payload.nil?

  decoded_payload = self.backend.decode(payload)
  return decoded_payload unless @use_schema_classes

  Utils::SchemaClass.instance(decoded_payload,
                              @schema,
                              @namespace)
end

#decode_message_hash(payload) ⇒ Object

Parameters:

  • payload (String)

Returns:

  • (Object)


83
84
85
# File 'lib/deimos/transcoder.rb', line 83

def decode_message_hash(payload)
  self.key_field ? decode_key(payload) : decode_message(payload)
end

#encode(payload) ⇒ String

Parameters:

  • payload (Object)

Returns:

  • (String)


63
64
65
66
67
# File 'lib/deimos/transcoder.rb', line 63

def encode(payload)
  return nil if payload.nil?

  self.backend.encode(payload)
end

#encode_key(key) ⇒ String

for use in test helpers

Parameters:

  • key (Object)

Returns:

  • (String)


27
28
29
30
31
32
33
# File 'lib/deimos/transcoder.rb', line 27

def encode_key(key)
  if self.key_field
    self.backend.encode_key(self.key_field, key, topic: @topic)
  else
    self.backend.encode(key, topic: @topic)
  end
end