Sha256: a8f3bac3ca354cc105ef4638da7ef92f24a6b2f8a676f764f0b7077d606cc52b

Contents?: true

Size: 1.27 KB

Versions: 24

Compression:

Stored size: 1.27 KB

Contents

module JSONAPIonify::Api
  class Server::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

24 entries across 24 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.9 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.8 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.7 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.5 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.4 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.3 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.2 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.1 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.12.0 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.11.11 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.11.10 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.11.9 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.11.8 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.11.7 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.11.6 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.11.5 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.11.0 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.10.2 lib/jsonapionify/api/server/media_type.rb
jsonapionify-0.10.1 lib/jsonapionify/api/server/media_type.rb