rfacebook 0.8.5 → 0.8.6

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.
@@ -3,6 +3,7 @@ require "rfacebook_on_rails/controller_extensions"
3
3
  module RFacebook
4
4
  module RailsControllerExtensions
5
5
  def self.included(base)
6
+ RAILS_DEFAULT_LOGGER.info "** RFACEBOOK DEPRECATION NOTICE: direct use of RFacebook::RailsControllerExtensions (from facebook_rails_controller_extensions.rb) is deprecated, use the RFacebook on Rails plugin instead (http://rfacebook.rubyforge.org)"
6
7
  base.send(:include, RFacebook::Rails::ControllerExtensions)
7
8
  end
8
9
  end
@@ -166,7 +166,7 @@ class FacebookSession
166
166
 
167
167
  def cached_call_method(method,params)
168
168
  key = cache_key_for(method,params)
169
- @logger.debug "** RFacebook::FacebookSession\#cached_call_method - #{method}(#{params.inspect}) - attempting to hit cache" if @logger
169
+ @logger.debug "** RFACEBOOK(GEM) - RFacebook::FacebookSession\#cached_call_method - #{method}(#{params.inspect}) - attempting to hit cache" if @logger
170
170
  return @callcache[key] ||= call_method(method,params)
171
171
  end
172
172
 
@@ -191,7 +191,7 @@ class FacebookSession
191
191
  # use_ssl - set to true if the call will be made over SSL
192
192
  def call_method(method, params={}, use_ssl=false)
193
193
 
194
- @logger.debug "** RFacebook::FacebookSession\#call_method - #{method}(#{params.inspect}) - making remote call" if @logger
194
+ @logger.debug "** RFACEBOOK(GEM) - RFacebook::FacebookSession\#call_method - #{method}(#{params.inspect}) - making remote call" if @logger
195
195
 
196
196
  # ensure that this object has been activated somehow
197
197
  if (!method.include?("auth") and !is_activated?)
@@ -225,13 +225,13 @@ class FacebookSession
225
225
 
226
226
  # do the request
227
227
  xmlstring = post_request(@api_server_base_url, @api_server_path, method, params, use_ssl)
228
- # @logger.debug "** RFacebook::FacebookSession\#call_method - #{method}(#{params.inspect}) - raw XML response: #{xmlstring}" if @logger
228
+ # @logger.debug "** RFACEBOOK(GEM) - RFacebook::FacebookSession\#call_method - #{method}(#{params.inspect}) - raw XML response: #{xmlstring}" if @logger
229
229
  xml = Facepricot.new(xmlstring)
230
230
 
231
231
  # error checking
232
232
  if xml.at("error_response")
233
233
 
234
- @logger.debug "** RFacebook::FacebookSession\#call_method - #{method}(#{params.inspect}) - remote call failed" if @logger
234
+ @logger.debug "** RFACEBOOK(GEM) - RFacebook::FacebookSession\#call_method - #{method}(#{params.inspect}) - remote call failed" if @logger
235
235
 
236
236
  @last_call_was_successful = false
237
237
  code = xml.at("error_code").inner_html
@@ -67,7 +67,7 @@ module RFacebook
67
67
 
68
68
 
69
69
 
70
- # SECTION: Required Methods
70
+ # SECTION: Special Variables
71
71
 
72
72
  def fbparams
73
73
 
@@ -75,13 +75,13 @@ module RFacebook
75
75
 
76
76
  # try to get fbparams from the params hash
77
77
  if (!@fbparams || @fbparams.length <= 0)
78
- @fbparams = fbsession.get_fb_sig_params(dup_params)
78
+ @fbparams = rfacebook_session_holder.get_fb_sig_params(dup_params)
79
79
  end
80
80
 
81
81
  # else, try to get fbparams from the cookies hash
82
82
  # TODO: we don't write anything into the cookie, so this is kind of pointless right now
83
83
  if (@fbparams.length <= 0)
84
- @fbparams = fbsession.get_fb_sig_params(cookies)
84
+ @fbparams = rfacebook_session_holder.get_fb_sig_params(cookies)
85
85
  end
86
86
 
87
87
  # TODO: fb_sig_params now includes all friend ids by default, so we can avoid an API call to friends.get
@@ -90,38 +90,30 @@ module RFacebook
90
90
  return @fbparams
91
91
 
92
92
  end
93
-
94
- def fbsession
95
93
 
96
- if !@fbsession
97
-
98
- # create a session no matter what
99
- @fbsession = FacebookWebSession.new(facebook_api_key, facebook_api_secret)
94
+ def fbsession
100
95
 
