Rack::Transform
is a middleware that attemps to make a compatibility layer
between two different request/responses that should hit the same endpoint.
The need arouse when migrating an API from PHP to Ruby, where the URLs were different and the response body used to have more metadata that was moved to headers.
use Rack::Transform do |map|
map.on "playlist-get-cues" do |transformer|
transformer.request = proc do |env|
...
end
transformer.response = proc do |status, header, body|
...
end
end
map.on "search" do |transformer|
...
end
...
end
map.on
will receive a string that should match the type
parameter from the
URL and then a block to set the request and response transformers. For more
information, see Rack::Transform::Transformer
Rack:Transform::Transformer
intent is to transform rack requests and/or
responses into a different format.
It can manipulate the request, the response, or both.
Usage:
Transformer.new do |transformer|
transformer.request = proc do |env|
...
end
transformer.response = proc do |status, header, body|
...
end
end
request
and response
methods should receive an object that responds to
call
, it can be a proc or and object. And they will receive an env
object
and status
, header
, body
tuple accordingly. Should return the same
modified as needed. You can use the base transformers if you prefer to use
actual classes instead of proc
s.
To help with boilerplate code, base transformer request and response classes were added. More info.
See the contributing guide.