Module: Hanami::Action::Mime

Defined in:
lib/hanami/action/mime.rb

Overview

Mime type API

See Also:

  • ClassMethods#accept

Since:

  • 0.1.0

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

Since:

  • 0.1.0

'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

Since:

  • 1.2.0

'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

Since:

  • 0.1.0

'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

Since:

  • 0.1.0

'*/*'.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

Since:

  • 0.1.0

'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

Since:

  • 0.3.0

'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

Since:

  • 1.0.0

{
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

Instance Method Summary collapse

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

Parameters:

  • base (Class)

    the target action

See Also:

Since:

  • 0.1.0


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

#charsetString

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=

Examples:

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
    # ...
    charset # => 'text/html'
  end
end

Returns:

  • (String)

    the charset of the request.

See Also:

  • #charset=
  • Configuration#default_charset
  • #default_charset
  • #DEFAULT_CHARSET

Since:

  • 0.3.0


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

Examples:

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
    # ...
    self.charset = 'koi8-r'
  end
end

Returns:

  • (String)

    the charset of the request.

Since:

  • 0.3.0


327
328
329
# File 'lib/hanami/action/mime.rb', line 327

def charset=(value)
  @charset = value
end

#content_typeString

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=

Examples:

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
    # ...
    content_type # => 'text/html'
  end
end

Returns:

  • (String)

    the content type from the request.

See Also:

Since:

  • 0.1.0


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

#formatSymbol

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

Examples:

Default scenario

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
  end
end

action = Show.new

_, headers, _ = action.call({ 'HTTP_ACCEPT' => 'text/html' })
headers['Content-Type'] # => 'text/html'
action.format           # => :html

Set value

require 'hanami/controller'

class Show
  include Hanami::Action

  def call(params)
    self.format = :xml
  end
end

action = Show.new

_, headers, _ = action.call({ 'HTTP_ACCEPT' => 'text/html' })
headers['Content-Type'] # => 'application/xml'
action.format           # => :xml

Returns:

  • (Symbol)

    a symbol that corresponds to the content type

See Also:

Since:

  • 0.2.0


267
268
269
# File 'lib/hanami/action/mime.rb', line 267

def format
  @format ||= detect_format
end