Class: HTTPX::ContentType

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/response.rb

Overview

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(header_value) ⇒ ContentType

Returns a new instance of ContentType.



212
213
214
215
216
# File 'lib/httpx/response.rb', line 212

def initialize(header_value)
  @header_value = header_value
  @mime_type = @charset = nil
  @initialized = false
end

Instance Method Details

#charsetObject

returns the charset declared in the header.

ContentType.new("application/json; charset=utf-8").charset #=> "utf-8"
ContentType.new("text/plain").charset #=> nil


233
234
235
236
237
238
239
# File 'lib/httpx/response.rb', line 233

def charset
  return @charset if @initialized

  load

  @charset
end

#mime_typeObject

returns the mime type declared in the header.

ContentType.new("application/json; charset=utf-8").mime_type #=> "application/json"


221
222
223
224
225
226
227
# File 'lib/httpx/response.rb', line 221

def mime_type
  return @mime_type if @initialized

  load

  @mime_type
end