Sha256: ad1d0c56967a430152b70ee2dc4364ad08c178d63852a1509bb3395f36c1d8b8

Contents?: true

Size: 1.48 KB

Versions: 18

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

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

  class MediaType
    SPLIT_PATTERN = /[;,]/

    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
        if type = content_type.split(SPLIT_PATTERN, 2).first
          type.rstrip!
          type.downcase!
          type
        end
      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|
          s.strip!
          k, v = s.split('=', 2)
          k.downcase!
          hsh[k] = 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

18 entries across 18 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rack-3.1.7/lib/rack/media_type.rb
rack-3.1.8 lib/rack/media_type.rb
rack-2.2.10 lib/rack/media_type.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rack-2.2.9/lib/rack/media_type.rb
rack-3.1.7 lib/rack/media_type.rb
rack-3.1.6 lib/rack/media_type.rb
rack-3.1.5 lib/rack/media_type.rb
rack-3.1.4 lib/rack/media_type.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rack-3.1.3/lib/rack/media_type.rb
rack-3.1.3 lib/rack/media_type.rb
rack-3.1.2 lib/rack/media_type.rb
rack-3.1.1 lib/rack/media_type.rb
rack-3.1.0 lib/rack/media_type.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/rack-3.0.11/lib/rack/media_type.rb
rack-2.2.9 lib/rack/media_type.rb
rack-3.0.10 lib/rack/media_type.rb
rack-3.0.9.1 lib/rack/media_type.rb
rack-2.2.8.1 lib/rack/media_type.rb