class Rack::Forwarder

Constants

HEADERS_TO_FORWARD
VERSION

Public Class Methods

new(app, options = {}, &block) click to toggle source
# File lib/rack/forwarder.rb, line 15
def initialize(app, options = {}, &block)
  @app = app
  @matchers = Registry.new
  @options = options
  instance_eval(&block)
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/forwarder.rb, line 26
def call(env)
  request = Request.new(env)
  matcher = @matchers.match?(request.path)
  return @app.call(env) unless matcher

  request_method = request.request_method.to_s.downcase
  options = {
    headers: extract_http_headers(env),
  }.merge(@options)
  response = Excon.public_send(
    request_method,
    matcher.url_from(request.path),
    options,
  )

  [response.status, headers_from_response(response.headers), [response.body]]
end
forward(regexp, to:) click to toggle source
# File lib/rack/forwarder.rb, line 22
def forward(regexp, to:)
  @matchers.register(regexp, to)
end

Private Instance Methods

extract_http_headers(env) click to toggle source
# File lib/rack/forwarder.rb, line 46
def extract_http_headers(env)
  headers = env.each_with_object(Utils::HeaderHash.new) do |(key, value), hash|
    hash[$1] = value if key =~ /HTTP_(.*)/
  end
  headers["X-Request-Id"] = env["action_dispatch.request_id"]

  headers
end
headers_from_response(headers) click to toggle source
# File lib/rack/forwarder.rb, line 55
def headers_from_response(headers)
  HEADERS_TO_FORWARD.each_with_object(Utils::HeaderHash.new) do |header, hash|
    value = headers[header]
    hash[header] = value if value
  end
end