Class: Mail::Jenc::RFC2231Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/mail/jenc/rfc2231_encoder.rb

Class Method Summary collapse

Class Method Details

.encode(name, charset, **options) ⇒ Object



5
6
7
# File 'lib/mail/jenc/rfc2231_encoder.rb', line 5

def encode(name, charset, **options)
  encode_to_hash(name, charset, **options).map { |k, v| "#{k}=#{v}" }.join(";\r\n\s")
end

.encode_to_hash(name, charset, key: 'filename') ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mail/jenc/rfc2231_encoder.rb', line 9

def encode_to_hash(name, charset, key: 'filename')
  hexes = PercentEncoder.encode_to_array(
    Mail::Encodings.transcode_charset(name, name.encoding, charset)
  )

  first_hex_num = hex_num(charset.size + key.size + 3)
  if hexes.size <= first_hex_num
    params = { "#{key}*" => "#{charset.downcase}''#{hexes.join}" }
  else
    params = { "#{key}*0*" => "#{charset.downcase}''#{hexes.shift(first_hex_num).join}" }
    slices = hexes.each_slice(hex_num(key.size + 3))
    slices.each_with_index do |sliced, i|
      kc = "#{key}*#{i+1}*"
      params[kc] = sliced.join
    end
  end

  params
end