Sha256: 91c6c930c2359bc01279bcc47de49cb3e65c8ba392128a269f8b48e06c147e94

Contents?: true

Size: 1.7 KB

Versions: 130

Compression:

Stored size: 1.7 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
  # Parameters are defined in RFC2045, split keys are in RFC2231

  class ParameterHash < IndifferentHash

    include Mail::Utilities

    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 == '*'
            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

    def encoded
      map.sort { |a,b| a.first.to_s <=> b.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

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

Version data entries

130 entries across 118 versions & 14 rubygems

Version Path
mail-2.5.5 lib/mail/fields/common/parameter_hash.rb
mail-2.5.5.rc1 lib/mail/fields/common/parameter_hash.rb
classiccms-0.7.5 vendor/bundle/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
classiccms-0.7.4 vendor/bundle/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
classiccms-0.7.3 vendor/bundle/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
mail-portertech-2.6.2.edge lib/mail/fields/common/parameter_hash.rb
mail-2.6.1 lib/mail/fields/common/parameter_hash.rb
mail-2.6.0 lib/mail/fields/common/parameter_hash.rb
challah-1.0.0 vendor/bundle/gems/mail-2.5.4/lib/mail/fields/common/parameter_hash.rb
classiccms-0.7.2 vendor/bundle/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
classiccms-0.7.1 vendor/bundle/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
swipe-rails-0.0.5 vendor/bundle/gems/mail-2.5.4/lib/mail/fields/common/parameter_hash.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
classiccms-0.7.0 vendor/bundle/gems/mail-2.4.4/lib/mail/fields/common/parameter_hash.rb
mail-2.5.4 lib/mail/fields/common/parameter_hash.rb