Question

I have a gem responsible for handling off-site payments for my users. Given an amount and a payment gateway, it determines how to redirect to the gateway's payment page. The redirection can be via GET or an auto-submitted POST form, with various headers.

I need it to be compatible with Sinatra and other Rack frameworks, so it returns a Rack::Response.

However, I have trouble telling my Rails controller that this is the response I want to send the user, and that it should just return it.

I can integrate it easily in Sinatra :

get '/pay' do
  rack_response = Rack::Response.new(['Hello'], 200, {'Content-Type' => 'text/plain'})
  return rack_response.to_a
end

The following seems to work in Rails 4 :

# test_controller.rb
def pay
  rack_response = Rack::Response.new(['Hello'], 200, {'Content-Type' => 'text/plain'})

  self.response = ActionDispatch::Response.new(*rack_response.to_a)
  self.response.close
  return
end

However I cannot find an elegant way to make it work in a Rails 3.2 app. Am I missing something?

Was it helpful?

Solution

This seems to work in a Rails 3.2 app

# test_controller.rb
def pay
  rack_response = Rack::Response.new(['Hello'], 200, {'Content-Type' => 'text/plain'})

  self.response_body = rack_response.body
  self.status = rack_response.status
  self.response.headers = rack_response.headers  

  return
end

It seems a little wonky though, if anyone has a more elegant way I'd take it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top