Sha256: bc2d2a100f6be4089e76d182d9cc8df295ccdd79512aaccef7fedd502e724257

Contents?: true

Size: 1.33 KB

Versions: 55

Compression:

Stored size: 1.33 KB

Contents

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.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?
        Hash[*content_type.split(SPLIT_PATTERN)[1..-1].
          collect { |s| s.split('=', 2) }.
          map { |k,v| [k.downcase, strip_doublequotes(v)] }.flatten]
      end

      private

        def strip_doublequotes(str)
          (str[0] == ?" && str[-1] == ?") ? str[1..-2] : str
        end
    end
  end
end

Version data entries

55 entries across 53 versions & 14 rubygems

Version Path
rack-2.0.9.4 lib/rack/media_type.rb
rack-2.0.9.3 lib/rack/media_type.rb
rack-2.0.9.2 lib/rack/media_type.rb
rack-2.0.9.1 lib/rack/media_type.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-2.0.5/lib/rack/media_type.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rack-2.0.8/lib/rack/media_type.rb
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/media_type.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/rack-2.0.6/lib/rack/media_type.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/media_type.rb
rack-2.0.9 lib/rack/media_type.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rack-2.0.8/lib/rack/media_type.rb
rack-2.0.8 lib/rack/media_type.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/media_type.rb
tdiary-5.1.0 vendor/bundle/gems/rack-2.0.7/lib/rack/media_type.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/media_type.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/rack/media_type.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/rack-2.0.7/lib/rack/media_type.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/media_type.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/media_type.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rack-2.0.7/lib/rack/media_type.rb