Class: Rack::Forwarder

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/forwarder.rb,
lib/rack/forwarder/matcher.rb,
lib/rack/forwarder/version.rb,
lib/rack/forwarder/registry.rb

Defined Under Namespace

Classes: Registry

Constant Summary collapse

HEADERS_TO_FORWARD =
%w(
  Content-Type
  Content-Length
)
VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &block) ⇒ Forwarder

Returns a new instance of Forwarder.



15
16
17
18
19
20
# File 'lib/rack/forwarder.rb', line 15

def initialize(app, options = {}, &block)
  @app = app
  @matchers = Registry.new
  @options = options
  instance_eval(&block)
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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:) ⇒ Object



22
23
24
# File 'lib/rack/forwarder.rb', line 22

def forward(regexp, to:)
  @matchers.register(regexp, to)
end