96
+ # if we are in the canvas or iframe, we should be able to activate the session here
97
+ if (!rfacebook_session_holder.is_valid? and (in_facebook_canvas? or in_facebook_frame?))
98
+
101
99
  # then try to activate it somehow (or retrieve from previous state)
102
100
  # these might be nil
103
101
  facebookUid = fbparams["user"]
104
102
  facebookSessionKey = fbparams["session_key"]
105
103
  expirationTime = fbparams["expires"]
106
-
104
+
107
105
  if (facebookUid and facebookSessionKey and expirationTime)
108
- # Method 1: we have the user id and key from the fb_sig_ params
109
- @fbsession.activate_with_previous_session(facebookSessionKey, facebookUid, expirationTime)
110
- RAILS_DEFAULT_LOGGER.debug "** rfacebook: Activated session from inside the canvas"
111
-
112
- elsif (!in_facebook_canvas? and session[:rfacebook_fbsession])
113
- # Method 2: we've logged in the user already
114
- @fbsession = session[:rfacebook_fbsession]
115
-
116
- end
117
-
106
+ # we have the user id and key from the fb_sig_ params, activate the session
107
+ rfacebook_session_holder.activate_with_previous_session(facebookSessionKey, facebookUid, expirationTime)
108
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Activated session from inside the canvas"
109
+ else
110
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK WARNING: Tried to activate session from inside the canvas, but failed"
111
+ end
112
+
118
113
  end
119
114
 
120
- if @fbsession
121
- @fbsession.logger = RAILS_DEFAULT_LOGGER
122
- end
123
-
124
- return @fbsession
115
+ # if all went well, we should definitely have a valid Facebook session object
116
+ return rfacebook_session_holder
125
117
 
126
118
  end
127
119
 
@@ -129,7 +121,7 @@ module RFacebook
129
121
 
130
122
  # DEPRECATED
131
123
  def facebook_redirect_to(url)
132
- RAILS_DEFAULT_LOGGER.info "DEPRECATION NOTICE: facebook_redirect_to is deprecated in RFacebook. Instead, you can use redirect_to like any Rails app."
124
+ RAILS_DEFAULT_LOGGER.info "** RFACEBOOK DEPRECATION NOTICE: facebook_redirect_to is deprecated in RFacebook. Instead, you can use redirect_to like any Rails app."
133
125
  if in_facebook_canvas?
134
126
  render :text => "<fb:redirect url=\"#{url}\" />"
135
127
  elsif url =~ /^https?:\/\/([^\/]*\.)?facebook\.com(:\d+)?/i
@@ -144,31 +136,50 @@ module RFacebook
144
136
  end
145
137
 
146
138
  def in_facebook_frame?
147
- return (params["fb_sig_in_iframe"] != nil || params["fb_sig_in_canvas"] != nil)
139
+ return (params["fb_sig_in_iframe"] != nil or params["fb_sig_in_canvas"] != nil)
148
140
  end
149
141
 
150
142
  def in_external_app?
151
143
  return (!params[:fb_sig] and !in_facebook_frame?)
152
144
  end
145
+
146
+ # SECTION: before_filters
153
147
 
154
148
  def handle_facebook_login
155
-
156
- if (params["auth_token"] and !in_facebook_canvas?)
157
-
158
- # create a session
159
- session[:rfacebook_fbsession] = FacebookWebSession.new(facebook_api_key, facebook_api_secret)
160
- session[:rfacebook_fbsession].activate_with_token(params["auth_token"])
161
-
162
- # template method call upon success
163
- if session[:rfacebook_fbsession].is_valid?
164
- RAILS_DEFAULT_LOGGER.debug "** rfacebook: Login was successful, calling finish_facebook_login"
165
- finish_facebook_login
149
+
150
+ if (!in_facebook_canvas? and !rfacebook_session_holder.is_valid?)
151
+
152
+ if params["auth_token"]
153
+
154
+ # activate with the auth token
155
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: creating a new Facebook session from auth_token"
156
+ rfacebook_session_holder.activate_with_token(params["auth_token"])
157
+
158
+ # template method call upon success
159
+ if rfacebook_session_holder.is_valid?
160
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Login was successful, calling finish_facebook_login"
161
+ if in_external_app?
162
+ finish_facebook_login
163
+ end
164
+ end
165
+
166
+ elsif (session[:rfacebook_session] and session[:rfacebook_session].is_valid?)
167
+
168
+ # grab saved Facebook session from Rails session
169
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: grabbing Facebook session from Rails session"
170
+ @rfacebook_session_holder = session[:rfacebook_session]
171
+
166
172
  end
167
173
 
