This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
/
factory.js
78 lines (73 loc) · 1.73 KB
/
factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* OAuth Manager Factory
* @author Tang Bo Hao
*/
/**
* configure social
*
* @param platforms
* {platform_name : {key: key, secret: secret}}
*
* you can set multi platforms
*
* usage:
*
* var factory = require('social')({
* renren: {
* key : '.....'
* , secret : '.....'
* }
* });
*
* factory.createClient('renren'); // create RenRen Client
*
* factory.setDefault('renren'); // set RenRen as the default platform
* factory.createClient(); // now also can create RenRenClient
*
*/
module.exports = function (configs, is_debug_mode) {
var Factory = {};
for(var name in configs) {
var clientClass;
switch(name) {
case 'wyx':
clientClass = require('./wyx');
break;
case 'renren':
clientClass = require('./renren');
break;
case 'qq':
clientClass = require('./qq');
break;
default:
}
configs[name].platform = name;
configs[name].clientClass = clientClass;
}
/**
* Create a social client
* @param {String/Object} snsInfo platform name or snsInfo from getAppEntryInfo
* @return {Object} Social platform instance
*/
Factory.createClient = function(snsInfo) {
var pf;
if( typeof snsInfo === "string"){
pf = snsInfo;
snsInfo = null;
}else{
pf = snsInfo.platform;
}
var appinfo = configs[pf];
// no appinfo, return null
if(!appinfo) return null;
return new appinfo.clientClass(appinfo, snsInfo, is_debug_mode);
};
Factory.getAppEntryInfo = function(req) {
for(var name in configs) {
var config = configs[name];
var entryInfo = config.clientClass.getAppEntryInfo(req, config.secret);
if(entryInfo) return entryInfo;
}
};
return Factory;
};