Class: Facepalm::Rack::PostCanvasMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/facepalm/rack/post_canvas_middleware.rb

Overview

This rack middleware converts POST requests from Facebook to GET requests. It’s necessary to make RESTful routes work as expected without any changes in the application.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ PostCanvasMiddleware

Returns a new instance of PostCanvasMiddleware.



8
9
10
# File 'lib/facepalm/rack/post_canvas_middleware.rb', line 8

def initialize(app, options = {})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/facepalm/rack/post_canvas_middleware.rb', line 12

def call(env)
  request = ::Rack::Request.new(env)

  if request.POST['signed_request'] && request.post? && request.params['_method'].blank?
    env['REQUEST_METHOD'] = 'GET'
  end

  # Put signed_request parameter to the same place where HTTP header X-Signed-Request come.
  # This let us work both with params and HTTP headers in the same way. Very useful for AJAX.
  env['HTTP_SIGNED_REQUEST'] ||= request.POST['signed_request']

  @app.call(env)
end