Class: Protege::Gateway::Mail::MessageId

Inherits:
Object
  • Object
show all
Defined in:
lib/protege/gateway/mail/message_id.rb

Overview

Value object representing an RFC 5322 Message-ID.

Instances are frozen on construction; equality is based on the normalized string value. Construct via the factory methods — .parse, .synthetic, or .ensure — rather than .new.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ MessageId

Wrap a pre-normalized value and freeze. Prefer the factory methods.

Parameters:

  • value (String)

    an already-normalized, angle-bracketed message-id.



73
74
75
76
# File 'lib/protege/gateway/mail/message_id.rb', line 73

def initialize(value)
  @value = value
  freeze
end

Instance Attribute Details

#valueObject (readonly)

Normalized message-id string (always angle-bracketed).



68
69
70
# File 'lib/protege/gateway/mail/message_id.rb', line 68

def value
  @value
end

Class Method Details

.ensure(value) ⇒ MessageId

Parse value, or fall back to a synthetic id when it is missing.

Parameters:

  • value (String, nil)

    the raw message-id, possibly nil or blank.

Returns:

  • (MessageId)

    the parsed id, or a synthetic one when value is nil/blank.



47
48
49
50
51
# File 'lib/protege/gateway/mail/message_id.rb', line 47

def ensure(value)
  return synthetic if value.nil? || value.to_s.strip.empty?

  parse(value)
end

.parse(value) ⇒ MessageId

Parse a raw value into a normalized, angle-bracketed MessageId.

Trims surrounding whitespace and preserves case. Already-bracketed values pass through; half-bracketed values lose the stray bracket before being re-wrapped.

Parameters:

  • value (String)

    the raw message-id, with or without brackets.

Returns:



26
27
28
# File 'lib/protege/gateway/mail/message_id.rb', line 26

def parse(value)
  new(normalize(value))
end

.synthetic(domain: 'protege.local') ⇒ MessageId

Build a unique synthetic MessageId for inputs that lack one.

The domain should be the real sending domain for outbound mail, so the Message-ID is well-formed and recipients (e.g. Gmail) trust it and thread replies. It defaults to protege.local only for the inbound/console fallback case, where the id is internal and never travels back out.

Parameters:

  • domain (String) (defaults to: 'protege.local')

    the domain for the id's right-hand side.

Returns:

  • (MessageId)

    a fresh <synthetic.UUID@domain> id.



39
40
41
# File 'lib/protege/gateway/mail/message_id.rb', line 39

def synthetic(domain: 'protege.local')
  new("<synthetic.#{SecureRandom.uuid}@#{domain}>")
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare by normalized value.

Parameters:

  • other (Object)

    the value to compare against.

Returns:

  • (Boolean)

    true when other is a MessageId with the same value.



82
83
84
# File 'lib/protege/gateway/mail/message_id.rb', line 82

def ==(other)
  other.is_a?(MessageId) && @value == other.value
end

#hashInteger

Hash by normalized value so a MessageId works as a Hash key.

Returns:

  • (Integer)

    the hash code.



91
92
93
# File 'lib/protege/gateway/mail/message_id.rb', line 91

def hash
  @value.hash
end

#to_sString

Return the normalized string value.

Returns:

  • (String)

    the angle-bracketed message-id.



98
99
100
# File 'lib/protege/gateway/mail/message_id.rb', line 98

def to_s
  @value
end