Module: Roda::RodaPlugins::Proxy::RequestMethods

Defined in:
lib/roda/proxy.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#proxyObject

Proxies the request, forwarding all headers except Host which is rewritten to be the destination host. The response headers, body and status are returned to the client.



37
38
39
40
41
42
# File 'lib/roda/proxy.rb', line 37

def proxy
  method = Faraday.method(env['REQUEST_METHOD'].downcase.to_sym)
  f_response = method.call(_proxy_url) { |req| _proxy_request(req) }
#           p f_response
  _respond(f_response)
end

#proxy_when(condition = true, probability: 1.0) ⇒ Object

Conditionally proxies when condition is true and with selective probability. For instance, to proxy 50% of the time:

r.proxy_when(probability: 0.5)

Condition can be a truthy value or a block / lambda, in which case the result from the #call is expected to be truthy.

r.proxy_when( r.env['HTTP_PROXY_ME'] == 'true' )

The two parameters can be combined, the probability is evaluated first.

r.proxy_when( r.env['HTTP_PROXY_ME'] == 'true', probability: 0.8 )

If and only if this method choses not to proxy is the block evaluated. The block is then expected to return a meaningful response to Roda.



54
55
56
57
58
59
60
61
62
# File 'lib/roda/proxy.rb', line 54

def proxy_when(condition = true, probability: 1.0)
  shall_proxy = Random.rand(0.0..1.0) <= probability
            
  if shall_proxy && ( condition.respond_to?(:call) ? condition.call : condition )
    proxy
  else
    yield(self)
  end
end