Sha256: af1c82a7d22614b6db1b6d40113862c2548842b3b57c4a56afb9ef1ef54e2d18

Contents?: true

Size: 1.4 KB

Versions: 23

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Rack
  # Rack::MediaType parse media type and parameters out of content_type string

  class MediaType
    SPLIT_PATTERN = %r{\s*[;,]\s*}

    class << self
      # The media type (type/subtype) portion of the CONTENT_TYPE header
      # without any media type parameters. e.g., when CONTENT_TYPE is
      # "text/plain;charset=utf-8", the media-type is "text/plain".
      #
      # For more information on the use of media types in HTTP, see:
      # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
      def type(content_type)
        return nil unless content_type
        content_type.split(SPLIT_PATTERN, 2).first.tap(&:downcase!)
      end

      # The media type parameters provided in CONTENT_TYPE as a Hash, or
      # an empty Hash if no CONTENT_TYPE or media-type parameters were
      # provided.  e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
      # this method responds with the following Hash:
      #   { 'charset' => 'utf-8' }
      def params(content_type)
        return {} if content_type.nil?

        content_type.split(SPLIT_PATTERN)[1..-1].each_with_object({}) do |s, hsh|
          k, v = s.split('=', 2)

          hsh[k.tap(&:downcase!)] = strip_doublequotes(v)
        end
      end

      private

        def strip_doublequotes(str)
          (str.start_with?('"') && str.end_with?('"')) ? str[1..-2] : str
        end
    end
  end
end

Version data entries

23 entries across 22 versions & 5 rubygems

Version Path
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rack-3.0.9/lib/rack/media_type.rb
rack-3.0.9 lib/rack/media_type.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rack-3.0.8/lib/rack/media_type.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rack-3.0.8/lib/rack/media_type.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rack-3.0.7/lib/rack/media_type.rb
rack-3.0.8 lib/rack/media_type.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rack-3.0.7/lib/rack/media_type.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/rack-3.0.7/lib/rack/media_type.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/rack-3.0.7/lib/rack/media_type.rb
rack-3.0.7 lib/rack/media_type.rb
rack-3.0.6.1 lib/rack/media_type.rb
rack-3.0.6 lib/rack/media_type.rb
rack-3.0.5 lib/rack/media_type.rb
rack-3.0.4.2 lib/rack/media_type.rb
rack-3.0.4.1 lib/rack/media_type.rb
rack-3.0.4 lib/rack/media_type.rb
rack-3.0.3 lib/rack/media_type.rb
rack-3.0.2 lib/rack/media_type.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/rack-3.0.1/lib/rack/media_type.rb
rack-3.0.1 lib/rack/media_type.rb