Rack middleware for modifying response headers

Language: Ruby

module Rackables
  # Allows you to tap into the response headers hash. Example:
  #
  #   use Rackables::ResponseHeaders do |headers|
  #     headers['X-Foo'] = 'bar'
  #     headers.delete('X-Baz')
  #   end
  #
  class ResponseHeaders
    def initialize(app, &block)
      @app = app
      @block = block
    end

    def call(env)
      response = @app.call(env)
      headers = ::Rack::Utils::HeaderHash.new(response[1])
      @block.call(headers)
      response[1] = headers
      response
    end
  end
end
Reveal More
Added over 2 years ago by Devo_normal gbuesing