Class: HTTPX::ContentType
- Inherits:
-
Object
- Object
- HTTPX::ContentType
- 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
-
#charset ⇒ Object
returns the charset declared in the header.
-
#initialize(header_value) ⇒ ContentType
constructor
A new instance of ContentType.
-
#mime_type ⇒ Object
returns the mime type declared in the header.
Constructor Details
permalink #initialize(header_value) ⇒ ContentType
Returns a new instance of ContentType.
204 205 206 |
# File 'lib/httpx/response.rb', line 204 def initialize(header_value) @header_value = header_value end |
Instance Method Details
permalink #charset ⇒ Object
returns the charset declared in the header.
ContentType.new("application/json; charset=utf-8").charset #=> "utf-8"
ContentType.new("text/plain").charset #=> nil
222 223 224 225 226 227 |
# File 'lib/httpx/response.rb', line 222 def charset return @charset if defined?(@charset) m = @header_value.to_s[CHARSET_RE, 1] m && @charset = m.strip.delete('"') end |
permalink #mime_type ⇒ Object
returns the mime type declared in the header.
ContentType.new("application/json; charset=utf-8").mime_type #=> "application/json"
211 212 213 214 215 216 |
# File 'lib/httpx/response.rb', line 211 def mime_type return @mime_type if defined?(@mime_type) m = @header_value.to_s[MIME_TYPE_RE, 1] m && @mime_type = m.strip.downcase end |