Class: Deimos::Consumer

Inherits:
Object
  • Object
show all
Includes:
Deimos::Consume::BatchConsumption, Deimos::Consume::MessageConsumption, SharedConfig
Defined in:
lib/deimos/consumer.rb

Overview

Basic consumer class. Inherit from this class and override either consume or consume_batch, depending on the delivery mode of your listener. ‘consume` -> use `delivery :message` or `delivery :batch` `consume_batch` -> use `delivery :inline_batch`

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Deimos::Consume::BatchConsumption

#around_consume_batch, #consume_batch

Methods included from Deimos::Consume::MessageConsumption

#around_consume, #consume

Class Method Details

.decoderDeimos::SchemaBackends::Base



21
22
23
24
# File 'lib/deimos/consumer.rb', line 21

def decoder
  @decoder ||= Deimos.schema_backend(schema: config[:schema],
                                     namespace: config[:namespace])
end

.key_decoderDeimos::SchemaBackends::Base



27
28
29
30
# File 'lib/deimos/consumer.rb', line 27

def key_decoder
  @key_decoder ||= Deimos.schema_backend(schema: config[:key_schema],
                                         namespace: config[:namespace])
end

Instance Method Details

#decode_key(key) ⇒ Object

Helper method to decode an encoded key.

Parameters:

  • key (String)

Returns:

  • (Object)

    the decoded key.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/deimos/consumer.rb', line 36

def decode_key(key)
  return nil if key.nil?

  config = self.class.config
  unless config[:key_configured]
    raise 'No key config given - if you are not decoding keys, please use '\
      '`key_config plain: true`'
  end

  if config[:key_field]
    self.class.decoder.decode_key(key, config[:key_field])
  elsif config[:key_schema]
    self.class.key_decoder.decode(key, schema: config[:key_schema])
  else # no encoding
    key
  end
end

#decode_message(payload) ⇒ Object

Helper method to decode an encoded message.

Parameters:

  • payload (Object)

Returns:

  • (Object)

    the decoded message.



57
58
59
60
61
62
63
64
# File 'lib/deimos/consumer.rb', line 57

def decode_message(payload)
  decoded_payload = payload.nil? ? nil : self.class.decoder.decode(payload)
  return decoded_payload unless Utils::SchemaClass.use?(self.class.config.to_h)

  Utils::SchemaClass.instance(decoded_payload,
                              self.class.config[:schema],
                              self.class.config[:namespace])
end