Module: FacebookRailsController

Defined in:
app/controllers/facebook_rails_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticated_by_facebook?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/controllers/facebook_rails_controller.rb', line 75

def authenticated_by_facebook?
  return !session[:access_token].nil?
end

#check_if_f8ids_are_friends(first_f8id, second_f8id) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'app/controllers/facebook_rails_controller.rb', line 32

def check_if_f8ids_are_friends(first_f8id, second_f8id)
  #handle corner cases with anonymous users
  return false if first_f8id.nil? or second_f8id.nil?

  return (facebook_api.fql_query('SELECT uid2 FROM friend where uid1=' + first_f8id + ' AND uid2=' + second_f8id).size > 0)
rescue Exception => e
  logger.error "ERROR:" + e.to_s
  return false
end

#clean_and_recode_current_url(escape = true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/facebook_rails_controller.rb', line 48

def clean_and_recode_current_url(escape = true)
  omit_keys = ["_method", "format", "signed_request", "code", "clear_users_session"]
  options = (params||{}).clone
  options = options.reject{|k,v| omit_keys.include?(k.to_s)}
  options = options.merge({:only_path => false})
  if escape
    return CGI.escape(url_for(options))
  else
    return url_for(options)
  end
end

#collect_facebook_authenticationObject

Add method to before filter to collect user



110
111
112
113
114
115
116
# File 'app/controllers/facebook_rails_controller.rb', line 110

def collect_facebook_authentication
  if @require_fb_authentication
    ensure_authenticated_to_facebook
  else
    try_facebook_authentication
  end
end

#current_user_dataObject



16
17
18
19
# File 'app/controllers/facebook_rails_controller.rb', line 16

def current_user_data
  @current_user_data ||= facebook_api.get_object("me")
  return @current_user_data || {}
end

#current_user_f8idObject



21
22
23
# File 'app/controllers/facebook_rails_controller.rb', line 21

def current_user_f8id
  session[:user_f8id] ||= current_user_data["id"]
end

#current_user_friends_f8idsObject



25
26
27
28
29
30
# File 'app/controllers/facebook_rails_controller.rb', line 25

def current_user_friends_f8ids
  @current_user_friends_f8ids ||= facebook_api.get_connections('me','friends', :fields => 'id').collect { |f| f['id']}
rescue Exception => e
  logger.error 'FACEBOOK ERROR: ' + e.to_s
  return []
end

#direct_href(path = '/') ⇒ Object



8
9
10
# File 'app/controllers/facebook_rails_controller.rb', line 8

def direct_href(path = '/')
  FACEBOOK['f8app_callback'].to_s + path
end

#ensure_authenticated_to_facebookObject

Will force user to authorize the application to access the current url



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/facebook_rails_controller.rb', line 90

def ensure_authenticated_to_facebook
  attempt_to_acquire_facebook_credentials do |fb_oauth|
    if params[:error_reason] == "user_denied"
      redirect_url = root_url
    else
      redirect_url = fb_oauth.url_for_oauth_code(:callback => clean_and_recode_current_url, :permissions => Game.extended_permissions)
    end
    top_redirect_to(redirect_url)
  end
rescue Exception => e
  logger.error 'AUTHENTICATION ERROR: ' + e.to_s
  top_redirect_to clean_and_recode_current_url(false)
end

#facebook_apiObject



12
13
14
# File 'app/controllers/facebook_rails_controller.rb', line 12

def facebook_api
  @facebook_api ||= Koala::Facebook::API.new(session[:access_token])
end

#facebook_href(path = '/') ⇒ Object



4
5
6
# File 'app/controllers/facebook_rails_controller.rb', line 4

def facebook_href(path = '/')
  FACEBOOK['f8app_home'].to_s + path
end

#get_facebook_localeObject



118
119
120
121
122
123
124
# File 'app/controllers/facebook_rails_controller.rb', line 118

def get_facebook_locale
  puts current_user_data.inspect
  current_user_data["locale"]
rescue Exception => e
  logger.warn "Facebook locale error: #{ e.inspect }"
  session[:f8_locale] || "en_US"
end

#require_facebook_authenticationObject

prepend before filter to force authentication when collect_facebook_authentication is called



105
106
107
# File 'app/controllers/facebook_rails_controller.rb', line 105

def require_facebook_authentication
  @require_fb_authentication = true
end

#top_redirect_to(url) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/facebook_rails_controller.rb', line 60

def top_redirect_to(url)
  @redirect_url = CGI.unescape(url)
  render :layout => false, :inline => <<-HTML
        <html><head>
          <script type="text/javascript">
            window.top.location.href = "<%=raw @redirect_url -%>";
          </script>
          <noscript>
            <meta http-equiv="refresh" content="0;url=<%= @redirect_url %>" />
            <meta http-equiv="window-target" content="_top" />
          </noscript>
        </head></html>
  HTML
end

#try_facebook_authenticationObject

Will only try to collect facebook credentials but it will let the user in even there aren’t any



80
81
82
83
84
85
86
87
# File 'app/controllers/facebook_rails_controller.rb', line 80

def try_facebook_authentication
  attempt_to_acquire_facebook_credentials do |fb_oauth|
    clear_oauth
  end
rescue Exception => e
  clear_oauth
  logger.error "FACEBOOK AUTH EXCEPTION: #{ e.inspect } #{ e.backtrace }"
end

#url_smart_add_params(url, params = {}) ⇒ Object



42
43
44
45
46
# File 'app/controllers/facebook_rails_controller.rb', line 42

def url_smart_add_params(url, params = {})
  new_url = url + (url.index(/\w\?\w/) ? '&' : '?')
  params.each_pair {|key, value| new_url << key.to_s << "=" << value.to_s << '&'}
  return new_url.chop
end