quinn
quinn

Reputation: 5978

How can I access the raw request in rails and re-instantiate the request later in rails?

I want to access the request object before a file upload gets converted into a tempfile and add it to a queue to be processed later, mainly because IO objects can't be marshaled or serialized. How can I do this?

I'll also need to "redo" the request at later point. At what point in the rails request lifecycle would I hook into to do this?

Upvotes: 2

Views: 398

Answers (3)

quinn
quinn

Reputation: 5978

Code example, in routes.rb:

post 'incoming_email' => Proc.new { |env|
  RequestCache.create!(
    data: env['rack.input'].read,
    content_type: env['CONTENT_TYPE'],
    content_length: env['CONTENT_LENGTH']
  )

  [200, {'Content-Type' => 'text/plain'}, ['OK']]
}

and in the model:

class RequestCache < ActiveRecord::Base
  attr_accessible :data, :content_length, :content_type

  def params
    Rack::Request.new({
      'rack.input' => StringIO.new(data),
      'CONTENT_LENGTH' => content_length,
      'CONTENT_TYPE' => content_type
    }).POST
  end
end

Upvotes: 2

Chamnap
Chamnap

Reputation: 4766

Well, just to do this you don't do something complicated like that.

If you want to do upload in the background process, you could simply use delayed_paperclip. This gem use delayed_job and process upload, resize, send to s3 (if you want) in the background process.

Upvotes: 1

vise
vise

Reputation: 13383

I don't know how feasible this is, but I would use rack middleware.

Upvotes: 1

Related Questions