module Roda::RodaPlugins::DefaultHeaders

  1. lib/roda/plugins/default_headers.rb

The default_headers plugin accepts a hash of headers, and overrides the default_headers method in the response class to be a copy of the headers.

Note that when using this module, you should not attempt to mutate of the values set in the default headers hash.

Example:

plugin :default_headers, 'Content-Type'=>'text/csv'

You can modify the default headers later by loading the plugin again:

plugin :default_headers, 'Foo'=>'bar'
plugin :default_headers, 'Bar'=>'baz'

Methods

Public Class

  1. configure

Public Class methods

configure (app, headers={})

Merge the given headers into the existing default headers, if any.

[show source]
   # File lib/roda/plugins/default_headers.rb
25       def self.configure(app, headers={})
26         headers = app.opts[:default_headers] = (app.default_headers || app::RodaResponse::DEFAULT_HEADERS).merge(headers).freeze
27 
28         if headers.all?{|k, v| k.is_a?(String) && v.is_a?(String)}
29           response_class = app::RodaResponse
30           owner = response_class.instance_method(:set_default_headers).owner
31           if owner == Base::ResponseMethods || (owner == response_class && app.opts[:set_default_headers_overridder] == response_class)
32             app.opts[:set_default_headers_overridder] = response_class
33             response_class.class_eval(<<-END, __FILE__, __LINE__+1)
34               def set_default_headers
35                 h = @headers
36                 #{headers.map{|k,v| "h[#{k.inspect}] ||= #{v.inspect}"}.join('; ')}
37               end
38             END
39           end
40         end
41       end