Sha256: d71f8765e6e25f23c4b0db6994996cb9027adaa0e19b884225dfe76cc7662564

Contents?: true

Size: 595 Bytes

Versions: 11

Compression:

Stored size: 595 Bytes

Contents

module HTTP
  ContentType = Struct.new(:mime_type, :charset) do
    MIME_TYPE_RE = %r{^([^/]+/[^;]+)(?:$|;)}
    CHARSET_RE   = /;\s*charset=([^;]+)/i

    class << self
      # Parse string and return ContentType struct
      def parse(str)
        new mime_type(str), charset(str)
      end

    private

      # :nodoc:
      def mime_type(str)
        md = str.to_s.match MIME_TYPE_RE
        md && md[1].to_s.strip.downcase
      end

      # :nodoc:
      def charset(str)
        md = str.to_s.match CHARSET_RE
        md && md[1].to_s.strip.gsub(/^"|"$/, '')
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
http-0.7.4 lib/http/content_type.rb
http-0.6.4 lib/http/content_type.rb
http-0.7.3 lib/http/content_type.rb
http-0.7.2 lib/http/content_type.rb
http-0.7.1 lib/http/content_type.rb
http-0.7.0 lib/http/content_type.rb
http-0.6.3 lib/http/content_type.rb
http-0.6.2 lib/http/content_type.rb
http-0.6.1 lib/http/content_type.rb
http-0.6.0 lib/http/content_type.rb
http-0.6.0.pre lib/http/content_type.rb