module Hanami::Action::Mime::ClassMethods

Public Instance Methods

format_to_mime_type(format) click to toggle source

@since 0.2.0 @api private

# File lib/hanami/action/mime.rb, line 126
def format_to_mime_type(format)
  configuration.mime_type_for(format) ||
    MIME_TYPES[format] or
    raise Hanami::Controller::UnknownFormatError.new(format)
end

Private Instance Methods

accept(*formats) click to toggle source

Restrict the access to the specified mime type symbols.

@param be converted into a mime type

@since 0.1.0

@see Hanami::Controller::Configuration#format

@example

require 'hanami/controller'

class Show
  include Hanami::Action
  accept :html, :json

  def call(params)
    # ...
  end
end

# When called with "*/*"              => 200
# When called with "text/html"        => 200
# When called with "application/json" => 200
# When called with "application/xml"  => 406
# File lib/hanami/action/mime.rb, line 161
def accept(*formats)
  mime_types = formats.map do |format|
    format_to_mime_type(format)
  end

  configuration.restrict_mime_types!(mime_types)

  before do
    unless mime_types.find {|mt| accept?(mt) }
      halt 406
    end
  end
end