class HTTPX::ContentType

  1. lib/httpx/response.rb
Superclass: Object

Helper class which decodes the HTTP “content-type” header.

Methods

Public Class

  1. new

Public Instance

  1. charset
  2. mime_type

Constants

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

Public Class methods

new(header_value)
[show source]
    # File lib/httpx/response.rb
199 def initialize(header_value)
200   @header_value = header_value
201 end

Public Instance methods

charset()

returns the charset declared in the header.

ContentType.new("application/json; charset=utf-8").charset #=> "utf-8"
ContentType.new("text/plain").charset #=> nil
[show source]
    # File lib/httpx/response.rb
217 def charset
218   return @charset if defined?(@charset)
219 
220   m = @header_value.to_s[CHARSET_RE, 1]
221   m && @charset = m.strip.delete('"')
222 end
mime_type()

returns the mime type declared in the header.

ContentType.new("application/json; charset=utf-8").mime_type #=> "application/json"
[show source]
    # File lib/httpx/response.rb
206 def mime_type
207   return @mime_type if defined?(@mime_type)
208 
209   m = @header_value.to_s[MIME_TYPE_RE, 1]
210   m && @mime_type = m.strip.downcase
211 end