Sha256: 53983733a8532bd2938ceec3989557ba0992954ef7266fc807d9f9a6df1fa0f3

Contents?: true

Size: 1.81 KB

Versions: 16

Compression:

Stored size: 1.81 KB

Contents

# encoding: utf-8
# frozen_string_literal: true
require 'mail/constants'
require 'mail/indifferent_hash'
require 'mail/encodings'

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 #:nodoc:
    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 { |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_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}=#{Utilities.quote_token(value)}}
      end.join(";\r\n\s")
    end

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

Version data entries

16 entries across 15 versions & 6 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/mail-2.8.1/lib/mail/fields/parameter_hash.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/parameter_hash.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/mail-2.8.1/lib/mail/fields/parameter_hash.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/mail-2.8.1/lib/mail/fields/parameter_hash.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/parameter_hash.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/parameter_hash.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/parameter_hash.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/mail-2.8.1/lib/mail/fields/parameter_hash.rb
mail-2.8.1 lib/mail/fields/parameter_hash.rb
mail-2.8.1.rc2 lib/mail/fields/parameter_hash.rb
mail-2.8.0.1 lib/mail/fields/parameter_hash.rb
mail-2.8.0.1.rc1 lib/mail/fields/parameter_hash.rb
mail-2.8.0 lib/mail/fields/parameter_hash.rb
mail-2.8.0.rc3 lib/mail/fields/parameter_hash.rb
mail-2.8.0.rc2 lib/mail/fields/parameter_hash.rb
mail-2.8.0.rc1 lib/mail/fields/parameter_hash.rb