rplatform 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,124 @@
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 "facebook_session"
31
+
32
+ module RFacebook
33
+
34
+ class FacebookWebSession < FacebookSession
35
+
36
+ # Gets the authentication URL for this application
37
+ #
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
42
+ def get_login_url(options={})
43
+ # handle options
44
+ nextPage = options[:next] ||= nil
45
+ popup = (options[:popup] == nil) ? false : true
46
+ skipcookie = (options[:skipcookie] == nil) ? false : true
47
+ hidecheckbox = (options[:hidecheckbox] == nil) ? false : true
48
+ frame = (options[:frame] == nil) ? false : true
49
+ canvas = (options[:canvas] == nil) ? false : true
50
+
51
+ # url pieces
52
+ optionalNext = (nextPage == nil) ? "" : "&next=#{CGI.escape(nextPage.to_s)}"
53
+ optionalPopup = (popup == true) ? "&popup=true" : ""
54
+ optionalSkipCookie = (skipcookie == true) ? "&skipcookie=true" : ""
55
+ optionalHideCheckbox = (hidecheckbox == true) ? "&hide_checkbox=true" : ""
56
+ optionalFrame = (frame == true) ? "&fbframe=true" : ""
57
+ optionalCanvas = (canvas == true) ? "&canvas=true" : ""
58
+
59
+ # build and return URL
60
+ return "http://#{get_network_param(:www_host)}#{get_network_param(:www_path_login)}?v=1.0&api_key=#{@api_key}#{optionalPopup}#{optionalNext}#{optionalSkipCookie}#{optionalHideCheckbox}#{optionalFrame}#{optionalCanvas}"
61
+ end
62
+
63
+ # Gets the installation URL for this application
64
+ #
65
+ # options.next:: the page to redirect to after installation
66
+ def get_install_url(options={})
67
+ # handle options
68
+ nextPage = options[:next] ||= nil
69
+
70
+ # url pieces
71
+ optionalNext = (nextPage == nil) ? "" : "&next=#{CGI.escape(nextPage.to_s)}"
72
+
73
+ # build and return URL
74
+ return "http://#{get_network_param(:www_host)}#{get_network_param(:www_path_install)}?api_key=#{@api_key}#{optionalNext}"
75
+ end
76
+
77
+ # Gets the session information available after current user logs in.
78
+ #
79
+ # auth_token:: string token passed back by the callback URL
80
+ def activate_with_token(auth_token)
81
+ result = remote_call("auth.getSession", {:auth_token => auth_token})
82
+ unless result.nil?
83
+ @session_user_id = result.at("uid").inner_html
84
+ @session_key = result.at("session_key").inner_html
85
+ @session_expires = result.at("expires").inner_html
86
+ end
87
+ end
88
+
89
+ # Sets the session key directly (for example, if you have an infinite session)
90
+ #
91
+ # key:: the session key to use
92
+ def activate_with_previous_session(key, uid=nil, expires=nil)
93
+ # set the expiration
94
+ @session_expires = expires
95
+
96
+ # set the session key
97
+ @session_key = key
98
+
99
+ # determine the current user's id
100
+ if uid
101
+ @session_user_id = uid
102
+ else
103
+ result = remote_call("users.getLoggedInUser")
104
+ @session_user_id = result.at("users_getLoggedInUser_response").inner_html
105
+ end
106
+ end
107
+
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?)
111
+ end
112
+
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)
120
+ end
121
+
122
+ end
123
+
124
+ end
@@ -0,0 +1,135 @@
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
+
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
+
34
+ require "hpricot"
35
+
36
+ module RFacebook
37
+
38
+ module FacepricotChaining
39
+
40
+ private
41
+
42
+ def make_facepricot_chain(key, doc) # :nodoc:
43
+
44
+ if matches = /(.*)_list/.match(key)
45
+ listKey = matches[1]
46
+ end
47
+
48
+ results = nil
49
+
50
+ if listKey
51
+ result = doc.search("/#{listKey}")
52
+ if result.empty?
53
+ result = doc.search("//#{listKey}")
54
+ end
55
+ else
56
+ result = doc.at("/#{key}")
57
+ if !result
58
+ result = doc.at("//#{key}")
59
+ end
60
+ end
61
+
62
+ if result
63
+ if result.is_a?(Array)
64
+ return result.map{|r| FacepricotChain.new(r)}
65
+ else
66
+ return FacepricotChain.new(result)
67
+ end
68
+ else
69
+ return nil
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ class Facepricot
77
+
78
+ include FacepricotChaining
79
+
80
+ def initialize(xml)
81
+ @doc = Hpricot.XML(xml)
82
+ @raw_xml = xml
83
+ end
84
+
85
+ def method_missing(methodSymbol, *params)
86
+ begin
87
+ @doc.method(methodSymbol).call(*params) # pose as Hpricot document
88
+ rescue
89
+ return make_facepricot_chain(methodSymbol.to_s, @doc.containers[0])
90
+ end
91
+ end
92
+
93
+ def hpricot
94
+ return @doc
95
+ end
96
+
97
+ def response
98
+ return FacepricotChain.new(@doc.containers[0])
99
+ end
100
+
101
+ def raw_xml
102
+ return @raw_xml
103
+ end
104
+
105
+ def to_s
106
+ return @doc.containers[0].inner_html
107
+ end
108
+
109
+ def get(key)
110
+ return make_facepricot_chain(key.to_s, @doc.containers[0])
111
+ end
112
+
113
+ end
114
+
115
+ class FacepricotChain < String
116
+
117
+ include FacepricotChaining
118
+
119
+ def initialize(hpricotDoc)
120
+ super(hpricotDoc.inner_html.gsub("&amp;", "&"))
121
+ @doc = hpricotDoc
122
+ end
123
+
124
+ def method_missing(methodSymbol, *params)
125
+ return make_facepricot_chain(methodSymbol.to_s, @doc)
126
+ end
127
+
128
+ def get(key)
129
+ return make_facepricot_chain(key.to_s, @doc)
130
+ end
131
+
132
+ end
133
+
134
+
135
+ end
@@ -0,0 +1,5 @@
1
+ require 'facebook_web_session'
2
+ require 'facebook_desktop_session'
3
+ class RPlatform
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,54 @@
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__) + '/test_helper'
31
+ require File.dirname(__FILE__) + '/facebook_session_test_methods'
32
+
33
+ class RFacebook::FacebookDesktopSession
34
+ def test_initialize(*params)
35
+ initialize(*params)
36
+ 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