Module: AceMQ::AMQP::Codec

Defined in:
lib/acemq/amqp/codec.rb

Overview

What turns a payload into bytes and back.

Not a base class to inherit from — anything answering these four methods is a codec, which is how a codec from another gem works here without depending on this one. The methods are documented here because a duck type still has to be written down somewhere.

Ruby's decode returns a value rather than filling in a destination the caller passed. That is the one place this deliberately departs from the Go shape, which has no choice: Ruby has no type to be told, so asking for one would be ceremony with nothing behind it.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check!(candidate) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError) —

    when candidate is not a codec



85
86
87
88
89
90
91
# File 'lib/acemq/amqp/codec.rb', line 85

def self.check!(candidate)
  return candidate if codec?(candidate)

  raise ArgumentError,
        "#{candidate.inspect} is not a codec: it needs content_type, encode, " \
        "decode and can_decode?"
end

.codec?(candidate) ⇒ Boolean

Whether an object can stand in as a codec.

Checked when a connection is built rather than when the first message arrives, so a codec missing a method is a mistake at start-up instead of a mystery at three in the morning.

Parameters:

  • candidate (Object)

Returns:

  • (Boolean)


79
80
81
# File 'lib/acemq/amqp/codec.rb', line 79

def self.codec?(candidate)
  %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) }
end

Instance Method Details

#can_decode?(content_type) ⇒ Boolean

Returns whether this codec should handle it.

Parameters:

  • content_type (String) —

    what the sender said, or "" when it said nothing

Returns:

  • (Boolean) —

    whether this codec should handle it



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/acemq/amqp/codec.rb', line 70

module Codec
  # Whether an object can stand in as a codec.
  #
  # Checked when a connection is built rather than when the first message
  # arrives, so a codec missing a method is a mistake at start-up instead
  # of a mystery at three in the morning.
  #
  # @param candidate [Object]
  # @return [Boolean]
  def self.codec?(candidate)
    %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) }
  end

  # @raise [ArgumentError] when +candidate+ is not a codec
  # @api private
  def self.check!(candidate)
    return candidate if codec?(candidate)

    raise ArgumentError,
          "#{candidate.inspect} is not a codec: it needs content_type, encode, " \
          "decode and can_decode?"
  end
end

#content_type ⇒ String

Returns what this codec writes onto a message.

Returns:

  • (String) —

    what this codec writes onto a message



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/acemq/amqp/codec.rb', line 70

module Codec
  # Whether an object can stand in as a codec.
  #
  # Checked when a connection is built rather than when the first message
  # arrives, so a codec missing a method is a mistake at start-up instead
  # of a mystery at three in the morning.
  #
  # @param candidate [Object]
  # @return [Boolean]
  def self.codec?(candidate)
    %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) }
  end

  # @raise [ArgumentError] when +candidate+ is not a codec
  # @api private
  def self.check!(candidate)
    return candidate if codec?(candidate)

    raise ArgumentError,
          "#{candidate.inspect} is not a codec: it needs content_type, encode, " \
          "decode and can_decode?"
  end
end

#decode(body) ⇒ Object

Returns the payload.

Parameters:

  • body (String) —

    the bytes as they arrived

Returns:

  • (Object) —

    the payload



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/acemq/amqp/codec.rb', line 70

module Codec
  # Whether an object can stand in as a codec.
  #
  # Checked when a connection is built rather than when the first message
  # arrives, so a codec missing a method is a mistake at start-up instead
  # of a mystery at three in the morning.
  #
  # @param candidate [Object]
  # @return [Boolean]
  def self.codec?(candidate)
    %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) }
  end

  # @raise [ArgumentError] when +candidate+ is not a codec
  # @api private
  def self.check!(candidate)
    return candidate if codec?(candidate)

    raise ArgumentError,
          "#{candidate.inspect} is not a codec: it needs content_type, encode, " \
          "decode and can_decode?"
  end
end

#encode(payload) ⇒ String

Returns the bytes to publish.

Parameters:

  • payload (Object)

Returns:

  • (String) —

    the bytes to publish



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/acemq/amqp/codec.rb', line 70

module Codec
  # Whether an object can stand in as a codec.
  #
  # Checked when a connection is built rather than when the first message
  # arrives, so a codec missing a method is a mistake at start-up instead
  # of a mystery at three in the morning.
  #
  # @param candidate [Object]
  # @return [Boolean]
  def self.codec?(candidate)
    %i[content_type encode decode can_decode?].all? { |name| candidate.respond_to?(name) }
  end

  # @raise [ArgumentError] when +candidate+ is not a codec
  # @api private
  def self.check!(candidate)
    return candidate if codec?(candidate)

    raise ArgumentError,
          "#{candidate.inspect} is not a codec: it needs content_type, encode, " \
          "decode and can_decode?"
  end
end