rfacebook 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,22 +32,14 @@ require "facebook_session"
32
32
  module RFacebook
33
33
 
34
34
  class FacebookWebSession < FacebookSession
35
-
36
- ################################################################################################
37
- ################################################################################################
38
- # :section: URL Accessors
39
- ################################################################################################
40
-
41
- # Function: get_login_url
42
- # Gets the authentication URL
35
+
36
+ # Gets the authentication URL for this application
43
37
  #
44
- # Parameters:
45
- # options.next - the page to redirect to after login
46
- # options.popup - boolean, whether or not to use the popup style (defaults to false)
47
- # options.skipcookie - boolean, whether to force new Facebook login (defaults to false)
48
- # options.hidecheckbox - boolean, whether to show the "infinite session" option checkbox
38
+ # options.next:: the page to redirect to after login
39
+ # options.popup:: boolean, whether or not to use the popup style (defaults to false)
40
+ # options.skipcookie:: boolean, whether to force new Facebook login (defaults to false)
41
+ # options.hidecheckbox:: boolean, whether to show the "infinite session" option checkbox
49
42
  def get_login_url(options={})
50
-
51
43
  # handle options
52
44
  nextPage = options[:next] ||= nil
53
45
  popup = (options[:popup] == nil) ? false : true
@@ -65,17 +57,13 @@ module RFacebook
65
57
  optionalCanvas = (canvas == true) ? "&canvas=true" : ""
66
58
 
67
59
  # build and return URL
68
- return "http://#{WWW_SERVER_BASE_URL}#{WWW_PATH_LOGIN}?v=1.0&api_key=#{@api_key}#{optionalPopup}#{optionalNext}#{optionalSkipCookie}#{optionalHideCheckbox}#{optionalFrame}#{optionalCanvas}"
69
-
60
+ return "http://#{WWW_HOST}#{WWW_PATH_LOGIN}?v=1.0&api_key=#{@api_key}#{optionalPopup}#{optionalNext}#{optionalSkipCookie}#{optionalHideCheckbox}#{optionalFrame}#{optionalCanvas}"
70
61
  end
71
62
 
72
- # Function: get_install_url
73
- # Gets the installation URL for this application
63
+ # Gets the installation URL for this application
74
64
  #
75
- # Parameters:
76
- # options.next - the page to redirect to after installation
65
+ # options.next:: the page to redirect to after installation
77
66
  def get_install_url(options={})
78
-
79
67
  # handle options
80
68
  nextPage = options[:next] ||= nil
81
69
 
@@ -83,36 +71,25 @@ module RFacebook
83
71
  optionalNext = (nextPage == nil) ? "" : "&next=#{CGI.escape(nextPage.to_s)}"
84
72
 
85
73
  # build and return URL
86
- return "http://#{WWW_SERVER_BASE_URL}#{WWW_PATH_INSTALL}?api_key=#{@api_key}#{optionalNext}"
87
-
74
+ return "http://#{WWW_HOST}#{WWW_PATH_INSTALL}?api_key=#{@api_key}#{optionalNext}"
88
75
  end
89
-
90
- ################################################################################################
91
- ################################################################################################
92
- # :section: Session Activation
93
- ################################################################################################
94
-
95
- # Function: activate_with_token
96
- # Gets the session information available after current user logs in.
76
+
77
+ # Gets the session information available after current user logs in.
97
78
  #
98
- # Parameters:
99
- # auth_token - string token passed back by the callback URL
79
+ # auth_token:: string token passed back by the callback URL
100
80
  def activate_with_token(auth_token)
101
- result = call_method("auth.getSession", {:auth_token => auth_token})
102
- if result != nil
81
+ result = remote_call("auth.getSession", {:auth_token => auth_token})
82
+ unless result.nil?
103
83
  @session_user_id = result.at("uid").inner_html
104
84
  @session_key = result.at("session_key").inner_html
105
85
  @session_expires = result.at("expires").inner_html
106
86
  end
107
87
  end
108
88
 
109
- # Function: activate_with_previous_session
110
- # Sets the session key directly (for example, if you have an infinite session key)
89
+ # Sets the session key directly (for example, if you have an infinite session)
111
90
  #
