Skip to content

Commit 6b58248

Browse files
committed
Pass through Rack::Response
1 parent bcf6286 commit 6b58248

File tree

7 files changed

+15
-11
lines changed

7 files changed

+15
-11
lines changed

examples/aggregation/config.ru

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'api_valve'
2+
require 'byebug'
23

34
app = Rack::Builder.new do
45
use ApiValve::Middleware::ErrorHandling
@@ -9,11 +10,10 @@ app = Rack::Builder.new do
910
threads = (1..5).map do |i|
1011
Thread.new { forwarder.call request, 'path' => "posts/#{i}" }
1112
end
12-
threads.each(&:join)
1313
body = threads.map(&:value).map do |rack_response|
14-
JSON.parse(rack_response[2].first)
14+
JSON.parse(rack_response.body.first)
1515
end.to_json
16-
[200, {'Content-Type' => 'application/json'}, [body]]
16+
Rack::Response.new(body, 200, {'Content-Type' => 'application/json'})
1717
end
1818
end
1919

examples/basic/config.ru

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require 'api_valve'
22

33
app = Rack::Builder.new do
44
map '/api' do
5-
run ApiValve::Proxy.from_hash(endpoint: 'http://api.host/api/')
5+
run ApiValve::Proxy.from_hash(endpoint: 'https://jsonplaceholder.typicode.com')
66
end
77
map '/oauth' do
88
run ApiValve::Proxy.from_hash(endpoint: 'http://auth.host/oauth/')

lib/api_valve/error_responder.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ def initialize(error)
55
end
66

77
def call
8-
[
8+
Rack::Response[
99
status,
1010
{'Content-Type' => 'application/vnd.api+json'},
11-
[MultiJson.dump({errors: [json_error]}, mode: :compat)]
11+
MultiJson.dump({errors: [json_error]}, mode: :compat)
1212
]
1313
end
1414

lib/api_valve/forwarder/response.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize(original_request, original_response, options = {})
2626

2727
# Must return a rack compatible response array of status code, headers and body
2828
def rack_response
29-
[status, headers, [body]]
29+
Rack::Response.new(body, status, headers)
3030
end
3131

3232
protected

lib/api_valve/middleware/error_handling.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def call(env)
88
@app.call(env)
99
rescue Exception => e # rubocop:disable Lint/RescueException
1010
log_error e
11-
self.class.const_get(ApiValve.error_responder).new(e).call
11+
render_error(e).to_a
1212
end
1313

1414
private
@@ -17,5 +17,9 @@ def log_error(error)
1717
ApiValve.logger.error { "#{error.class}: #{error.message}" }
1818
ApiValve.logger.error { error.backtrace.join("\n") }
1919
end
20+
21+
def render_error(error)
22+
self.class.const_get(ApiValve.error_responder).new(error).call
23+
end
2024
end
2125
end

lib/api_valve/proxy.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def initialize(forwarder)
2424
end
2525

2626
def call(env)
27-
to_app.call(env)
27+
to_app.call(env).to_a
2828
rescue ApiValve::Error::Client, ApiValve::Error::Server => e
29-
render_error e
29+
render_error(e).to_a
3030
end
3131

3232
delegate :add_route, to: :route_set

spec/api_valve/forwarder/response_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{'Location' => '/remote-prefix/see/other/path'}
2121
end
2222
let(:rack_response) { response.rack_response }
23-
let(:headers) { rack_response[1] }
23+
let(:headers) { rack_response.headers }
2424

2525
describe 'Location header' do
2626
subject { headers['Location'] }

0 commit comments

Comments
 (0)