module Mail::Jenc::RFC2231Encoder

Public Class Methods

encode(name, options = {}) click to toggle source
# File lib/mail/jenc/encoder.rb, line 17
def encode(name, options = {})
  encode_to_hash(name, **options).map { |k, v| "#{k}=#{v}" }.join(";\r\n\s")
end
encode_to_hash(name, key: 'filename', charset: 'utf-8') click to toggle source
# File lib/mail/jenc/encoder.rb, line 21
def encode_to_hash(name, key: 'filename', charset: 'utf-8')
  hexes = name.unpack('H*')[0].scan(/.{2}/).map { |hex| "%#{hex.upcase}" }

  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

Private Class Methods

hex_num(size) click to toggle source
# File lib/mail/jenc/encoder.rb, line 41
def hex_num(size)
  ((80 - 4 - size).to_f / 3).to_i
end