Class: Carrot::Facebook::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/carrot-facebook/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
# File 'lib/carrot-facebook/middleware.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/carrot-facebook/middleware.rb', line 10

def call(env)
  request             = Rack::Request.new(env)
  env[:is_iframe_app] = false

  if request.POST and request.POST['signed_request']
    env["REQUEST_METHOD"] = 'GET'
    env[:is_iframe_app]   = true
    env[:facebook_data]   = parsed_signed_request(request.POST['signed_request'])

    if env[:facebook_data]
      if env[:facebook_data][:app_data].present?
        begin
          path_override                  = Yajl::Parser.parse(env[:facebook_data][:app_data], symbolize_keys: true) 
          env['PATH_INFO']               = path_override[:path] if path_override[:path]
          env['QUERY_STRING']            = path_override[:params].map { |params| params.join('=') }.join('&') if path_override[:params]
          env[:facebook_data][:app_data] = path_override
        rescue => e
          env[:facebook_data][:app_data] = env[:facebook_data][:app_data]
        end
      else
        env[:facebook_data][:app_data] = nil
      end
    end
  end

  @app.call(env)
end