facebook-stub 0.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ require "facebook-stub/version"
2
+ require 'facebook-stub/rails/action_view_helper'
3
+
4
+ module Facebook
5
+ module Stub
6
+ BASE_INFO = {
7
+ "name" => "Gandalf the Grey",
8
+ "location" => {
9
+ "name" => "San Francisco, California",
10
+ "city" => "San Francisco",
11
+ "zip" => "94107",
12
+ "country" => "United States",
13
+ "id" => 114952118516947,
14
+ "state" => "California"
15
+ },
16
+ "timezone" => -8,
17
+ "gender" => "male",
18
+ "id" => "234567898765432",
19
+ "birthday" => "07/21/1954",
20
+ "updated_time" => "2011-02-10T00:40:26+0000",
21
+ "verified" => true,
22
+ "locale" => "en_US",
23
+ "religion" => "Wikipedia",
24
+ "bio" => "The greatest thing you'll ever learn is just to love and be loved in return.\r\n\r\nIf hearing the name Sharon Salinger causes you physical pain and emotional trauma then we are friends.\r\n\r\nMy life has a great cast but I can't figure out the plot.",
25
+ "hometown" => { "name" => nil, "id" => "" },
26
+ "link" => "http://www.facebook.com/gandalf.the.grey",
27
+ "political" => "Fellowship",
28
+ "email" => "gandalf@change.org",
29
+ "first_name" => "Gandalf",
30
+ "middle_name" => "the",
31
+ "last_name" => "Grey"
32
+ }
33
+
34
+ def Stub::reset!
35
+ # Marshal to force a deep copy
36
+ @info = Marshal.load(Marshal.dump(BASE_INFO))
37
+ end
38
+
39
+ def Stub::info
40
+ @info
41
+ end
42
+
43
+ def Stub::invalid!
44
+ @info = {}
45
+ end
46
+
47
+ Stub::reset!
48
+ end
49
+ end
50
+
51
+
52
+ if defined?(ActionView::Base)
53
+ ActionView::Base.send(:include, Facebook::Stub::Rails::ActionViewHelper)
54
+ end
@@ -0,0 +1,20 @@
1
+ module Facebook
2
+ module Stub
3
+ module Rails
4
+ module ActionViewHelper
5
+ @@facebook_javascript_stub = nil
6
+ def include_facebook_stub
7
+ @@facebook_javascript_stub ||= begin
8
+ data = ''
9
+ f = File.open(File.join(*([File.dirname(__FILE__)] + ['..'] * 3 + ['facebook-stub.js'])))
10
+ f.each_line do |line|
11
+ data << line
12
+ end
13
+ f.close
14
+ javascript_tag data
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Facebook
2
+ module Stub
3
+ VERSION = "0.0.1.1"
4
+ end
5
+ end
data/src/facebook.js ADDED
@@ -0,0 +1,307 @@
1
+ ;(function(window, undefined) {
2
+
3
+ //var standardPerms = '{"extended":["status_update","photo_upload","video_upload","offline_access","email","create_note","share_item","publish_stream","contact_email"],"user":["manage_friendlists","create_event","read_requests","manage_pages"],"friends":[]}';
4
+
5
+ // two globals for creating the cookie
6
+ // FB Functions
7
+ function init(data) {
8
+ FBWorld.initialized = true;
9
+ state('appId', data.appId);
10
+ }
11
+
12
+ function login(callback, options) {
13
+ if (calledBeforeInit('login')) return;
14
+ if (FBWorld.state('loggedIn')) {
15
+ console.log('FB.login() called when user is already connected.');
16
+ if (FBWorld.state('connected')) {
17
+ callback(getStatus('standard'));
18
+ } else {
19
+ simulatePromptToConnect(callback, options);
20
+ }
21
+ } else {
22
+ simulatePromptToLogin(callback, options);
23
+ }
24
+ }
25
+
26
+ function logout(callback) {
27
+ if (calledBeforeInit('logout')) return;
28
+ if (!FBWorld.state('loggedIn')) console.log('FB.logout() called without a session.');
29
+ FBWorld.notLoggedIn();
30
+ callback(getStatus());
31
+ }
32
+
33
+ function getLoginStatus(callback, perms) {
34
+ if (calledBeforeInit('getLoginStatus')) return;
35
+ callback(getStatus(perms ? 'extended' : false));
36
+ }
37
+
38
+ function getUserID() {
39
+ if (calledBeforeInit('getUserID')) return;
40
+ return uid();
41
+ }
42
+
43
+
44
+ function getSession() {
45
+ if (calledBeforeInit('getSession')) return false;
46
+ return getStatus().session;
47
+ }
48
+
49
+ function api(location, callback) {
50
+ if (!FBWorld.state('connected')) {
51
+ callback(undefined);
52
+ } else if (location == '/me/friends') {
53
+ callback({data:FBWorld.friendList()});
54
+ } else if (location == '/me/permissions') {
55
+ var theState = FBWorld.state();
56
+ var perms;
57
+ if (theState && theState.perms) {
58
+ perms = {data:[theState.perms.extended]}
59
+ }
60
+ callback( perms );
61
+ }
62
+ }
63
+
64
+ // FBWorld Functions
65
+ //3 states: loggedOut, loggedIn, connected
66
+ function state() {
67
+ var theState = JSON.parse(FBWorld.Helpers.makeMeACookie('fb-stub') || '{}');
68
+ if (arguments.length === 0) return theState;
69
+ if (arguments.length === 1) return theState[arguments[0]];
70
+ if (arguments.length === 2) {
71
+ theState[arguments[0]] = arguments[1];
72
+ FBWorld.Helpers.makeMeACookie('fb-stub', JSON.stringify(theState), cookieOptions);
73
+ return arguments[1];
74
+ }
75
+ if (arguments.length === 3) {
76
+ if(typeof(theState[arguments[0]]) == 'undefined') theState[arguments[0]] = {};
77
+ theState[arguments[0]][arguments[1]] = arguments[2];
78
+ FBWorld.Helpers.makeMeACookie('fb-stub', JSON.stringify(theState), cookieOptions);
79
+ return arguments[2];
80
+ }
81
+ }
82
+
83
+ function uid() {
84
+ return FBWorld.state('uid');
85
+ }
86
+
87
+ function setUid(newUid) {
88
+ return FBWorld.state('uid', newUid);
89
+ }
90
+
91
+ function setExtendedPermissions(newPermissions) {
92
+ return FBWorld.state('perms', 'extended', newPermissions);
93
+ }
94
+
95
+ function setSecret(newSecret) {
96
+ return state('secret', newSecret);
97
+ }
98
+
99
+ function loggedIn() {
100
+ createConnectedCookie();
101
+ FBWorld.state('loggedIn', true);
102
+ return true;
103
+ }
104
+
105
+ function notLoggedIn() {
106
+ deleteConnectedCookie();
107
+ FBWorld.state('loggedIn', false);
108
+ }
109
+
110
+ function connected() {
111
+ createConnectedCookie();
112
+ FBWorld.state('connected', true);
113
+ }
114
+
115
+ function notConnected() {
116
+ deleteConnectedCookie();
117
+ FBWorld.state('connected', false);
118
+ }
119
+
120
+ function addFriend(id, name) {
121
+ var friends = FBWorld.friendList();
122
+ friends.push({id: id, name: name});
123
+ FBWorld.Helpers.makeMeACookie('fb_friends', JSON.stringify(friends));
124
+ }
125
+
126
+ function friendList() {
127
+ return JSON.parse(FBWorld.Helpers.makeMeACookie('fb_friends') || '[]');
128
+ }
129
+
130
+ var XFBML = {
131
+ parse: function(element, callback) {
132
+ callback();
133
+ }
134
+ };
135
+
136
+ FB = { // Emulates the FB API
137
+ getLoginStatus : getLoginStatus,
138
+ logout : logout,
139
+ login : login,
140
+ init : init,
141
+ getSession : getSession,
142
+ api : api,
143
+ XFBML : XFBML,
144
+ getUserID : getUserID
145
+ };
146
+
147
+ FBWorld = { // used to set the state of Facebook
148
+ state : state,
149
+ loggedIn : loggedIn,
150
+ notLoggedIn : notLoggedIn,
151
+ setUid : setUid,
152
+ setSecret : setSecret,
153
+ uid : uid,
154
+ connected : connected,
155
+ notConnected : notConnected,
156
+ setExtendedPermissions : setExtendedPermissions,
157
+
158
+ initialized : false,
159
+ beingPromptedToLogIn : false,
160
+ beingPromptedToLogInCallback : undefined,
161
+ // this will come later, no need for it now
162
+ // successfullyLogin: successfullyLogin,
163
+ // failToLogin: failToLogin,
164
+
165
+ beingPromptedToConnect : false,
166
+ beingPromptedToConnectInCallback : undefined,
167
+ allowConnection : allowConnection,
168
+ denyConnection : denyConnection,
169
+
170
+ //friends
171
+ addFriend : addFriend,
172
+ friendList : friendList
173
+ };
174
+
175
+ // PRIVATE FUNCTIONS
176
+
177
+ function getStatus(permissions) {
178
+ var theState = FBWorld.state();
179
+
180
+ // Connected
181
+ if (theState.loggedIn && theState.connected) {
182
+ var status = {
183
+ status: "connected",
184
+ authResponse: createConnectedCookie()
185
+ };
186
+
187
+ if(typeof(permissions) != 'undefined') {
188
+ status.perms = permissions == 'extended' ? JSON.stringify(theState.perms) : theState.perms.standard;
189
+ }
190
+ return status;
191
+ }
192
+
193
+ // not connected
194
+ if (theState.loggedIn && !theState.connected) {
195
+ return {
196
+ perms: null,
197
+ authResponse: null,
198
+ status: 'notConnected'
199
+ };
200
+ }
201
+
202
+ // not logged in
203
+ if (!theState.loggedIn) {
204
+ return {
205
+ perms: null,
206
+ authResponse: null,
207
+ status: 'unknown'
208
+ };
209
+ }
210
+
211
+ };
212
+
213
+ function calledBeforeInit() {
214
+ if (FBWorld.initialized) return false;
215
+ console.log("FB."+meth+" called before FB.init");
216
+ return true;
217
+ }
218
+
219
+ function simulatePromptToLogin(callback, options) {
220
+ // simulate being prompted to log in
221
+ FBWorld.beingPromptedToLogIn = true;
222
+ FBWorld.beingPromptedToLogInCallback = function(approved) {
223
+ FBWorld.beingPromptedToLogin = false;
224
+ FBWorld.beingPromptedToLoginCallback = undefined;
225
+ if(approved) {
226
+ FBWorld.loggedIn();
227
+ if (!FBWorld.state('connected')) {
228
+ simulatePromptToConnect(callback, options);
229
+ } else {
230
+ FBWorld.state('perms', 'standard', options.perms);
231
+ callback(getStatus('standard'));
232
+ }
233
+ } else {
234
+ FBWorld.notLoggedIn();
235
+ callback(getStatus());
236
+ }
237
+
238
+ };
239
+ };
240
+
241
+ function simulatePromptToConnect(callback, options) {
242
+ // simulate being prompted to connect
243
+ FBWorld.beingPromptedToConnect = true;
244
+ FBWorld.beingPromptedToConnectCallback = function(approved) {
245
+ approved ? FBWorld.connected() : FBWorld.notConnected();
246
+ FBWorld.beingPromptedToConnect = false;
247
+ FBWorld.beingPromptedToConnectCallback = undefined;
248
+ if (approved) {
249
+ FBWorld.state('perms', 'standard', options.perms);
250
+ }
251
+ callback(getStatus('standard'));
252
+ };
253
+ };
254
+
255
+ function allowConnection() {
256
+ if (!FBWorld.beingPromptedToConnect) throw "you are not being prompted to connect";
257
+ FBWorld.beingPromptedToConnectCallback(true);
258
+ };
259
+
260
+ function denyConnection() {
261
+ if (!FBWorld.beingPromptedToConnect) throw "you are not being prompted to connect";
262
+ FBWorld.beingPromptedToConnectCallback(false);
263
+ };
264
+
265
+ var cookieOptions = { path: '/', domain: window.location.hostname.replace(/^www/, '')};
266
+
267
+ // cookie looks like this: (with the quotes): "access_token=theToken&base_domain=local-change.org&expires=0&secret=theSecret&session_key=theSessionKeysig=theSig-Hashed&uid=theUID"
268
+ function createConnectedCookie() {
269
+ var theState = {
270
+ user_id: state('uid'),
271
+ code: 'theAccessToken|hashData',
272
+ // We need to verify the timezone for this value. Traditionally FB uses PST8PDT, but it may be UTC.
273
+ issued_at: Math.floor(new Date().getTime() / 1000)
274
+ };
275
+
276
+ if (uid() != null) {
277
+ theState.uid = uid();
278
+ }
279
+
280
+ FBWorld.Helpers.makeMeACookie('fbsr_'+state('appId'), cookieToString(theState, state('secret')), cookieOptions);
281
+ return theState;
282
+ }
283
+
284
+ function cookieToString(theState, secret) {
285
+ // Set the algorithm here, to keep any changes here.
286
+ theState.algorithm = 'HMAC-SHA256';
287
+
288
+ var payload = JSON.stringify(theState),
289
+ encodedPayload = FBWorld.Helpers.base64_encode(payload),
290
+ shaObj = new FBWorld.Helpers.jsSHA(encodedPayload, "ASCII"),
291
+ b64Signature = shaObj.getHMAC(secret, "ASCII", "SHA-256", "B64");
292
+
293
+ // jsSHA uses an odd Base64 encoder, which uses + where FB has -. For now we'll just replace them,
294
+ // but if we find other inconsistencies, we should use the HEX value and encode it ourselves.
295
+ b64Signature.replace('+', '-');
296
+
297
+ return b64Signature + '.' + encodedPayload;
298
+ }
299
+
300
+ function deleteConnectedCookie() {
301
+ FBWorld.Helpers.makeMeACookie('fbsr_'+state('appId'), null, cookieOptions);
302
+ }
303
+
304
+
305
+ })(this);
306
+ FBWorld.Helpers = {};
307
+ setTimeout(function() { if (typeof fbAsyncInit === 'function') fbAsyncInit(); }, 1);
@@ -0,0 +1,54 @@
1
+ FBWorld.Helpers.base64_encode = function (data, utf8encode) {
2
+ // http://kevin.vanzonneveld.net
3
+ // + original by: Tyler Akins (http://rumkin.com)
4
+ // + improved by: Bayron Guevara
5
+ // + improved by: Thunder.m
6
+ // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
7
+ // + bugfixed by: Pellentesque Malesuada
8
+ // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
9
+ // + improved by: Rafał Kukawski (http://kukawski.pl)
10
+ // - depends on: utf8_encode
11
+ // * example 1: base64_encode('Kevin van Zonneveld');
12
+ // * returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
13
+ // mozilla has this native
14
+ // - but breaks in 2.0.0.12!
15
+ //if (typeof this.window['atob'] == 'function') {
16
+ // return atob(data);
17
+ //}
18
+ var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
19
+ var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
20
+ ac = 0,
21
+ enc = "",
22
+ tmp_arr = [];
23
+
24
+ if (!data) {
25
+ return data;
26
+ }
27
+
28
+ // Only do this if forced
29
+ if (utf8encode)
30
+ data = this.utf8_encode(data + '');
31
+
32
+ do { // pack three octets into four hexets
33
+ o1 = data.charCodeAt(i++);
34
+ o2 = data.charCodeAt(i++);
35
+ o3 = data.charCodeAt(i++);
36
+
37
+ bits = o1 << 16 | o2 << 8 | o3;
38
+
39
+ h1 = bits >> 18 & 0x3f;
40
+ h2 = bits >> 12 & 0x3f;
41
+ h3 = bits >> 6 & 0x3f;
42
+ h4 = bits & 0x3f;
43
+
44
+ // use hexets to index into b64, and append result to encoded string
45
+ tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
46
+ } while (i < data.length);
47
+
48
+ enc = tmp_arr.join('');
49
+
50
+ var r = data.length % 3;
51
+
52
+ return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
53
+
54
+ };
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Cookie plugin
3
+ *
4
+ * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
5
+ * Dual licensed under the MIT and GPL licenses:
6
+ * http://www.opensource.org/licenses/mit-license.php
7
+ * http://www.gnu.org/licenses/gpl.html
8
+ *
9
+ */
10
+
11
+ /**
12
+ * Create a cookie with the given name and value and other optional parameters.
13
+ *
14
+ * @example $.cookie('the_cookie', 'the_value');
15
+ * @desc Set the value of a cookie.
16
+ * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
17
+ * @desc Create a cookie with all available options.
18
+ * @example $.cookie('the_cookie', 'the_value');
19
+ * @desc Create a session cookie.
20
+ * @example $.cookie('the_cookie', null);
21
+ * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
22
+ * used when the cookie was set.
23
+ *
24
+ * @param String name The name of the cookie.
25
+ * @param String value The value of the cookie.
26
+ * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
27
+ * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
28
+ * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
29
+ * If set to null or omitted, the cookie will be a session cookie and will not be retained
30
+ * when the the browser exits.
31
+ * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
32
+ * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
33
+ * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
34
+ * require a secure protocol (like HTTPS).
35
+ * @type undefined
36
+ *
37
+ * @name $.cookie
38
+ * @cat Plugins/Cookie
39
+ * @author Klaus Hartl/klaus.hartl@stilbuero.de
40
+ */
41
+
42
+ /**
43
+ * Get the value of a cookie with the given name.
44
+ *
45
+ * @example $.cookie('the_cookie');
46
+ * @desc Get the value of a cookie.
47
+ *
48
+ * @param String name The name of the cookie.
49
+ * @return The value of the cookie.
50
+ * @type String
51
+ *
52
+ * @name $.cookie
53
+ * @cat Plugins/Cookie
54
+ * @author Klaus Hartl/klaus.hartl@stilbuero.de
55
+ */
56
+
57
+ // Modified to make it not use jquery
58
+ FBWorld.Helpers.makeMeACookie = function(name, value, options) {
59
+ if (typeof value != 'undefined') { // name and value given, set cookie
60
+ options = options || {};
61
+ if (value === null) {
62
+ value = '';
63
+ options.expires = -1;
64
+ } else {
65
+ options.expires = 100; // 100 days from now
66
+ }
67
+ var expires = '';
68
+ if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
69
+ var date;
70
+ if (typeof options.expires == 'number') {
71
+ date = new Date();
72
+ date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
73
+ } else {
74
+ date = options.expires;
75
+ }
76
+ expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
77
+ }
78
+ // CAUTION: Needed to parenthesize options.path and options.domain
79
+ // in the following expressions, otherwise they evaluate to undefined
80
+ // in the packed version for some reason...
81
+ var path = options.path ? '; path=' + (options.path) : '';
82
+ var domain = options.domain ? '; domain=' + (options.domain) : '';
83
+ var secure = options.secure ? '; secure' : '';
84
+ document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
85
+ } else { // only name given, get cookie
86
+ var cookieValue = null;
87
+ if (document.cookie && document.cookie != '') {
88
+ var cookies = document.cookie.split(';');
89
+ for (var i = 0; i < cookies.length; i++) {
90
+ var cookie = FBWorld.Helpers.trim(cookies[i]);
91
+ // Does this cookie string begin with the name we want?
92
+ if (cookie.substring(0, name.length + 1) == (name + '=')) {
93
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
94
+ break;
95
+ }
96
+ }
97
+ }
98
+ return cookieValue;
99
+ }
100
+ };
101
+
102
+ //Taken from jQuery
103
+ FBWorld.Helpers.trim = function( text ) {
104
+ return text == null ?
105
+ "" :
106
+ text.toString().replace( /^\s+/, "" ).replace( /\s+$/, "" );
107
+ };