Class: Mail::Encodings::TransferEncoding

Inherits:
Object
  • Object
show all
Defined in:
lib/mail/encodings/transfer_encoding.rb

Direct Known Subclasses

Identity, UnixToUnix

Constant Summary collapse

NAME =
''
PRIORITY =
-1

Class Method Summary collapse

Class Method Details

.can_encode?(enc) ⇒ Boolean

Override in subclasses to indicate that they can encode text that couldn’t be directly transported, e.g. Base64 has 7bit output, but it can encode binary.

Returns:

  • (Boolean)


19
20
21
# File 'lib/mail/encodings/transfer_encoding.rb', line 19

def self.can_encode?(enc)
  can_transport? enc
end

.can_transport?(enc) ⇒ Boolean

And encoding’s superclass can always transport it since the class hierarchy is arranged e.g. Base64 < 7bit < 8bit < Binary.

Returns:

  • (Boolean)


12
13
14
# File 'lib/mail/encodings/transfer_encoding.rb', line 12

def self.can_transport?(enc)
  enc && enc <= self
end

.compatible_input?(str) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/mail/encodings/transfer_encoding.rb', line 27

def self.compatible_input?(str)
  true
end

.cost(str) ⇒ Object



23
24
25
# File 'lib/mail/encodings/transfer_encoding.rb', line 23

def self.cost(str)
  raise "Unimplemented"
end

.lowest_cost(str, encodings) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mail/encodings/transfer_encoding.rb', line 56

def self.lowest_cost(str, encodings)
  best = nil
  best_cost = nil

  encodings.each do |enc|
    # If the current choice cannot be transported safely, give priority
    # to other choices but allow it to be used as a fallback.
    this_cost = enc.cost(str) if enc.compatible_input?(str)

    if !best_cost || (this_cost && this_cost < best_cost)
      best_cost = this_cost
      best = enc
    elsif this_cost == best_cost
      best = enc if enc::PRIORITY < best::PRIORITY
    end
  end

  best
end

.negotiate(message_encoding, source_encoding, str, allowed_encodings = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/mail/encodings/transfer_encoding.rb', line 35

def self.negotiate(message_encoding, source_encoding, str, allowed_encodings = nil)
  message_encoding = Encodings.get_encoding(message_encoding) || Encodings.get_encoding('8bit')
  source_encoding  = Encodings.get_encoding(source_encoding)

  if message_encoding && source_encoding && message_encoding.can_transport?(source_encoding) && source_encoding.compatible_input?(str)
    source_encoding
  else
    renegotiate(message_encoding, source_encoding, str, allowed_encodings)
  end
end

.renegotiate(message_encoding, source_encoding, str, allowed_encodings = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/mail/encodings/transfer_encoding.rb', line 46

def self.renegotiate(message_encoding, source_encoding, str, allowed_encodings = nil)
  encodings = Encodings.get_all.select do |enc|
    (allowed_encodings.nil? || allowed_encodings.include?(enc)) &&
      message_encoding.can_transport?(enc) &&
      enc.can_encode?(source_encoding)
  end

  lowest_cost(str, encodings)
end

.to_sObject



31
32
33
# File 'lib/mail/encodings/transfer_encoding.rb', line 31

def self.to_s
  self::NAME
end