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
#initialize(header_value) ⇒ ContentType
Returns a new instance of ContentType.
185 186 187 |
# File 'lib/httpx/response.rb', line 185 def initialize(header_value) @header_value = header_value end |
Instance Method Details
#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
203 204 205 206 207 208 |
# File 'lib/httpx/response.rb', line 203 def charset return @charset if defined?(@charset) m = @header_value.to_s[CHARSET_RE, 1] m && @charset = m.strip.delete('"') end |
#mime_type ⇒ Object
returns the mime type declared in the header.
ContentType.new("application/json; charset=utf-8").mime_type #=> "application/json"
192 193 194 195 196 197 |
# File 'lib/httpx/response.rb', line 192 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 |