Module: Sinatra::ReFacebookRegister

Defined in:
lib/refacebook/sinatra.rb

Instance Method Summary collapse

Instance Method Details

#require_facebook(*args) ⇒ Object

ReFacebook configuration done at the beginning of a Sinatra file.

An example:

require_facebook(:api_key =>'MY_API_KEY',
                 :secret_key => 'MY_SECRET_KEY',
                 :canvas_url => 'http://apps.facebook.com/canvas-page',
                 :require_login => true,
                 :store => store)
:api_key

Your application’s Facebook API key.

:secret_key

Your application’s Facebook Secret key.

:canvas_url

The full path to your canvas page.

:require_login

If this is set to true then the user is redirected to the login page where she needs to authenticate.

:store

Currently uses memcache-client as the session store since Rack doesn’t currently have non cookie based session stores.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/refacebook/sinatra.rb', line 40

def require_facebook *args
  settings = args[0]

  configure do
    set :canvas_url, settings[:canvas_url]
  end

  before do 
    # Passes the request to the correct request method since canvas requests
    # come in as POSTs.
    if params["fb_sig_in_canvas"].eql? "1" and params["fb_sig_request_method"].eql? "GET"
      request.env['REQUEST_METHOD'] = 'GET' 
    end


    if params['fb_sig_session_key']
      begin
        @fbsession = settings[:store].get(params['fb_sig_session_key'])
        @fbsession = ReFacebook::Session.new(settings[:api_key], settings[:secret_key]) unless @fbsession
        @fbsession.update_session_params(params)

        expiry = @fbsession.expires.to_i - @fbsession.time.to_i
        settings[:store].set(params['fb_sig_session_key'], @fbsession, expiry)
      rescue
        # Say the store fails!
        @fbsession = ReFacebook::Session.new(settings[:api_key], settings[:secret_key])
        @fbsession.update_session_params(params)
      end
    else
      # Just return a session, even if it's not a lasting session.
      @fbsession = ReFacebook::Session.new(settings[:api_key], settings[:secret_key])
    end

    if settings[:require_login] and !params['fb_sig_session_key']
      fbml_redirect(@fbsession.(:next => link_from_canvas(request.fullpath),
                                             :canvas => true))
    end
  end
end