Module: Lotus::Action::Mime

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

Overview

Mime type API

See Also:

  • ClassMethods#accept

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods

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
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

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



53
54
55
# File 'lib/lotus/action/mime.rb', line 53

def self.included(base)
  base.extend ClassMethods
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 'lotus/controller'

class Show
  include Lotus::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



242
243
244
# File 'lib/lotus/action/mime.rb', line 242

def charset
  @charset || default_charset || DEFAULT_CHARSET
end

#charset=(value) ⇒ String

Action charset setter, receives new charset value

Examples:

require 'lotus/controller'

class Show
  include Lotus::Action

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

Returns:

  • (String)

    the charset of the request.

Since:

  • 0.3.0



209
210
211
# File 'lib/lotus/action/mime.rb', line 209

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 #format=)
* Weighted value from Accept
* Default content type

To override the value, use #format=

Examples:

require 'lotus/controller'

class Show
  include Lotus::Action

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

Returns:

  • (String)

    the content type from the request.

See Also:

  • #format=
  • Configuration#default_request_format
  • #default_content_type
  • #DEFAULT_CONTENT_TYPE

Since:

  • 0.1.0



188
189
190
# File 'lib/lotus/action/mime.rb', line 188

def content_type
  @content_type || default_response_type || accepts || 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 explicitely set by ‘#format=`, it will return that value

Examples:

Default scenario

require 'lotus/controller'

class Show
  include Lotus::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 'lotus/controller'

class Show
  include Lotus::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



155
156
157
# File 'lib/lotus/action/mime.rb', line 155

def format
  @format ||= detect_format
end