Module: Sinatra::Hellhound::FacebookHelpers

Defined in:
lib/hellhound.rb

Instance Method Summary collapse

Instance Method Details

#create_or_retrieve_facebook_user(&block) ⇒ Object

Retrive the user from the database or create a new one if it doesn’t exists.

If you want to store the user in memcached them you need to return the user at the end of the block

I.e

create_or_retrive_user do |user_data|      #    
  user = User.find :twitter_id => user_data["id"].to_s
  User.create :twitter_id => user_data["id"].to_s unless user
  user
end


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/hellhound.rb', line 119

def create_or_retrieve_facebook_user(&block)
  access_token = client.web_server.access_token(params[:code], :redirect_uri => redirect_uri)        
  session[:user_code] = params[:code]
  
  user_data = JSON.parse(access_token.get('/me'))        
  session[:user] = user_data["id"].to_s
  
  user = yield user_data        
  
  CACHE.set "user-#{session[:user]}", user if options.hellhound_cache
end

#current_access_tokenObject

Returns the current_user, it’s an instance of user model



100
101
102
# File 'lib/hellhound.rb', line 100

def current_access_token
  client.web_server.access_token(session[:user_code], :redirect_uri => redirect_uri)        
end

#facebook_clientObject



139
140
141
# File 'lib/hellhound.rb', line 139

def facebook_client
  OAuth2::Client.new(options.fb_api_key, options.fb_app_secret, :site => 'https://graph.facebook.com')
end

#facebook_redirect_uriObject



150
151
152
153
154
155
# File 'lib/hellhound.rb', line 150

def facebook_redirect_uri
  uri = URI.parse(request.url)
  uri.path = '/auth/facebook/callback'
  uri.query = nil
  uri.to_s
end

#get_credentialsObject

Returns a Hash with the user data.



134
135
136
137
# File 'lib/hellhound.rb', line 134

def get_credentials        
  access_token = client.web_server.access_token(params[:code], :redirect_uri => redirect_uri)
  JSON.parse(access_token.get('/me'))
end

#github_redirect_uriObject



143
144
145
146
147
148
# File 'lib/hellhound.rb', line 143

def github_redirect_uri
  uri = URI.parse(request.url)
  uri.path = '/auth/github/callback'
  uri.query = nil
  uri.to_s
end