Sha256: b7d58b1a616eb2b392199dc043f2c836787e0997b6e52c91e1d5d345a008f9c0

Contents?: true

Size: 1.25 KB

Versions: 25

Compression:

Stored size: 1.25 KB

Contents

# encoding: utf-8
module Mail

  # 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
  class ParameterHash < HashWithIndifferentAccess

    def [](key_name)
      pairs = select { |k,v| k =~ /^#{key_name}\*/ }
      pairs = pairs.to_a if RUBY_VERSION >= '1.9'
      if pairs.empty? # Just dealing with a single value pair
        super(key_name)
      else # Dealing with a multiple value pair or a single encoded value pair
        string = pairs.sort { |a,b| a.first <=> b.first }.map { |v| v.last }.join('')
        if mt = string.match(/([\w\d\-]+)'(\w\w)'(.*)/)
          string = mt[3]
          encoding = mt[1]
        else
          encoding = nil
        end
        Mail::Encodings.param_decode(string, encoding)
      end
    end

    def encoded
      map.sort { |a,b| a.first <=> b.first }.map do |key_name, value|
        unless value.ascii_only?
          value = Mail::Encodings.param_encode(value)
          key_name = "#{key_name}*"
        end
        %Q{#{key_name}="#{value}"}
      end.join(";\r\n\t")
    end

  end
end

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
mail-2.1.3 lib/mail/fields/common/parameter_hash.rb
kbaum-mail-2.1.2.1 lib/mail/fields/common/parameter_hash.rb
mail-2.1.2 lib/mail/fields/common/parameter_hash.rb
mail-2.1.1 lib/mail/fields/common/parameter_hash.rb
mail-2.1.0 lib/mail/fields/common/parameter_hash.rb
mail-2.0.5 lib/mail/fields/common/parameter_hash.rb
mail-2.0.3 lib/mail/fields/common/parameter_hash.rb
mail-1.6.0 lib/mail/fields/common/parameter_hash.rb
mail-1.5.4 lib/mail/fields/common/parameter_hash.rb
mail-1.5.3 lib/mail/fields/common/parameter_hash.rb
mail-1.5.2 lib/mail/fields/common/parameter_hash.rb
mail-1.5.1 lib/mail/fields/common/parameter_hash.rb
mail-1.5.0 lib/mail/fields/common/parameter_hash.rb
mail-1.4.3 lib/mail/fields/common/parameter_hash.rb
mail-1.4.2 lib/mail/fields/common/parameter_hash.rb
mail-1.4.1 lib/mail/fields/common/parameter_hash.rb
mail-1.4.0 lib/mail/fields/common/parameter_hash.rb
mail-1.3.5 lib/mail/fields/common/parameter_hash.rb
mail-1.3.4 lib/mail/fields/common/parameter_hash.rb
mail-1.3.3 lib/mail/fields/common/parameter_hash.rb