112
- # Parameters:
113
- # key - the session key to use
91
+ # key:: the session key to use
114
92
  def activate_with_previous_session(key, uid=nil, expires=nil)
115
-
116
93
  # set the expiration
117
94
  @session_expires = expires
118
95
 
@@ -123,75 +100,23 @@ module RFacebook
123
100
  if uid
124
101
  @session_user_id = uid
125
102
  else
126
- result = call_method("users.getLoggedInUser")
103
+ result = remote_call("users.getLoggedInUser")
127
104
  @session_user_id = result.at("users_getLoggedInUser_response").inner_html
128
105
  end
129
-
130
- end
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
- #log_debug "** RFACEBOOK(GEM) - fbparams is empty because the signature was timed out"
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
- #log_debug "** RFACEBOOK(GEM) - fbparams is empty because the signature did not match"
172
- sigParams = {}
173
- end
174
-
175
- return sigParams
176
-
177
106
  end
178
107
 
179
- ################################################################################################
180
- ################################################################################################
181
- # :section: Template Methods
182
- ################################################################################################
183
-
184
- # Function: is_activated?
185
- # Returns true when we have activated ourselves somehow
186
- def is_activated?
187
- return (@session_key != nil)
108
+ # returns true if this session is completely ready to be used and make API calls
109
+ def ready?
110
+ return (@session_key != nil and !expired?)
188
111
  end
189
112
 
190
- # Function: get_secret
191
- # Used by super::signature to generate a signature
192
- # Web sessions simply use their API secret.
193
- def get_secret(params) # :nodoc:
194
- return @api_secret
113
+ # Used for signing a set of parameters in the way that Facebook
114
+ # specifies: <http://developers.facebook.com/documentation.php?v=1.0&doc=auth>
115
+ #
116
+ # params:: a Hash containing the parameters to sign
117
+ def signature(params)
118
+ # always sign the parameters with the API secret
119
+ return signature_helper(params, @api_secret)
195
120
  end
196
121
 
197
122
  end
@@ -27,40 +27,38 @@
27
27
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
28
  #
29
29
 
30
+
31
+ # FIXME: work out the kinks of Facepricot, including moving to JSON support rather than XML, and renaming to something less inane (like FacebookResponse)
32
+ # NOTE: Facepricot will likely be deprecated in version 1.0
33
+
30
34
  require "hpricot"
31
35
 
32
36
  module RFacebook
33
37
 
34
38
  module FacepricotChaining
35
-
36
- def make_facepricot_chain(key, doc)
37
39
 
40
+ private
41
+
42
+ def make_facepricot_chain(key, doc) # :nodoc:
43
+
38
44
  if matches = /(.*)_list/.match(key)
39
45
  listKey = matches[1]
40
46
  end
41
-
42
- result = nil
43
47
 
44
- if !result
45
- if listKey
46
- result = doc.search("/#{listKey}")
47
- end
48
- end
49
-
50
- if !result
51
- result = doc.at("/#{key}")
52
- end
48
+ results = nil
53
49
 
54
- if !result
55
- if listKey
50
+ if listKey
51
+ result = doc.search("/#{listKey}")
52
+ if result.empty?
56
53
  result = doc.search("//#{listKey}")
54
+ end
55
+ else
56
+ result = doc.at("/#{key}")
57
+ if !result
58
+ result = doc.at("//#{key}")
57
59
  end
58
60
  end
59
-
60
- if !result
61
- result = doc.at("//#{key}")
62
- end
63
-
61
+
64
62
  if result
65
63
  if result.is_a?(Array)
66
64
  return result.map{|r| FacepricotChain.new(r)}
@@ -113,11 +111,11 @@ module RFacebook
113
111
  end
114
112
 
115
113
  end
116
-
114
+
117
115
  class FacepricotChain < String
118
116
 
119
117
  include FacepricotChaining
120
-
118
+
121
119
  def initialize(hpricotDoc)
122
120
  super(hpricotDoc.inner_html.gsub("&amp;", "&"))
123
121
  @doc = hpricotDoc
@@ -0,0 +1,2 @@
1
+ require 'facebook_web_session'
2
+ require 'facebook_desktop_session'
@@ -27,44 +27,28 @@
27
27
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
28
  #
29
29
 
