Class: Mail::ParameterHash

Inherits:
IndifferentHash
  • Object
show all
Defined in:
lib/mail/jdec/fields/parameter_hash_patch.rb

Overview

ParameterHash is an intelligent Hash that allows you to add parameter values including the MIME extension paramaters that have the name*0=“blah”, name*1=“bleh” keys, and will just return a single key called name=“blahbleh” and do any required un-encoding to make that happen

Parameters are defined in RFC2045. Split keys are in RFC2231.

Instance Method Summary collapse

Instance Method Details

#[](key_name) ⇒ Object

:nodoc:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mail/jdec/fields/parameter_hash_patch.rb', line 16

def [](key_name)
  key_pattern = Regexp.escape(key_name.to_s)
  pairs = []
  exact = nil

  each do |k,v|
    if k =~ /^#{key_pattern}(\*|$)/i
      if $1 == Constants::ASTERISK
        pairs << [k, v]
      else
        exact = k
      end
    end
  end

  if pairs.empty? # Just dealing with a single value pair
    super(exact || key_name)
  else # Dealing with a multiple value pair or a single encoded value pair
    string = pairs.sort_by { |a| a.first.to_s =~ /^([^*]+)\*(\d+)/ ? [$1, $2.to_i] : [a.first.to_s, 0] }.map { |v| v.last }.join('')
    if mt = string.match(/([\w\-]+)?'(\w\w)?'(.*)/)
      string = mt[3]
      encoding = mt[1]
    else
      encoding = nil
    end
    Mail::Encodings.param_decode(string, encoding)
  end
end

#decodedObject



55
56
57
58
59
# File 'lib/mail/jdec/fields/parameter_hash_patch.rb', line 55

def decoded
  map.sort_by { |a| a.first.to_s =~ /^([^*]+)\*(\d+)/ ? [$1, $2.to_i] : [a.first.to_s, 0] }.map! do |key_name, value|
    %Q{#{key_name}=#{Utilities.quote_token(value)}}
  end.join("; ")
end

#encodedObject



45
46
47
48
49
50
51
52
53
# File 'lib/mail/jdec/fields/parameter_hash_patch.rb', line 45

def encoded
  map.sort_by { |a| a.first.to_s =~ /^([^*]+)\*(\d+)/ ? [$1, $2.to_i] : [a.first.to_s, 0] }.map! do |key_name, value|
    unless value.ascii_only?
      value = Mail::Encodings.param_encode(value)
      key_name = "#{key_name}*"
    end
    %Q{#{key_name}=#{Utilities.quote_token(value)}}
  end.join(";\r\n\s")
end