Sha256: b052aab25ae1d1ee50767517dc7981f81f7b73efed245f1c14268e8f7511dbcf

Contents?: true

Size: 700 Bytes

Versions: 15

Compression:

Stored size: 700 Bytes

Contents

# frozen_string_literal: true

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

    attr_accessor :mime_type, :charset

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

      private

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

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

    def initialize(mime_type = nil, charset = nil)
      @mime_type = mime_type
      @charset   = charset
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/content_type.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/content_type.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/content_type.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/http-5.2.0/lib/http/content_type.rb
direct7-0.0.13 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/content_type.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/content_type.rb
http-5.2.0 lib/http/content_type.rb
direct7-0.0.11 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/lib/http/content_type.rb
http-5.1.1 lib/http/content_type.rb
http-5.1.0 lib/http/content_type.rb
http-5.0.4 lib/http/content_type.rb
http-5.0.3 lib/http/content_type.rb
http-5.0.2 lib/http/content_type.rb
http-5.0.1 lib/http/content_type.rb
http-5.0.0 lib/http/content_type.rb