Module: Lotus::Action::Mime

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

Overview

Mime type API

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

HTTP_ACCEPT =

The key that returns accepted mime types from the Rack env

Since:

  • 0.1.0

'HTTP_ACCEPT'.freeze
CONTENT_TYPE =

The header key to set the mime type of the response

Since:

  • 0.1.0

'Content-Type'.freeze
DEFAULT_ACCEPT =

The default mime type for an incoming HTTP request

Since:

  • 0.1.0

'*/*'.freeze
DEFAULT_CONTENT_TYPE =

The default mime type that is returned in the response

Since:

  • 0.1.0

'application/octet-stream'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Since:

  • 0.1.0



29
30
31
32
33
# File 'lib/lotus/action/mime.rb', line 29

def self.included(base)
  base.class_eval do
    extend ClassMethods
  end
end

Instance Method Details

#accept?(mime_type) ⇒ Boolean (protected)

Match the given mime type with the Accept header

Examples:

require 'lotus/controller'

class Show
  include Lotus::Action

  def call(params)
    # ...
    # @_env['HTTP_ACCEPT'] # => 'text/html,application/xhtml+xml,application/xml;q=0.9'

    accept?('text/html')        # => true
    accept?('application/xml')  # => true
    accept?('application/json') # => false

    # @_env['HTTP_ACCEPT'] # => '*/*'

    accept?('text/html')        # => true
    accept?('application/xml')  # => true
    accept?('application/json') # => true
  end
end

Returns:

  • (Boolean)

    true if the given mime type matches Accept

Since:

  • 0.1.0



172
173
174
175
176
# File 'lib/lotus/action/mime.rb', line 172

def accept?(mime_type)
  !!::Rack::Utils.q_values(accept).find do |mime, _|
    ::Rack::Mime.match?(mime_type, mime)
  end
end

#content_typeString (protected)

The content type that will be automatically set in the response.

It prefers, in order:

* Explicit set value (see #content_type=)
* Weighted value from Accept
* Default content type

To override the value, use #content_type=

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:

Since:

  • 0.1.0



139
140
141
# File 'lib/lotus/action/mime.rb', line 139

def content_type
  @content_type || accepts || DEFAULT_CONTENT_TYPE
end

#content_type=(content_type) ⇒ void (protected)

This method returns an undefined value.

Sets the given content type

Lotus::Action sets the proper content type automatically, this method

is designed to override that value.

Examples:

require 'lotus/controller'

class Show
  include Lotus::Action

  def call(params)
    # ...
    self.content_type = 'application/json'
  end
end

Parameters:

  • content_type (String)

    the content type

See Also:

Since:

  • 0.1.0



108
109
110
# File 'lib/lotus/action/mime.rb', line 108

def content_type=(content_type)
  @content_type = content_type
end

#finishObject (protected)

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.

Finalize the response by setting the current content type

See Also:

Since:

  • 0.1.0



80
81
82
83
# File 'lib/lotus/action/mime.rb', line 80

def finish
  super
  headers.merge! CONTENT_TYPE => content_type
end