30
- module RFacebook
31
- module Rails
32
- module ViewExtensions
33
-
34
- def in_facebook_canvas?
35
- @controller.in_facebook_canvas?
36
- end
37
-
38
- def in_facebook_frame?
39
- @controller.in_facebook_frame?
40
- end
41
-
42
- def in_mock_ajax?
43
- @controller.in_mock_ajax?
44
- end
45
-
46
- def fbparams
47
- @controller.fbparams
48
- end
49
-
50
- def fbsession
51
- @controller.fbsession
52
- end
53
-
54
- def image_path(*params)
55
- path = super(*params)
56
- if ((in_facebook_canvas? or in_mock_ajax?) and !(/(\w+)(\:\/\/)([\w0-9\.]+)([\:0-9]*)(.*)/.match(path)))
57
- path = "#{request.protocol}#{request.host_with_port}#{path}"
58
- end
59
- return path
60
- end
30
+ require File.dirname(__FILE__) + '/test_helper'
31
+ require File.dirname(__FILE__) + '/facebook_session_test_methods'
61
32
 
62
- def facebook_debug_panel(options={})
63
- return @controller.facebook_debug_panel(options)
64
- end
65
-
66
- # TODO: override form_for to do <fb:editor> for Canvas pages, perhaps with an option to suppress such behavior
67
-
68
- end
33
+ class RFacebook::FacebookDesktopSession
34
+ def test_initialize(*params)
35
+ initialize(*params)
69
36
  end
