Class: Deimos::Transcoder
- Inherits:
-
Object
- Object
- Deimos::Transcoder
- Defined in:
- lib/deimos/transcoder.rb
Instance Attribute Summary collapse
-
#backend ⇒ Class < Deimos::SchemaBackends::Base]
Class < Deimos::SchemaBackends::Base].
-
#key_field ⇒ Object
Returns the value of attribute key_field.
Instance Method Summary collapse
- #call(message) ⇒ Object
- #decode_key(key) ⇒ Object
- #decode_message(payload) ⇒ Object
- #decode_message_hash(payload) ⇒ Object
- #encode(payload) ⇒ String
-
#encode_key(key) ⇒ String
for use in test helpers.
-
#initialize(schema:, namespace:, key_field: nil, use_schema_classes: nil, topic: nil) ⇒ Transcoder
constructor
A new instance of Transcoder.
Constructor Details
#initialize(schema:, namespace:, key_field: nil, use_schema_classes: nil, topic: nil) ⇒ Transcoder
Returns a new instance of Transcoder.
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
#backend ⇒ Class < Deimos::SchemaBackends::Base]
Returns Class < Deimos::SchemaBackends::Base].
20 21 22 |
# File 'lib/deimos/transcoder.rb', line 20 def backend @backend end |
#key_field ⇒ Object
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
71 72 73 74 75 76 77 78 79 |
# File 'lib/deimos/transcoder.rb', line 71 def call() if self.key_field decode_key(.raw_key) elsif .respond_to?(:raw_payload) (.raw_payload) else (.raw_key) end end |
#decode_key(key) ⇒ 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
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/deimos/transcoder.rb', line 50 def (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
83 84 85 |
# File 'lib/deimos/transcoder.rb', line 83 def (payload) self.key_field ? decode_key(payload) : (payload) end |
#encode(payload) ⇒ 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
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 |