168
- else
169
- RAILS_DEFAULT_LOGGER.debug "** rfacebook: Didn't activate session from handle_facebook_login"
174
+ # warning logs
175
+ if !rfacebook_session_holder.is_valid?
176
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK WARNING: Facebook session could not be activated (from handle_facebook_login)"
177
+ elsif params["auth_token"]
178
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: received a new auth_token, but we already have a valid session (ignored new auth_token)"
179
+ end
180
+
170
181
  end
171
-
182
+
172
183
  end
173
184
 
174
185
  def require_facebook_login
@@ -176,30 +187,35 @@ module RFacebook
176
187
  # handle a facebook login if given (external sites and iframe only)
177
188
  handle_facebook_login
178
189
 
190
+ # now finish it off depending on whether we are in canvas, iframe, or external app
179
191
  if !performed?
180
-
181
- RAILS_DEFAULT_LOGGER.debug "** rfacebook: Rendering has not been performed"
182
-
192
+
183
193
  # try to get the session
184
194
  sess = fbsession
185
195
 
186
196
  # handle invalid sessions by forcing the user to log in
187
197
  if !sess.is_valid?
188
198
 
189
- RAILS_DEFAULT_LOGGER.debug "** rfacebook: Session is not valid"
199
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Session is not valid"
190
200
 
191
201
  if in_external_app?
192
- RAILS_DEFAULT_LOGGER.debug "** rfacebook: Redirecting to login"
202
+
203
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Redirecting to login for external app"
193
204
  redirect_to sess.get_login_url
194
205
  return false
206
+
195
207
  elsif (!fbparams or fbparams.size == 0)
196
- RAILS_DEFAULT_LOGGER.debug "** rfacebook: Failed to activate due to a bad API key or API secret"
208
+
209
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK WARNING: Failed to activate due to a bad API key or API secret"
197
210
  render_text facebook_debug_panel
198
211
  return false
212
+
199
213
  else
200
- RAILS_DEFAULT_LOGGER.debug "** rfacebook: Rendering canvas redirect"
214
+
215
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Redirecting to login for canvas app"
201
216
  render :text => "<fb:redirect url=\"#{sess.get_login_url(:canvas=>true)}\" />"
202
217
  return false
218
+
203
219
  end
204
220
  end
205
221
  end
@@ -212,52 +228,8 @@ module RFacebook
212
228
  render :text => "<fb:redirect url=\"#{sess.get_install_url}\" />"
213
229
  end
214
230
  end
215
-
216
- def self.included(base)
217
-
218
- # FIXME: figure out why this is necessary...for some reason, we
219
- # can't just define url_for in the module itself (it never gets called)
220
- base.class_eval '
221
231
 
222
- alias_method(:url_for__ALIASED, :url_for)
223
-
224
- def url_for(options={}, *parameters)
225
- if !options
226
- RAILS_DEFAULT_LOGGER.info "** options cannot be nil in call to url_for"
227
- end
228
- if in_facebook_canvas? #TODO: or in_facebook_frame?)
229
- if options.is_a? Hash
230
- options[:only_path] = true
231
- end
232
- path = url_for__ALIASED(options, *parameters)
233
- if path.starts_with?(self.facebook_callback_path)
234
- path.gsub!(self.facebook_callback_path, self.facebook_canvas_path)
235
- if !options.has_key?(:only_path)
236
- path = "http://apps.facebook.com#{path}"
237
- end
238
- end
239
- else
240
- path = url_for__ALIASED(options, *parameters)
241
- end
242
-
243
- return path
244
- end
245
-
246
-
247
- alias_method(:redirect_to__ALIASED, :redirect_to)
248
-
249
- def redirect_to(options = {}, *parameters)
250
- if in_facebook_canvas?
251
- RAILS_DEFAULT_LOGGER.debug "** Canvas redirect to #{url_for(options)}"
252
- render :text => "<fb:redirect url=\"#{url_for(options)}\" />"
253
- else
254
- RAILS_DEFAULT_LOGGER.debug "** Regular redirect_to"
255
- redirect_to__ALIASED(options, *parameters)
256
- end
257
- end
258
-
259
- '
260
- end
232
+ # SECTION: Facebook Debug Panel
261
233
 
262
234
  def render_with_facebook_debug_panel(options={})
263
235
  # oldLayout = options[:layout]
@@ -290,6 +262,100 @@ module RFacebook
290
262
  ].compact
291
263
  return StatusManager.new(checks)
292
264
  end
