Module: Hanami::Action::Mime
- Defined in:
- lib/hanami/action/mime.rb
Overview
Mime type API
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
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
- 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
110 111 112 113 114 115 |
# File 'lib/hanami/action/mime.rb', line 110 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=
327 328 329 |
# File 'lib/hanami/action/mime.rb', line 327 def charset @charset || default_charset || DEFAULT_CHARSET end |
#charset=(value) ⇒ String
Action charset setter, receives new charset value
294 295 296 |
# File 'lib/hanami/action/mime.rb', line 294 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=
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/hanami/action/mime.rb', line 266 def content_type return @content_type unless @content_type.nil? if accept_header? type = content_type_from_accept_header return type if type end 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
229 230 231 |
# File 'lib/hanami/action/mime.rb', line 229 def format @format ||= detect_format end |