rfacebook 0.9.1 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +0 -1
- data/lib/facebook_desktop_session.rb +58 -51
- data/lib/facebook_session.rb +214 -129
- data/lib/facebook_web_session.rb +75 -78
- data/lib/rfacebook_on_rails/controller_extensions.rb +99 -62
- data/lib/rfacebook_on_rails/model_extensions.rb +51 -23
- data/lib/rfacebook_on_rails/plugin/init.rb +13 -3
- data/lib/rfacebook_on_rails/plugin/rake.rb +1 -1
- data/lib/rfacebook_on_rails/session_extensions.rb +17 -17
- data/lib/rfacebook_on_rails/templates/exception_backtrace.rhtml +97 -0
- metadata +18 -7
data/lib/facebook_web_session.rb
CHANGED
@@ -31,30 +31,19 @@ require "facebook_session"
|
|
31
31
|
|
32
32
|
module RFacebook
|
33
33
|
|
34
|
-
class FacebookWebSession < FacebookSession
|
35
|
-
|
36
|
-
# SECTION: Properties
|
34
|
+
class FacebookWebSession < FacebookSession
|
37
35
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
def session_user_id
|
43
|
-
return @session_uid
|
44
|
-
end
|
45
|
-
|
46
|
-
def session_expires
|
47
|
-
return @session_expires
|
48
|
-
end
|
49
|
-
|
50
|
-
# SECTION: URL Getters
|
36
|
+
################################################################################################
|
37
|
+
################################################################################################
|
38
|
+
# :section: URL Accessors
|
39
|
+
################################################################################################
|
51
40
|
|
52
41
|
# Function: get_login_url
|
53
42
|
# Gets the authentication URL
|
54
43
|
#
|
55
44
|
# Parameters:
|
56
45
|
# options.next - the page to redirect to after login
|
57
|
-
# options.popup - boolean, whether or not to use the popup style (defaults to
|
46
|
+
# options.popup - boolean, whether or not to use the popup style (defaults to false)
|
58
47
|
# options.skipcookie - boolean, whether to force new Facebook login (defaults to false)
|
59
48
|
# options.hidecheckbox - boolean, whether to show the "infinite session" option checkbox
|
60
49
|
def get_login_url(options={})
|
@@ -80,6 +69,11 @@ class FacebookWebSession < FacebookSession
|
|
80
69
|
|
81
70
|
end
|
82
71
|
|
72
|
+
# Function: get_install_url
|
73
|
+
# Gets the installation URL for this application
|
74
|
+
#
|
75
|
+
# Parameters:
|
76
|
+
# options.next - the page to redirect to after installation
|
83
77
|
def get_install_url(options={})
|
84
78
|
|
85
79
|
# handle options
|
@@ -93,44 +87,10 @@ class FacebookWebSession < FacebookSession
|
|
93
87
|
|
94
88
|
end
|
95
89
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
# setup
|
102
|
-
timeout = 48*3600
|
103
|
-
namespace = "fb_sig"
|
104
|
-
prefix = "#{namespace}_"
|
105
|
-
|
106
|
-
# get the params prefixed by "fb_sig_" (and remove the prefix)
|
107
|
-
sigParams = {}
|
108
|
-
originalParams.each do |k,v|
|
109
|
-
oldLen = k.length
|
110
|
-
newK = k.sub(prefix, "")
|
111
|
-
if oldLen != newK.length
|
112
|
-
sigParams[newK] = v
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
# handle invalidation
|
117
|
-
if (timeout and (sigParams["time"].nil? or (Time.now.to_i - sigParams["time"].to_i > timeout.to_i)))
|
118
|
-
# invalidate if the timeout has been reached
|
119
|
-
sigParams = {}
|
120
|
-
end
|
121
|
-
|
122
|
-
if !sig_params_valid?(sigParams, originalParams[namespace])
|
123
|
-
# invalidate if the signatures don't match
|
124
|
-
sigParams = {}
|
125
|
-
end
|
126
|
-
|
127
|
-
return sigParams
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
# SECTION: Session Activation
|
90
|
+
################################################################################################
|
91
|
+
################################################################################################
|
92
|
+
# :section: Session Activation
|
93
|
+
################################################################################################
|
134
94
|
|
135
95
|
# Function: activate_with_token
|
136
96
|
# Gets the session information available after current user logs in.
|
@@ -140,7 +100,7 @@ class FacebookWebSession < FacebookSession
|
|
140
100
|
def activate_with_token(auth_token)
|
141
101
|
result = call_method("auth.getSession", {:auth_token => auth_token})
|
142
102
|
if result != nil
|
143
|
-
@
|
103
|
+
@session_user_id = result.at("uid").inner_html
|
144
104
|
@session_key = result.at("session_key").inner_html
|
145
105
|
@session_expires = result.at("expires").inner_html
|
146
106
|
end
|
@@ -161,42 +121,79 @@ class FacebookWebSession < FacebookSession
|
|
161
121
|
|
162
122
|
# determine the current user's id
|
163
123
|
if uid
|
164
|
-
@
|
124
|
+
@session_user_id = uid
|
165
125
|
else
|
166
126
|
result = call_method("users.getLoggedInUser")
|
167
|
-
@
|
127
|
+
@session_user_id = result.at("users_getLoggedInUser_response").inner_html
|
168
128
|
end
|
169
129
|
|
170
130
|
end
|
171
|
-
|
172
|
-
|
173
|
-
|
131
|
+
|
132
|
+
################################################################################################
|
133
|
+
################################################################################################
|
134
|
+
# :section: Canvas Signature Validation
|
135
|
+
################################################################################################
|
136
|
+
|
137
|
+
# Function: get_fb_sig_params
|
138
|
+
# Returns the fb_sig params from Hash that has all request params. Hash is empty if the
|
139
|
+
# signature was invalid.
|
140
|
+
#
|
141
|
+
# Parameters:
|
142
|
+
# originalParams - a Hash that contains the fb_sig_* params (i.e. Rails params)
|
143
|
+
#
|
144
|
+
def get_fb_sig_params(originalParams)
|
145
|
+
|
146
|
+
# setup
|
147
|
+
timeout = 48*3600
|
148
|
+
prefix = "fb_sig_"
|
149
|
+
|
150
|
+
# get the params prefixed by "fb_sig_" (and remove the prefix)
|
151
|
+
sigParams = {}
|
152
|
+
originalParams.each do |k,v|
|
153
|
+
oldLen = k.length
|
154
|
+
newK = k.sub(prefix, "")
|
155
|
+
if oldLen != newK.length
|
156
|
+
sigParams[newK] = v
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# handle invalidation
|
161
|
+
if (timeout and (sigParams["time"].nil? or (Time.now.to_i - sigParams["time"].to_i > timeout.to_i)))
|
162
|
+
# invalidate if the timeout has been reached
|
163
|
+
@logger.debug "** RFACEBOOK(GEM) - fbparams is empty because the signature was timed out" if @logger
|
164
|
+
sigParams = {}
|
165
|
+
end
|
166
|
+
|
167
|
+
# check that the signatures match
|
168
|
+
expectedSig = originalParams["fb_sig"]
|
169
|
+
if !(sigParams and expectedSig and generate_signature(sigParams, @api_secret) == expectedSig)
|
170
|
+
# didn't match, empty out the params
|
171
|
+
@logger.debug "** RFACEBOOK(GEM) - fbparams is empty because the signature did not match" if @logger
|
172
|
+
sigParams = {}
|
173
|
+
end
|
174
|
+
|
175
|
+
return sigParams
|
176
|
+
|
174
177
|
end
|
175
178
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
#
|
182
|
-
|
183
|
-
|
179
|
+
################################################################################################
|
180
|
+
################################################################################################
|
181
|
+
# :section: Template Methods
|
182
|
+
################################################################################################
|
183
|
+
|
184
|
+
# Function: is_activated?
|
185
|
+
# Returns true when we have activated ourselves somehow
|
184
186
|
def is_activated?
|
185
187
|
return (@session_key != nil)
|
186
188
|
end
|
187
189
|
|
188
190
|
# Function: get_secret
|
189
|
-
#
|
190
|
-
|
191
|
+
# Used by super::signature to generate a signature
|
192
|
+
# Web sessions simply use their API secret.
|
193
|
+
def get_secret(params) # :nodoc:
|
191
194
|
return @api_secret
|
192
195
|
end
|
193
|
-
|
194
|
-
def sig_params_valid?(sigParams, expectedSig)
|
195
|
-
return (sigParams and expectedSig and generate_signature(sigParams, @api_secret) == expectedSig)
|
196
|
-
end
|
197
196
|
|
198
197
|
end
|
199
|
-
|
200
|
-
|
201
|
-
|
198
|
+
|
202
199
|
end
|
@@ -34,15 +34,15 @@ module RFacebook
|
|
34
34
|
module Rails
|
35
35
|
module ControllerExtensions
|
36
36
|
|
37
|
-
#
|
37
|
+
# :section: StandardErrors
|
38
38
|
|
39
|
-
class APIKeyNeededStandardError < StandardError; end
|
40
|
-
class APISecretNeededStandardError < StandardError; end
|
41
|
-
class APICanvasPathNeededStandardError < StandardError; end
|
42
|
-
class APICallbackNeededStandardError < StandardError; end
|
43
|
-
class APIFinisherNeededStandardError < StandardError; end
|
39
|
+
class APIKeyNeededStandardError < StandardError; end # :nodoc:
|
40
|
+
class APISecretNeededStandardError < StandardError; end # :nodoc:
|
41
|
+
class APICanvasPathNeededStandardError < StandardError; end # :nodoc:
|
42
|
+
class APICallbackNeededStandardError < StandardError; end # :nodoc:
|
43
|
+
class APIFinisherNeededStandardError < StandardError; end # :nodoc:
|
44
44
|
|
45
|
-
#
|
45
|
+
# :section: Template Methods (must be implemented by concrete subclass)
|
46
46
|
|
47
47
|
def facebook_api_key
|
48
48
|
raise APIKeyNeededStandardError, "RFACEBOOK ERROR: when using the RFacebook on Rails plugin, please be sure that you have a facebook.yml file with 'key' defined"
|
@@ -66,7 +66,7 @@ module RFacebook
|
|
66
66
|
|
67
67
|
|
68
68
|
|
69
|
-
#
|
69
|
+
# :section: Special Variables
|
70
70
|
|
71
71
|
# Function: fbparams
|
72
72
|
# Accessor for all params beginning with "fb_sig_"
|
@@ -123,10 +123,10 @@ module RFacebook
|
|
123
123
|
|
124
124
|
end
|
125
125
|
|
126
|
-
#
|
126
|
+
# :section: Helpful Methods
|
127
127
|
|
128
128
|
# DEPRECATED
|
129
|
-
def facebook_redirect_to(url)
|
129
|
+
def facebook_redirect_to(url) # :nodoc:
|
130
130
|
RAILS_DEFAULT_LOGGER.info "** RFACEBOOK DEPRECATION NOTICE: facebook_redirect_to is deprecated in RFacebook. Instead, you can use redirect_to like any Rails app."
|
131
131
|
if in_facebook_canvas?
|
132
132
|
render :text => "<fb:redirect url=\"#{url}\" />"
|
@@ -152,14 +152,23 @@ module RFacebook
|
|
152
152
|
def in_external_app?
|
153
153
|
# FIXME: once you click away in an iframe app, you are considered to be an external app
|
154
154
|
# TODO: read up on the RFacebook hacks for avoiding nested iframes
|
155
|
-
return (
|
155
|
+
return (params["fb_sig"] == nil and !in_facebook_frame?)
|
156
156
|
end
|
157
157
|
|
158
158
|
def added_facebook_application?
|
159
|
-
return
|
159
|
+
return (params["fb_sig_added"] != nil)# and params["fb_sig_in_iframe"] == "1")
|
160
160
|
end
|
161
161
|
|
162
|
-
|
162
|
+
def facebook_platform_signature_verified?
|
163
|
+
return (fbparams and fparams.size > 0)
|
164
|
+
end
|
165
|
+
|
166
|
+
# TODO: define something along the lines of is_logged_in_to_facebook? that returns fbsession.is_ready? perhaps
|
167
|
+
|
168
|
+
################################################################################################
|
169
|
+
################################################################################################
|
170
|
+
# :section: Before_filters
|
171
|
+
################################################################################################
|
163
172
|
|
164
173
|
def handle_facebook_login
|
165
174
|
|
@@ -209,10 +218,7 @@ module RFacebook
|
|
209
218
|
end
|
210
219
|
|
211
220
|
def require_facebook_login
|
212
|
-
|
213
|
-
# handle a facebook login if given (external sites and iframe only)
|
214
|
-
handle_facebook_login
|
215
|
-
|
221
|
+
|
216
222
|
# now finish it off depending on whether we are in canvas, iframe, or external app
|
217
223
|
if !performed?
|
218
224
|
|
@@ -262,44 +268,16 @@ module RFacebook
|
|
262
268
|
return true
|
263
269
|
end
|
264
270
|
|
265
|
-
|
271
|
+
################################################################################################
|
272
|
+
################################################################################################
|
273
|
+
# :section: Facebook Debug Panel
|
274
|
+
################################################################################################
|
266
275
|
|
267
276
|
def render_with_facebook_debug_panel(options={})
|
268
277
|
begin
|
269
278
|
renderedOutput = render_to_string(options)
|
270
279
|
rescue Exception => e
|
271
|
-
|
272
|
-
prettyBacktrace = e.backtrace.map do |line|
|
273
|
-
cleanLine = line.gsub(RAILS_ROOT, "").gsub("<", "<").gsub(">", ">")
|
274
|
-
pieces = cleanLine.split("\n")
|
275
|
-
if (pieces and pieces.size> 0)
|
276
|
-
cleanLine = "<ul>"
|
277
|
-
pieces.each do |piece|
|
278
|
-
if matches = /.*[\/\\]+((.*)\:([0-9]+)\:\s*in\s*\`(.*)\')/.match(piece)
|
279
|
-
oldPiece = piece
|
280
|
-
filename = matches[2]
|
281
|
-
line = matches[3]
|
282
|
-
method = matches[4]
|
283
|
-
piece = "<div class='summary'><strong>#{filename}</strong>:<em>#{line}</em> in <strong>#{method}</strong></div>"
|
284
|
-
piece += "<div class='rawsummary'>#{oldPiece}</div>"
|
285
|
-
end
|
286
|
-
cleanLine += "<li>#{piece}</li>"
|
287
|
-
end
|
288
|
-
cleanLine += "</ul>"
|
289
|
-
end
|
290
|
-
"<tr><td>#{cleanLine}</td></tr>"
|
291
|
-
end
|
292
|
-
renderedOutput = "
|
293
|
-
<div class='RFacebook'>
|
294
|
-
<div class='backtrace'>
|
295
|
-
<table>
|
296
|
-
<tr><td>
|
297
|
-
<h1>RFacebook <span style='font-weight: normal; color: #6D84B4'>exception backtrace</span></h1>
|
298
|
-
</td></tr>
|
299
|
-
#{prettyBacktrace}
|
300
|
-
</table>
|
301
|
-
</div>
|
302
|
-
</div>"
|
280
|
+
renderedOutput = facebook_canvas_backtrace(e)
|
303
281
|
end
|
304
282
|
render_text "#{facebook_debug_panel}#{renderedOutput}"
|
305
283
|
end
|
@@ -309,7 +287,49 @@ module RFacebook
|
|
309
287
|
return ERB.new(template).result(Proc.new{})
|
310
288
|
end
|
311
289
|
|
312
|
-
def
|
290
|
+
# def rescue_action(exception)
|
291
|
+
# # TODO: for security, we only do this in development in the canvas
|
292
|
+
# if (in_facebook_canvas? and RAILS_ENV == "development")
|
293
|
+
# render_text "#{facebook_debug_panel}#{facebook_canvas_backtrace(exception)}"
|
294
|
+
# else
|
295
|
+
# # otherwise, do the default
|
296
|
+
# super
|
297
|
+
# end
|
298
|
+
# end
|
299
|
+
|
300
|
+
def facebook_canvas_backtrace(exception)
|
301
|
+
|
302
|
+
# TODO: potentially integrate features from Evan Weaver's facebook_exceptions
|
303
|
+
rfacebookBacktraceLines = []
|
304
|
+
exception.backtrace.each do |line|
|
305
|
+
|
306
|
+
# escape HTML
|
307
|
+
cleanLine = line.gsub(RAILS_ROOT, "").gsub("<", "<").gsub(">", ">")
|
308
|
+
|
309
|
+
# split up these lines by carriage return
|
310
|
+
pieces = cleanLine.split("\n")
|
311
|
+
if (pieces and pieces.size> 0)
|
312
|
+
pieces.each do |piece|
|
313
|
+
if matches = /.*[\/\\]+((.*)\:([0-9]+)\:\s*in\s*\`(.*)\')/.match(piece)
|
314
|
+
# for each parsed line, add to the array for later rendering in the template
|
315
|
+
rfacebookBacktraceLines << {
|
316
|
+
:filename => matches[2],
|
317
|
+
:line => matches[3],
|
318
|
+
:method => matches[4],
|
319
|
+
:rawsummary => piece,
|
320
|
+
}
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
# render to the ERB template
|
327
|
+
template = File.read(File.dirname(__FILE__) + "/templates/exception_backtrace.rhtml")
|
328
|
+
return ERB.new(template).result(Proc.new{})
|
329
|
+
|
330
|
+
end
|
331
|
+
|
332
|
+
def facebook_status_manager # :nodoc:
|
313
333
|
checks = [
|
314
334
|
SessionStatusCheck.new(self),
|
315
335
|
(FacebookParamsStatusCheck.new(self) unless (!in_facebook_canvas? and !in_facebook_frame?)),
|
@@ -324,9 +344,12 @@ module RFacebook
|
|
324
344
|
return StatusManager.new(checks)
|
325
345
|
end
|
326
346
|
|
327
|
-
|
347
|
+
################################################################################################
|
348
|
+
################################################################################################
|
349
|
+
# :section: RFacebook Private Methods
|
350
|
+
################################################################################################
|
328
351
|
|
329
|
-
def rfacebook_session_holder
|
352
|
+
def rfacebook_session_holder # :nodoc:
|
330
353
|
|
331
354
|
if (@rfacebook_session_holder == nil)
|
332
355
|
@rfacebook_session_holder = FacebookWebSession.new(facebook_api_key, facebook_api_secret)
|
@@ -337,7 +360,7 @@ module RFacebook
|
|
337
360
|
|
338
361
|
end
|
339
362
|
|
340
|
-
def rfacebook_persist_session_to_rails
|
363
|
+
def rfacebook_persist_session_to_rails # :nodoc:
|
341
364
|
if (!in_facebook_canvas? and rfacebook_session_holder.is_valid?)
|
342
365
|
RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: persisting Facebook session information into Rails session"
|
343
366
|
session[:rfacebook_session] = @rfacebook_session_holder.dup
|
@@ -346,9 +369,12 @@ module RFacebook
|
|
346
369
|
end
|
347
370
|
|
348
371
|
|
349
|
-
|
372
|
+
################################################################################################
|
373
|
+
################################################################################################
|
374
|
+
# :section: URL Management
|
375
|
+
################################################################################################
|
350
376
|
|
351
|
-
def url_for__RFACEBOOK(options={}, *parameters)
|
377
|
+
def url_for__RFACEBOOK(options={}, *parameters) # :nodoc:
|
352
378
|
|
353
379
|
# error check
|
354
380
|
if !options
|
@@ -358,7 +384,12 @@ module RFacebook
|
|
358
384
|
# use special URL rewriting when inside the canvas
|
359
385
|
# setting the mock_ajax option to true will override this
|
360
386
|
# and force usage of regular Rails rewriting
|
361
|
-
|
387
|
+
mockajaxSpecified = false
|
388
|
+
if options.is_a? Hash
|
389
|
+
mockajaxSpecified = options[:mock_ajax]
|
390
|
+
end
|
391
|
+
|
392
|
+
if (in_facebook_canvas? and !mockajaxSpecified) #TODO: do something separate for in_facebook_frame?
|
362
393
|
|
363
394
|
if options.is_a? Hash
|
364
395
|
options[:only_path] = true
|
@@ -386,7 +417,7 @@ module RFacebook
|
|
386
417
|
end
|
387
418
|
|
388
419
|
# mock-ajax rewriting
|
389
|
-
elsif
|
420
|
+
elsif mockajaxSpecified
|
390
421
|
options.delete(:mock_ajax) # clear it so it doesnt show up in the url
|
391
422
|
options[:only_path] = true
|
392
423
|
path = "#{request.protocol}#{request.host}:#{request.port}#{url_for__ALIASED(options, *parameters)}"
|
@@ -399,7 +430,7 @@ module RFacebook
|
|
399
430
|
return path
|
400
431
|
end
|
401
432
|
|
402
|
-
def redirect_to__RFACEBOOK(options = {}, *parameters)
|
433
|
+
def redirect_to__RFACEBOOK(options = {}, *parameters) # :nodoc:
|
403
434
|
if in_facebook_canvas?
|
404
435
|
|
405
436
|
canvasRedirUrl = url_for(options, *parameters)
|
@@ -413,11 +444,14 @@ module RFacebook
|
|
413
444
|
end
|
414
445
|
|
415
446
|
|
416
|
-
|
447
|
+
################################################################################################
|
448
|
+
################################################################################################
|
449
|
+
# :section: Extension Helpers
|
450
|
+
################################################################################################
|
417
451
|
|
418
|
-
CLASSES_EXTENDED = []
|
452
|
+
CLASSES_EXTENDED = [] # :nodoc:
|
419
453
|
|
420
|
-
def self.included(base)
|
454
|
+
def self.included(base) # :nodoc:
|
421
455
|
|
422
456
|
# check for a double include
|
423
457
|
doubleInclude = false
|
@@ -445,6 +479,9 @@ module RFacebook
|
|
445
479
|
alias_method(:redirect_to, :redirect_to__RFACEBOOK)
|
446
480
|
'
|
447
481
|
|
482
|
+
# ensure that every action handles facebook login
|
483
|
+
base.before_filter(:handle_facebook_login)
|
484
|
+
|
448
485
|
# ensure that we persist the Facebook session into the Rails session (if possible)
|
449
486
|
base.after_filter(:rfacebook_persist_session_to_rails)
|
450
487
|
|
@@ -32,28 +32,34 @@ module RFacebook
|
|
32
32
|
module Rails
|
33
33
|
module ModelExtensions
|
34
34
|
|
35
|
-
|
35
|
+
##################################################################
|
36
|
+
##################################################################
|
37
|
+
# :section: Errors
|
38
|
+
##################################################################
|
36
39
|
|
37
|
-
class APIKeyNeededStandardError < StandardError; end
|
38
|
-
class APISecretNeededStandardError < StandardError; end
|
40
|
+
class APIKeyNeededStandardError < StandardError; end # :nodoc:
|
41
|
+
class APISecretNeededStandardError < StandardError; end # :nodoc:
|
39
42
|
|
40
|
-
|
41
|
-
|
43
|
+
##################################################################
|
44
|
+
##################################################################
|
45
|
+
# :section: Template Methods (must be implemented by concrete subclass)
|
46
|
+
##################################################################
|
47
|
+
|
42
48
|
def facebook_api_key
|
43
|
-
raise APIKeyNeededStandardError
|
49
|
+
raise APIKeyNeededStandardError, "RFACEBOOK ERROR: when using the RFacebook on Rails plugin, please be sure that you have a facebook.yml file with 'key' defined"
|
44
50
|
end
|
45
51
|
|
46
52
|
def facebook_api_secret
|
47
|
-
raise APISecretNeededStandardError
|
53
|
+
raise APISecretNeededStandardError, "RFACEBOOK ERROR: when using the RFacebook on Rails plugin, please be sure that you have a facebook.yml file with 'secret' defined"
|
48
54
|
end
|
49
55
|
|
50
|
-
#
|
56
|
+
# :section: ActsAs method mixing
|
51
57
|
|
52
|
-
def self.included(base)
|
58
|
+
def self.included(base) # :nodoc:
|
53
59
|
base.extend ActsAsMethods
|
54
60
|
end
|
55
61
|
|
56
|
-
module ActsAsMethods
|
62
|
+
module ActsAsMethods # :nodoc:all
|
57
63
|
def acts_as_facebook_user
|
58
64
|
include RFacebook::Rails::ModelExtensions::ActsAsFacebookUser::InstanceMethods
|
59
65
|
extend RFacebook::Rails::ModelExtensions::ActsAsFacebookUser::ClassMethods
|
@@ -63,7 +69,8 @@ module RFacebook
|
|
63
69
|
|
64
70
|
##################################################################
|
65
71
|
##################################################################
|
66
|
-
#
|
72
|
+
# :section: Acts As Facebook User
|
73
|
+
##################################################################
|
67
74
|
module ActsAsFacebookUser
|
68
75
|
|
69
76
|
ACTORS = [] # holds a reference to all classes that have ActsAsFacebookUser injected
|
@@ -99,6 +106,10 @@ module RFacebook
|
|
99
106
|
"tv",
|
100
107
|
"wall_count",
|
101
108
|
"work_history",
|
109
|
+
"pic",
|
110
|
+
"pic_big",
|
111
|
+
"pic_small",
|
112
|
+
"pic_square"
|
102
113
|
]
|
103
114
|
|
104
115
|
|
@@ -106,13 +117,29 @@ module RFacebook
|
|
106
117
|
module ClassMethods
|
107
118
|
|
108
119
|
def find_or_create_by_facebook_session(sess)
|
109
|
-
|
110
|
-
|
111
|
-
|
120
|
+
if sess.is_ready?
|
121
|
+
|
122
|
+
# try to find, else create
|
123
|
+
instance = find_by_facebook_uid(sess.session_user_id)
|
124
|
+
if !instance
|
125
|
+
instance = self.new
|
126
|
+
end
|
127
|
+
|
128
|
+
# update session info
|
129
|
+
instance.facebook_session = sess
|
130
|
+
|
131
|
+
# update (or create) the object and return it
|
132
|
+
if !instance.save
|
133
|
+
RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: failed to update or create the Facebook user object in the database"
|
134
|
+
return nil
|
135
|
+
end
|
136
|
+
return instance
|
137
|
+
|
138
|
+
else
|
139
|
+
RAILS_DEFAULT_LOGGER.info "** RFACEBOOK WARNING: tried to use an inactive session for acts_as_facebook_user (in find_or_create_by_facebook_session)"
|
140
|
+
return nil
|
112
141
|
end
|
113
|
-
|
114
|
-
instance.save
|
115
|
-
return instance
|
142
|
+
|
116
143
|
end
|
117
144
|
|
118
145
|
end
|
@@ -122,9 +149,9 @@ module RFacebook
|
|
122
149
|
|
123
150
|
def facebook_session
|
124
151
|
if !@facebook_session
|
125
|
-
|
152
|
+
@facebook_session = FacebookWebSession.new(self.facebook_api_key, self.facebook_api_secret)
|
126
153
|
begin
|
127
|
-
|
154
|
+
@facebook_session.activate_with_previous_session(self.facebook_session_key, self.facebook_uid)
|
128
155
|
rescue
|
129
156
|
# not a valid facebook session, should we nil it out?
|
130
157
|
end
|
@@ -139,16 +166,17 @@ module RFacebook
|
|
139
166
|
end
|
140
167
|
|
141
168
|
def has_infinite_session_key?
|
169
|
+
# TODO: this check should really look at expires
|
142
170
|
return self.facebook_session_key != nil
|
143
171
|
end
|
144
172
|
|
145
|
-
def self.included(base)
|
173
|
+
def self.included(base) # :nodoc:
|
146
174
|
ActsAsFacebookUser::ACTORS << base
|
147
175
|
ActsAsFacebookUser::FIELDS.each do |fieldname|
|
148
|
-
base.class_eval <<-
|
176
|
+
base.class_eval <<-EOF
|
149
177
|
|
150
178
|
def #{fieldname}
|
151
|
-
if facebook_session.
|
179
|
+
if facebook_session.is_ready?
|
152
180
|
return facebook_session.cached_users_getInfo(
|
153
181
|
:uids => [facebook_uid],
|
154
182
|
:fields => ActsAsFacebookUser::FIELDS).user.send(:#{fieldname})
|
@@ -157,7 +185,7 @@ module RFacebook
|
|
157
185
|
end
|
158
186
|
end
|
159
187
|
|
160
|
-
|
188
|
+
EOF
|
161
189
|
end
|
162
190
|
end
|
163
191
|
|
@@ -90,9 +90,19 @@ end
|
|
90
90
|
|
91
91
|
# load Facebook configuration file (credit: Evan Weaver)
|
92
92
|
begin
|
93
|
-
|
94
|
-
rescue
|
95
|
-
|
93
|
+
yamlFile = YAML.load_file("#{RAILS_ROOT}/config/facebook.yml")
|
94
|
+
rescue Exception => e
|
95
|
+
raise StandardError, "config/facebook.yml could not be loaded."
|
96
|
+
end
|
97
|
+
|
98
|
+
if yamlFile
|
99
|
+
if yamlFile[RAILS_ENV]
|
100
|
+
FACEBOOK = yamlFile[RAILS_ENV]
|
101
|
+
else
|
102
|
+
raise StandardError, "config/facebook.yml exists, but doesn't have a configuration for RAILS_ENV=#{RAILS_ENV}."
|
103
|
+
end
|
104
|
+
else
|
105
|
+
raise StandardError, "config/facebook.yml does not exist."
|
96
106
|
end
|
97
107
|
|
98
108
|
# parse for full URLs in facebook.yml (multiple people have made this mistake)
|
@@ -35,7 +35,7 @@ namespace "facebook" do
|
|
35
35
|
######################################################################################
|
36
36
|
######################################################################################
|
37
37
|
desc "Sets up the RFacebook Rails Plugin. Right now, this simply copies facebook.yml into your config directory."
|
38
|
-
task "setup"
|
38
|
+
task "setup" do
|
39
39
|
|
40
40
|
filename = "#{RAILS_ROOT}/config/facebook.yml"
|
41
41
|
puts "======================================================"
|