class ApiValve::Forwarder::Response
This class is wraps the original response. The rack_response
method is called by the Forwarder
to build the rack response that will be returned by the proxy. By changing this class, we can control how the request is published to the original caller
Constants
- WHITELISTED_HEADERS
Attributes
options[R]
original_request[R]
original_response[R]
Public Class Methods
new(original_request, original_response, options = {})
click to toggle source
# File lib/api_valve/forwarder/response.rb, line 21 def initialize(original_request, original_response, options = {}) @original_request = original_request @original_response = original_response @options = options.with_indifferent_access end
Public Instance Methods
rack_response()
click to toggle source
Must return a rack compatible response array of status code, headers and body
# File lib/api_valve/forwarder/response.rb, line 28 def rack_response [status, headers, [body]] end
Protected Instance Methods
permission_handler()
click to toggle source
# File lib/api_valve/forwarder/response.rb, line 34 def permission_handler original_request.env['permission_handler'] end
Private Instance Methods
adjust_location(location)
click to toggle source
# File lib/api_valve/forwarder/response.rb, line 58 def adjust_location(location) return location if @options[:target_prefix] == @options[:local_prefix] location&.gsub(/^#{@options[:target_prefix]}/, @options[:local_prefix]) end
body()
click to toggle source
# File lib/api_valve/forwarder/response.rb, line 64 def body original_response.body.to_s end
headers()
click to toggle source
# File lib/api_valve/forwarder/response.rb, line 44 def headers whitelisted_headers.each_with_object({}) do |k, h| if k == 'Location' h[k] = adjust_location(original_response.headers[k]) else h[k] = original_response.headers[k] end end.compact end
status()
click to toggle source
# File lib/api_valve/forwarder/response.rb, line 40 def status original_response.status end
whitelisted_headers()
click to toggle source
# File lib/api_valve/forwarder/response.rb, line 54 def whitelisted_headers @options[:whitelisted_headers] || WHITELISTED_HEADERS end