Module: Roda::RodaPlugins::ResponseContentType

Defined in:
lib/roda/plugins/response_content_type.rb

Overview

The response_content_type extension adds response.content_type and response.content_type= methods for getting and setting the response content-type.

When setting the content-type, you can pass either a string, which is used directly:

response.content_type = "text/html"

Or, if you have registered mime types when loading the plugin:

plugin :response_content_type, mime_types: {
  plain: "text/plain",
  html: "text/html",
  pdf: "application/pdf"
}

You can use a symbol:

response.content_type = :html

If you would like to load all mime types supported by rack/mime, you can use the mime_types: :from_rack_mime option:

plugin :response_content_type, mime_types: :from_rack_mime

Note that you are unlikely to be using all of these mime types, so doing this will likely result in unnecessary memory usage. It is recommended to use a hash with only the mime types your application actually uses.

To prevent silent failures, if you attempt to set the response type with a symbol, and the symbol is not recognized, a KeyError is raised.

Defined Under Namespace

Modules: ResponseMethods

Class Method Summary collapse

Class Method Details

.configure(app, opts = OPTS) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/roda/plugins/response_content_type.rb', line 41

def self.configure(app, opts=OPTS)
  if mime_types = opts[:mime_types]
    mime_types = if mime_types == :from_rack_mime
      require "rack/mime"
      h = {}
      Rack::Mime::MIME_TYPES.each do |k, v|
        h[k.slice(1,100).to_sym] = v
      end
      h
    else
      mime_types.dup
    end
    app.opts[:repsonse_content_types] = mime_types.freeze
  else
    app.opts[:repsonse_content_types] ||= {}
  end
end