Class: AMQP::Client::MessageCodecRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp/client/message_codec_registry.rb

Overview

Internal registry that stores content_type parsers and content_encoding coders. Only exact content_type and content_encoding matches are supported.

Instance Method Summary collapse

Constructor Details

#initializeMessageCodecRegistry

Returns a new instance of MessageCodecRegistry.



8
9
10
11
# File 'lib/amqp/client/message_codec_registry.rb', line 8

def initialize
  @parsers = {} # content_type => handler
  @coders = {} # content_encoding => handler
end

Instance Method Details

#dupObject

Lightweight cloning: registry contents duplicated (shallow copy of handler references)



84
85
86
87
88
89
# File 'lib/amqp/client/message_codec_registry.rb', line 84

def dup
  copy = self.class.new
  @parsers.each { |k, v| copy.register_parser(content_type: k, parser: v) }
  @coders.each { |k, v| copy.register_coder(content_encoding: k, coder: v) }
  copy
end

#enable_builtin_codecsself

Enable all built-in codecs (parsers and coders)

Returns:

  • (self)


79
80
81
# File 'lib/amqp/client/message_codec_registry.rb', line 79

def enable_builtin_codecs
  enable_builtin_parsers.enable_builtin_coders
end

#enable_builtin_codersself

Enable built-in coders for common content encodings

Returns:

  • (self)


71
72
73
74
75
# File 'lib/amqp/client/message_codec_registry.rb', line 71

def enable_builtin_coders
  register_coder(content_encoding: "gzip", coder: Coders::Gzip)
  register_coder(content_encoding: "deflate", coder: Coders::Deflate)
  self
end

#enable_builtin_parsersself

Enable built-in parsers for common content types

Returns:

  • (self)


63
64
65
66
67
# File 'lib/amqp/client/message_codec_registry.rb', line 63

def enable_builtin_parsers
  register_parser(content_type: "text/plain", parser: Parsers::Plain)
  register_parser(content_type: "application/json", parser: Parsers::JSONParser)
  self
end

#find_coder(content_encoding) ⇒ Object?

Find coder handler based on content_encoding

Parameters:

  • content_encoding (String)

    The content_encoding to match

Returns:

  • (Object, nil)

    The coder object or nil if not found



45
46
47
# File 'lib/amqp/client/message_codec_registry.rb', line 45

def find_coder(content_encoding)
  @coders[content_encoding]
end

#find_parser(content_type) ⇒ Object?

Find parser handler based on message properties

Parameters:

  • content_type (String)

    The content_type to match

Returns:

  • (Object, nil)

    The parser object or nil if not found



38
39
40
# File 'lib/amqp/client/message_codec_registry.rb', line 38

def find_parser(content_type)
  @parsers[content_type]
end

#list_content_encodingsArray<String>

Introspection helper to list all registered content encodings

Returns:

  • (Array<String>)

    List of registered content encodings



57
58
59
# File 'lib/amqp/client/message_codec_registry.rb', line 57

def list_content_encodings
  @coders.keys
end

#list_content_typesArray<String>

Introspection helper to list all registered content types

Returns:

  • (Array<String>)

    List of registered content types



51
52
53
# File 'lib/amqp/client/message_codec_registry.rb', line 51

def list_content_types
  @parsers.keys
end

#register_coder(content_encoding:, coder:) ⇒ self

Register a coder for a specific content_encoding

Parameters:

  • content_encoding (String)

    The content_encoding to match

  • coder (Object)

    The coder object, must respond to encode(data, properties) and decode(data, properties)

Returns:

  • (self)


29
30
31
32
33
# File 'lib/amqp/client/message_codec_registry.rb', line 29

def register_coder(content_encoding:, coder:)
  validate_coder!(coder)
  @coders[content_encoding] = coder
  self
end

#register_parser(content_type:, parser:) ⇒ self

Register a parser for a content_type

Parameters:

  • content_type (String)

    The content_type to match

  • parser (Object)

    The parser object, must respond to parse(data, properties) and serialize(obj, properties)

Returns:

  • (self)


18
19
20
21
22
# File 'lib/amqp/client/message_codec_registry.rb', line 18

def register_parser(content_type:, parser:)
  validate_parser!(parser)
  @parsers[content_type] = parser
  self
end