Class: ApiValve::Forwarder

Inherits:
Object
  • Object
show all
Includes:
Benchmarking
Defined in:
lib/api_valve/forwarder.rb

Overview

This class is responsible for forwarding the HTTP request to the designated endpoint. It is instantiated once per Proxy with relevant options, and called from the router.

Defined Under Namespace

Classes: Request, Response

Instance Method Summary collapse

Methods included from Benchmarking

#benchmark

Constructor Details

#initialize(options = {}) ⇒ Forwarder

Initialized with global options. Possible values are: request: Options for the request wrapper. See Request#new. response: Options for the response wrapper. See Response#new



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

def initialize(options = {})
  @options = options.with_indifferent_access
  uri = URI(options[:endpoint])
  # Target prefix must be without trailing slash
  @target_prefix = uri.path.gsub(%r{/$}, '')
end

Instance Method Details

#call(original_request, local_options = {}) ⇒ Object

Takes the original rack request with optional options and returns a rack response Instantiates the Request and Response classes and wraps them around the original request and response.



25
26
27
28
29
# File 'lib/api_valve/forwarder.rb', line 25

def call(original_request, local_options = {})
  request = build_request(original_request, request_options.deep_merge(local_options))
  response = build_response(original_request, run_request(request), response_options)
  response.rack_response
end