Module: Hanami::Action::Mime
- Defined in:
- lib/hanami/action/mime.rb
Overview
Mime type API
Defined Under Namespace
Modules: ClassMethods, InstanceMethods Classes: RequestMimeWeight
Constant Summary collapse
- HTTP_ACCEPT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The key that returns accepted mime types from the Rack env
'HTTP_ACCEPT'.freeze
- HTTP_CONTENT_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The key that returns content mime type from the Rack env
'CONTENT_TYPE'.freeze
- CONTENT_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The header key to set the mime type of the response
'Content-Type'.freeze
- DEFAULT_ACCEPT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The default mime type for an incoming HTTP request
'*/*'.freeze
- DEFAULT_CONTENT_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The default mime type that is returned in the response
'application/octet-stream'.freeze
- DEFAULT_CHARSET =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The default charset that is returned in the response
'utf-8'.freeze
- MIME_TYPES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Most commom MIME Types used for responses
{ txt: 'text/plain', html: 'text/html', json: 'application/json', manifest: 'text/cache-manifest', atom: 'application/atom+xml', avi: 'video/x-msvideo', bmp: 'image/bmp', bz: 'application/x-bzip', bz2: 'application/x-bzip2', chm: 'application/vnd.ms-htmlhelp', css: 'text/css', csv: 'text/csv', flv: 'video/x-flv', gif: 'image/gif', gz: 'application/x-gzip', h264: 'video/h264', ico: 'image/vnd.microsoft.icon', ics: 'text/calendar', jpg: 'image/jpeg', js: 'application/javascript', mp4: 'video/mp4', mov: 'video/quicktime', mp3: 'audio/mpeg', mp4a: 'audio/mp4', mpg: 'video/mpeg', oga: 'audio/ogg', ogg: 'application/ogg', ogv: 'video/ogg', pdf: 'application/pdf', pgp: 'application/pgp-encrypted', png: 'image/png', psd: 'image/vnd.adobe.photoshop', rtf: 'application/rtf', sh: 'application/x-sh', svg: 'image/svg+xml', swf: 'application/x-shockwave-flash', tar: 'application/x-tar', torrent: 'application/x-bittorrent', tsv: 'text/tab-separated-values', uri: 'text/uri-list', vcs: 'text/x-vcalendar', wav: 'audio/x-wav', webm: 'video/webm', wmv: 'video/x-ms-wmv', woff: 'application/font-woff', woff2: 'application/font-woff2', wsdl: 'application/wsdl+xml', xhtml: 'application/xhtml+xml', xml: 'application/xml', xslt: 'application/xslt+xml', yml: 'text/yaml', zip: 'application/zip' }.freeze
Class Method Summary collapse
-
.included(base) ⇒ Object
private
Override Ruby’s hook for modules.
Instance Method Summary collapse
-
#charset ⇒ String
The charset that will be automatically set in the response.
-
#charset=(value) ⇒ String
Action charset setter, receives new charset value.
-
#content_type ⇒ String
The content type that will be automatically set in the response.
-
#format ⇒ Symbol
Returns a symbol representation of the content type.
Class Method Details
.included(base) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Override Ruby’s hook for modules. It includes Mime types logic
116 117 118 119 120 121 |
# File 'lib/hanami/action/mime.rb', line 116 def self.included(base) base.class_eval do extend ClassMethods prepend InstanceMethods end end |
Instance Method Details
#charset ⇒ String
The charset that will be automatically set in the response.
It prefers, in order:
* Explicit set value (see #charset=)
* Default configuration charset
* Default content type
To override the value, use #charset=
360 361 362 |
# File 'lib/hanami/action/mime.rb', line 360 def charset @charset || default_charset || DEFAULT_CHARSET end |
#charset=(value) ⇒ String
Action charset setter, receives new charset value
327 328 329 |
# File 'lib/hanami/action/mime.rb', line 327 def charset=(value) @charset = value end |
#content_type ⇒ String
The content type that will be automatically set in the response.
It prefers, in order:
* Explicit set value (see Hanami::Action::Mime#format=)
* Weighted value from Accept header based on all known MIME Types:
- Custom registered MIME Types (see Hanami::Controller::Configuration#format)
* Configured default content type (see Hanami::Controller::Configuration#default_response_format)
* Hard-coded default content type (see Hanami::Action::Mime::DEFAULT_CONTENT_TYPE)
To override the value, use #format=
304 305 306 307 308 |
# File 'lib/hanami/action/mime.rb', line 304 def content_type return @content_type unless @content_type.nil? @content_type = content_type_from_accept_header if accept_header? @content_type || default_response_type || default_content_type || DEFAULT_CONTENT_TYPE end |
#format ⇒ Symbol
Returns a symbol representation of the content type.
The framework automatically detects the request mime type, and returns the corresponding format.
However, if this value was explicitly set by ‘#format=`, it will return that value
267 268 269 |
# File 'lib/hanami/action/mime.rb', line 267 def format @format ||= detect_format end |