265
+
266
+ # SECTION: Private Methods
267
+
268
+ def rfacebook_session_holder
269
+
270
+ if (@rfacebook_session_holder == nil)
271
+ @rfacebook_session_holder = FacebookWebSession.new(facebook_api_key, facebook_api_secret)
272
+ @rfacebook_session_holder.logger = RAILS_DEFAULT_LOGGER
273
+ end
274
+
275
+ return @rfacebook_session_holder
276
+
277
+ end
278
+
279
+ def rfacebook_persist_session_to_rails
280
+ if (!in_facebook_canvas? and rfacebook_session_holder.is_valid?)
281
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: persisting Facebook session information into Rails session"
282
+ session[:rfacebook_session] = @rfacebook_session_holder.dup
283
+ session[:rfacebook_session].logger = nil # pstore can't serialize the Rails logger
284
+ end
285
+ end
286
+
287
+
288
+ # SECTION: URL Management
289
+
290
+ CLASSES_EXTENDED = []
291
+
292
+ def self.included(base)
293
+
294
+ # check for a double include
295
+ doubleInclude = false
296
+ CLASSES_EXTENDED.each do |klass|
297
+ if base.allocate.is_a?(klass) # TODO: is there a more direct way than allocating an instance and checking is_a?
298
+ doubleInclude = true
299
+ end
300
+ end
301
+
302
+ if doubleInclude
303
+ RAILS_DEFAULT_LOGGER.info "** RFACEBOOK WARNING: detected double-include of RFacebook controller extensions. Please see instructions for RFacebook on Rails plugin usage (http://rfacebook.rubyforge.org). You may be including the deprecated RFacebook::RailsControllerExtensions in addition to the plugin."
304
+
305
+ else
306
+ CLASSES_EXTENDED << base
307
+
308
+ # we need to use an eval since we will be overriding ActionController::Base methods
309
+ # and we need to be able to call the originals
310
+ base.class_eval '
311
+
312
+ alias_method(:url_for__ALIASED, :url_for)
313
+
314
+ def url_for(options={}, *parameters)
315
+ if !options
316
+ RAILS_DEFAULT_LOGGER.info "** RFACEBOOK WARNING: options cannot be nil in call to url_for"
317
+ end
318
+ if in_facebook_canvas? #TODO: or in_facebook_frame?)
319
+ if options.is_a? Hash
320
+ options[:only_path] = true
321
+ end
322
+ path = url_for__ALIASED(options, *parameters)
323
+ if path.starts_with?(self.facebook_callback_path)
324
+ path.gsub!(self.facebook_callback_path, self.facebook_canvas_path)
325
+ if !options.has_key?(:only_path)
326
+ path = "http://apps.facebook.com#{path}"
327
+ end
328
+ end
329
+ else
330
+ path = url_for__ALIASED(options, *parameters)
331
+ end
332
+
333
+ return path
334
+ end
335
+
336
+
337
+ alias_method(:redirect_to__ALIASED, :redirect_to)
338
+
339
+ def redirect_to(options = {}, *parameters)
340
+ if in_facebook_canvas?
341
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Canvas redirect to #{url_for(options)}"
342
+ render :text => "<fb:redirect url=\"#{url_for(options)}\" />"
343
+ else
344
+ RAILS_DEFAULT_LOGGER.debug "** RFACEBOOK INFO: Regular redirect_to"
345
+ redirect_to__ALIASED(options, *parameters)
346
+ end
347
+ end
348
+ '
349
+
350
+ # ensure that we persist the Facebook session in the Rails session (if possible)
351
+ base.after_filter(:rfacebook_persist_session_to_rails)
352
+
353
+ # fix third party cookies in IE
354
+ base.before_filter{ |c| c.headers['P3P'] = %|CP="NOI DSP COR NID ADMa OPTa OUR NOR"| }
355
+
356
+ end
357
+ end
358
+
293
359
 
294
360
  end
295
361
  end
@@ -74,13 +74,17 @@ end
74
74
 
75
75
  # make sure the paths have leading and trailing slashes
76
76
  def ensureLeadingAndTrailingSlashesForPath(path)
77
- if !path.starts_with?("/")
78
- path = "/#{path}"
79
- end
80
- if !path.reverse.starts_with?("/")
81
- path = "#{path}/"
77
+ if (path and path.size>0)
78
+ if !path.starts_with?("/")
79
+ path = "/#{path}"
80
+ end
81
+ if !path.reverse.starts_with?("/")
82
+ path = "#{path}/"
83
+ end
84
+ return path
85
+ else
86
+ return nil
82
87
  end
83
- return path
84
88
  end
85
89
 
86
90
  FACEBOOK["canvas_path"] = ensureLeadingAndTrailingSlashesForPath(FACEBOOK["canvas_path"])
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: rfacebook
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.8.5
7
- date: 2007-08-10 00:00:00 -07:00
6
+ version: 0.8.6
7
+ date: 2007-08-11 00:00:00 -07:00
8
8
  summary: A Ruby interface to the Facebook API v1.0+ (F8 and beyond). Works with RFacebook on Rails plugin (see rfacebook.rubyforge.org).
9
9
  require_paths:
10
10
  - lib