70
- end
37
+ end
38
+
39
+ class FacebookDesktopSessionTest < Test::Unit::TestCase
40
+
41
+ include FacebookSessionTestMethods
42
+
43
+ def setup
44
+ # setting up a desktop session means that we need to allow the initialize method to 'access' the API for a createToken request
45
+ @fbsession = RFacebook::FacebookDesktopSession.allocate
46
+ @fbsession.expects(:post_request).returns(RFacebook::Dummy::AUTH_CREATETOKEN_RESPONSE)
47
+ @fbsession.test_initialize(RFacebook::Dummy::API_KEY, RFacebook::Dummy::API_SECRET)
48
+ end
49
+
50
+ def test_should_return_login_url
51
+ assert_equal "http://www.facebook.com/login.php?v=1.0&api_key=#{RFacebook::Dummy::API_KEY}&auth_token=3e4a22bb2f5ed75114b0fc9995ea85f1&popup=true", @fbsession.get_login_url
52
+ end
53
+
54
+ end
@@ -0,0 +1,106 @@
1
+ # Copyright (c) 2007, Matt Pizzimenti (www.livelearncode.com)
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without modification,
5
+ # are permitted provided that the following conditions are met:
6
+ #
7
+ # Redistributions of source code must retain the above copyright notice,
8
+ # this list of conditions and the following disclaimer.
9
+ #
10
+ # Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ #
14
+ # Neither the name of the original author nor the names of contributors
15
+ # may be used to endorse or promote products derived from this software
16
+ # without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ #
29
+
30
+ module FacebookSessionTestMethods
31
+
32
+ def force_to_be_activated(fbsession)
33
+ fbsession.stubs(:ready?).returns(true)
34
+ end
35
+
36
+ def test_method_missing_dispatches_to_facebook_api
37
+ fbsession = @fbsession.dup
38
+ force_to_be_activated(fbsession)
39
+ fbsession.expects(:remote_call).returns("mocked")
40
+ assert_equal "mocked", fbsession.friends_get
41
+ end
42
+
43
+ def test_remote_error_causes_fbsession_to_raise_errors
44
+ fbsession = @fbsession.dup
45
+ force_to_be_activated(fbsession)
46
+ fbsession.expects(:post_request).returns(RFacebook::Dummy::ERROR_RESPONSE)
47
+ assert_raise(RFacebook::FacebookSession::RemoteStandardError){fbsession.friends_get}
48
+ end
49
+
50
+ def test_nomethod_error_raises_ruby_equivalent
51
+ fbsession = @fbsession.dup
52
+ force_to_be_activated(fbsession)
53
+ fbsession.expects(:post_request).returns(RFacebook::Dummy::ERROR_RESPONSE_3)
54
+ assert_raise(NoMethodError){fbsession.friends_get}
55
+ end
56
+
57
+ def test_badargument_error_raises_ruby_equivalent
58
+ fbsession = @fbsession.dup
59
+ force_to_be_activated(fbsession)
60
+ fbsession.expects(:post_request).returns(RFacebook::Dummy::ERROR_RESPONSE_100)
61
+ assert_raise(ArgumentError){fbsession.friends_get}
62
+ fbsession.expects(:post_request).returns(RFacebook::Dummy::ERROR_RESPONSE_606)
63
+ assert_raise(ArgumentError){fbsession.friends_get}
64
+ end
65
+
66
+ def test_expiration_error_raises_error_and_sets_expired_flag
67
+ fbsession = @fbsession.dup
68
+ force_to_be_activated(fbsession)
69
+ fbsession.expects(:post_request).returns(RFacebook::Dummy::ERROR_RESPONSE_102)
70
+ assert_raise(RFacebook::FacebookSession::ExpiredSessionStandardError){fbsession.friends_get}
71
+ assert fbsession.expired?
72
+ end
73
+
74
+ def test_facepricot_response_to_group_getMembers
75
+ fbsession = @fbsession.dup
76
+ force_to_be_activated(fbsession)
77
+ fbsession.expects(:post_request).returns(RFacebook::Dummy::GROUP_GETMEMBERS_RESPONSE)
78
+ memberInfo = fbsession.group_getMembers
79
+ assert memberInfo
80
+ assert_equal 4, memberInfo.members.uid_list.size
81
+ assert_equal 1, memberInfo.admins.uid_list.size
82
+ assert memberInfo.officers
83
+ assert memberInfo.not_replied
84
+ end
85
+
86
+ def test_api_call_to_users_getLoggedInUser
87
+ fbsession = @fbsession.dup
88
+ force_to_be_activated(fbsession)
89
+ fbsession.expects(:post_request).returns(RFacebook::Dummy::USERS_GETLOGGEDINUSER_RESPONSE)
90
+ assert_equal "1234567", fbsession.users_getLoggedInUser
91
+ end
92
+
93
+ def test_api_call_to_users_getInfo
94
+ fbsession = @fbsession.dup
95
+ force_to_be_activated(fbsession)
96
+ fbsession.expects(:post_request).returns(RFacebook::Dummy::USERS_GETINFO_RESPONSE)
97
+ userInfo = fbsession.users_getInfo
98
+ assert userInfo
99
+ assert_equal "94303", userInfo.current_location.get(:zip)
100
+ end
101
+
102
+ def test_should_raise_not_activated
103
+ assert_raise(RFacebook::FacebookSession::NotActivatedStandardError){@fbsession.friends_get}
104
+ end
105
+
106
+ end
@@ -0,0 +1,48 @@
1
+ # Copyright (c) 2007, Matt Pizzimenti (www.livelearncode.com)
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without modification,
5
+ # are permitted provided that the following conditions are met:
6
+ #
7
+ # Redistributions of source code must retain the above copyright notice,
8
+ # this list of conditions and the following disclaimer.
9
+ #
10
+ # Redistributions in binary form must reproduce the above copyright notice,
11
+ # this list of conditions and the following disclaimer in the documentation
12
+ # and/or other materials provided with the distribution.
13
+ #
14
+ # Neither the name of the original author nor the names of contributors
15
+ # may be used to endorse or promote products derived from this software
16
+ # without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ #
29
+
30
+ require File.dirname(__FILE__) + '/facebook_session_test_methods'
31
+
32
+ class FacebookWebSessionTest < Test::Unit::TestCase
33
+
34
+ include FacebookSessionTestMethods
35
+
36
+ def setup
37
+ @fbsession = RFacebook::FacebookWebSession.new(RFacebook::Dummy::API_KEY, RFacebook::Dummy::API_SECRET)
38
+ end
39
+
40
+ def test_should_return_install_url
41
+ assert_equal "http://www.facebook.com/install.php?api_key=#{RFacebook::Dummy::API_KEY}", @fbsession.get_install_url
42
+ end
43
+
44
+ def test_should_return_login_url
45
+ assert_equal "http://www.facebook.com/login.php?v=1.0&api_key=#{RFacebook::Dummy::API_KEY}", @fbsession.get_login_url
46
+ end
47
+
48
+ end