Sha256: f40aa8f55e6b9e513a849e7d3da645caf20210687855766076f062b24015ef74

Contents?: true

Size: 596 Bytes

Versions: 19

Compression:

Stored size: 596 Bytes

Contents

# frozen_string_literal: true

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)
        m = str.to_s[MIME_TYPE_RE, 1]
        m && m.strip.downcase
      end

      # :nodoc:
      def charset(str)
        m = str.to_s[CHARSET_RE, 1]
        m && m.strip.delete('"')
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
http-4.4.1 lib/http/content_type.rb
http-4.4.0 lib/http/content_type.rb
http-4.3.0 lib/http/content_type.rb
http-4.2.0 lib/http/content_type.rb
http-4.1.1 lib/http/content_type.rb
http-4.1.0 lib/http/content_type.rb
http-4.0.5 lib/http/content_type.rb
http-4.0.4 lib/http/content_type.rb
http-5.0.0.pre lib/http/content_type.rb
http-4.0.3 lib/http/content_type.rb
http-4.0.2 lib/http/content_type.rb
http-4.0.1 lib/http/content_type.rb
http-4.0.0 lib/http/content_type.rb
http-3.3.0 lib/http/content_type.rb
http-3.2.1 lib/http/content_type.rb
http-3.2.0 lib/http/content_type.rb
http-3.1.0 lib/http/content_type.rb
http-3.0.0 lib/http/content_type.rb
http-3.0.0.pre lib/http/content_type.rb