class Oxy::CustomHeaders

Simple middleware to set custom http headers

Public Class Methods

new(app, options) click to toggle source
# File lib/oxy/middleware/custom_headers.rb, line 5
def initialize(app, options)
  @app, @custom_headers = app, load_from_file(options)
end

Public Instance Methods

call(env) click to toggle source
# File lib/oxy/middleware/custom_headers.rb, line 9
def call(env)
  response = @app.call(env)
  headers = Rack::Utils::HeaderHash.new(response[1])
  response[1] = headers.merge(@custom_headers)
  response
end

Private Instance Methods

load_from_file(options) click to toggle source
# File lib/oxy/middleware/custom_headers.rb, line 17
def load_from_file(options)
  # parse custom headers from _config.yml
  filename = File.join(options.document_root, "_config.yml")
  conf = SafeYAML.load_file(filename) if File.exist?(filename)
  # yield custom headers (if any)
  if conf && conf.key?("webrick") && conf["webrick"]
    headers = conf["webrick"]["headers"]
  end
  # default to empty headers (if necessary)
  headers || {}
end