Module: FacebookRailsController

Defined in:
app/controllers/facebook_rails_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticated_by_facebook?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/controllers/facebook_rails_controller.rb', line 95

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

#check_if_f8ids_are_friends(first_f8id, second_f8id) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'app/controllers/facebook_rails_controller.rb', line 52

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



68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/facebook_rails_controller.rb', line 68

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



130
131
132
133
134
135
136
# File 'app/controllers/facebook_rails_controller.rb', line 130

def collect_facebook_authentication
  if @require_fb_authentication
    ensure_authenticated_to_facebook
  else
    try_facebook_authentication
  end
end

#current_user_dataObject



36
37
38
39
# File 'app/controllers/facebook_rails_controller.rb', line 36

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

#current_user_f8idObject



41
42
43
# File 'app/controllers/facebook_rails_controller.rb', line 41

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

#current_user_friends_f8idsObject



45
46
47
48
49
50
# File 'app/controllers/facebook_rails_controller.rb', line 45

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



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/facebook_rails_controller.rb', line 110

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



32
33
34
# File 'app/controllers/facebook_rails_controller.rb', line 32

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



138
139
140
141
142
143
144
# File 'app/controllers/facebook_rails_controller.rb', line 138

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

#in_facebook_app?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/controllers/facebook_rails_controller.rb', line 24

def in_facebook_app?
  FACEBOOK['f8app_host'].blank? or request.host == FACEBOOK['f8app_host']
end

#in_microsite?Boolean

Returns:

  • (Boolean)


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

def in_microsite?
  !in_facebook_app?
end

#microsite_href(path = '/') ⇒ Object



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

def microsite_href(path = '/')
  FACEBOOK['microsite_home'].to_s + path
end

#require_facebook_authenticationObject

prepend before filter to force authentication when collect_facebook_authentication is called



125
126
127
# File 'app/controllers/facebook_rails_controller.rb', line 125

def require_facebook_authentication
  @require_fb_authentication = true
end

#smart_href(path = '/') ⇒ Object



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

def smart_href(path = '/')
  if in_facebook_app?
    direct_href(path)
  else
    microsite_href(path)
  end
end

#top_redirect_to(url) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/facebook_rails_controller.rb', line 80

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



100
101
102
103
104
105
106
107
# File 'app/controllers/facebook_rails_controller.rb', line 100

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



62
63
64
65
66
# File 'app/controllers/facebook_rails_controller.rb', line 62

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