Class: Mail::ParameterHash

Inherits:
IndifferentHash show all
Includes:
Utilities
Defined in:
lib/mail/fields/common/parameter_hash.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

Constant Summary

Constant Summary

Constants included from Constants

Constants::ASTERISK, Constants::ATOM_UNSAFE, Constants::B_VALUES, Constants::CAPITAL_M, Constants::COLON, Constants::CONTROL_CHAR, Constants::CR, Constants::CRLF, Constants::CR_ENCODED, Constants::EMPTY, Constants::ENCODED_VALUE, Constants::EQUAL_LF, Constants::FIELD_BODY, Constants::FIELD_LINE, Constants::FIELD_NAME, Constants::FIELD_PREFIX, Constants::FIELD_SPLIT, Constants::FWS, Constants::HEADER_LINE, Constants::HEADER_SPLIT, Constants::HYPHEN, Constants::LF, Constants::LF_ENCODED, Constants::NULL_SENDER, Constants::PHRASE_UNSAFE, Constants::QP_SAFE, Constants::QP_UNSAFE, Constants::Q_VALUES, Constants::SPACE, Constants::TEXT, Constants::TOKEN_UNSAFE, Constants::UNDERSCORE, Constants::WSP

Instance Method Summary collapse

Methods included from Utilities

#atom_safe?, #bracket, #capitalize_field, #constantize, #dasherize, #dquote, #escape_paren, #map_lines, #map_with_index, #match_to_s, #paren, #quote_atom, #quote_phrase, #quote_token, #token_safe?, #unbracket, #underscoreize, #unparen, #unquote, #uri_escape, #uri_parser, #uri_unescape

Methods inherited from IndifferentHash

#[]=, #default, #delete, #dup, #fetch, #initialize, #key?, #merge, new_from_hash_copying_default, #regular_update, #regular_writer, #reverse_merge, #reverse_merge!, #stringify_keys, #stringify_keys!, #symbolize_keys, #to_hash, #to_options!, #update, #values_at

Constructor Details

This class inherits a constructor from Mail::IndifferentHash

Instance Method Details

#[](key_name) ⇒ Object



15
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
# File 'lib/mail/fields/common/parameter_hash.rb', line 15

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 == 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 { |a,b| a.first.to_s <=> b.first.to_s }.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



52
53
54
55
56
# File 'lib/mail/fields/common/parameter_hash.rb', line 52

def decoded
  map.sort_by { |a| a.first.to_s }.map! do |key_name, value|
    %Q{#{key_name}=#{quote_token(value)}}
  end.join("; ")
end

#encodedObject



42
43
44
45
46
47
48
49
50
# File 'lib/mail/fields/common/parameter_hash.rb', line 42

def encoded
  map.sort_by { |a| a.first.to_s }.map! do |key_name, value|
    unless value.ascii_only?
      value = Mail::Encodings.param_encode(value)
      key_name = "#{key_name}*"
    end
    %Q{#{key_name}=#{quote_token(value)}}
  end.join(";\r\n\s")
end