Permalink
Browse files

Create new file for store root class.

  • Loading branch information...
llun committed Mar 2, 2013
1 parent c82f96c commit 3466ccfb1069dc59bb06ecc38d0def0643f6ffe1
View
@@ -6,7 +6,7 @@ var Service = require('../models/service');
module.exports = {
authorize: function (req, res) {
- q.nfcall(services.get.bind(services), req.body.service, req.body.oauth_key, req.body.oauth_secret)
+ q.nfcall(services.get.bind(services), req.body.service, req.body.oauth_key, req.body.oauth_secret, req.body.url)
.then(function (service) {
service.authorize(req, res);
})
View
@@ -2,8 +2,8 @@ var util = require('util');
var Service = require('./oauth2');
-var Foursquare = function (key, secret, url) {
- Service.call(this, key, secret, url, 'https://api.foursquare.com/v2');
+var Foursquare = function (key, secret, accountUrl, url) {
+ Service.call(this, key, secret, accountUrl, url, 'https://api.foursquare.com/v2');
}
util.inherits(Foursquare, Service);
View
@@ -0,0 +1,8 @@
+var Service = function (key, secret, accountUrl, rootUrl) {
+ this.key = key;
+ this.secret = secret;
+ this.accountUrl = accountUrl;
+ this.url = rootUrl;
+}
+
+module.exports = Service;
View
@@ -1,35 +1,27 @@
var querystring = require('querystring'),
- OAuth2 = require('oauth').OAuth2;
+ OAuth2 = require('oauth').OAuth2,
+ util = require('util');
+
+var Service = require('./oauth2');
/**
* Facebook service class
*
* @param key, application oauth id from facebook developer page
* @param secret, application oauth secret from facebook developer page
+ * @param accountUrl, User URL
* @param url, web root url for redirect back from service
* @param scope, Facebook scope array
* @constructor
*/
-var Facebook = function (key, secret, url, scope) {
- this.key = key;
- this.secret = secret;
- this.url = url;
+var Facebook = function (key, secret, accountUrl, url, scope) {
+ Service.call(this, key, secret, accountUrl, url, 'https://www.googleapis.com/oauth2/v1');
+
// TODO: Find place to keep this settings.
this.scope = scope || [ 'read_stream' ];
-
- this.oauth = new OAuth2(key, secret, 'https://graph.facebook.com');
}
-Facebook.prototype.authorize = function (req, res) {
- res.redirect(this.authorizeUrl());
-}
-
-Facebook.prototype.callback = function (req, cb) {
- this.accessToken(this.authorizeUrl(), req.query.code,
- function (err, token) {
- cb(err, token);
- });
-}
+util.inherits(Facebook, Service);
/**
* Return authorize url for redirect by browser
View
@@ -1,9 +1,10 @@
-var OAuth = require('oauth').OAuth;
+var OAuth = require('oauth').OAuth,
+ util = require('util');
-var Flickr = function (key, secret, url) {
- this.key = key;
- this.secret = secret;
- this.url = url;
+var Service = require('./class');
+
+var Flickr = function (key, secret, accountUrl, url) {
+ Service.call(this, key, secret, accountUrl, url);
this.oauth = new OAuth(
'http://www.flickr.com/services/oauth/request_token',
@@ -15,6 +16,8 @@ var Flickr = function (key, secret, url) {
'HMAC-SHA1');
}
+util.inherits(Flickr, Service);
+
Flickr.secrets = {};
Flickr.prototype.authorize = function (req, res) {
View
@@ -2,8 +2,8 @@ var util = require('util');
var Service = require('./oauth2');
-var Github = function (key, secret, url) {
- Service.call(this, key, secret, url, 'https://api.github.com');
+var Github = function (key, secret, accountUrl, url) {
+ Service.call(this, key, secret, accountUrl, url, 'https://api.github.com');
}
util.inherits(Github, Service);
View
@@ -2,8 +2,8 @@ var util = require('util');
var Service = require('./oauth2');
-var GPlus = function (key, secret, url) {
- Service.call(this, key, secret, url, 'https://www.googleapis.com/oauth2/v1');
+var GPlus = function (key, secret, accountUrl, url) {
+ Service.call(this, key, secret, accountUrl, url, 'https://www.googleapis.com/oauth2/v1');
}
util.inherits(GPlus, Service);
View
@@ -3,10 +3,7 @@ var q = require('q'),
var Model = require('../models/service');
-var Service = function (key, secret, authorizeUrl) {
- this.key = key;
- this.secret = secret;
-}
+var Service = {};
// Supported services
Service.facebook = require('./facebook');
@@ -24,9 +21,10 @@ Service.linkedin = require('./linkedin');
* @param {String} name, service name
* @param {String} key, oauth key or id (optional)
* @param {String} secret, oauth secret (optional)
+ * @param {String} url, user account url (optional)
* @param {String} cb, callback
*/
-Service.get = function (name, key, secret, cb) {
+Service.get = function (name, key, secret, url, cb) {
var parameters = util.parameters(arguments);
var root = process.env.ROOT_URL || 'http://local.llun.in.th:3000';
@@ -37,16 +35,17 @@ Service.get = function (name, key, secret, cb) {
return q.nfcall(Model.create.bind(Model), {
name: parameters.name,
key: parameters.key,
- secret: parameters.secret
+ secret: parameters.secret,
+ accountUrl: parameters.url
});
}
else {
- parameters.cb(null, new Service[name](service.key, service.secret, root));
+ parameters.cb(null, new Service[name](service.key, service.secret, service.accountUrl, root));
}
})
.then(function (doc) {
if (doc) {
- parameters.cb(null, new Service[name](doc.key, doc.secret, root));
+ parameters.cb(null, new Service[name](doc.key, doc.secret, doc.accountUrl, root));
}
else {
parameters.cb(err);
@@ -83,4 +82,4 @@ Service.remove = function (id, cb) {
});
}
-module.exports = Service;
+module.exports = Service;
View
@@ -2,8 +2,8 @@ var util = require('util');
var Service = require('./oauth2');
-var Instagram = function (key, secret, url) {
- Service.call(this, key, secret, url, 'https://api.instagram.com/v1');
+var Instagram = function (key, secret, accountUrl, url) {
+ Service.call(this, key, secret, accountUrl, url, 'https://api.instagram.com/v1');
}
util.inherits(Instagram, Service);
View
@@ -2,8 +2,8 @@ var util = require('util');
var Service = require('./oauth2');
-var Linkedin = function (key, secret, url) {
- Service.call(this, key, secret, url, 'http://api.linkedin.com/v1');
+var Linkedin = function (key, secret, accountUrl, url) {
+ Service.call(this, key, secret, accountUrl, url, 'http://api.linkedin.com/v1');
}
util.inherits(Linkedin, Service);
View
@@ -1,13 +1,15 @@
-var OAuth2 = require('oauth').OAuth2;
+var OAuth2 = require('oauth').OAuth2,
+ util = require('util');
-var OAuth2Service = function (key, secret, url, api) {
- this.key = key;
- this.secret = secret;
- this.url = url;
+var Service = require('./class');
+var OAuth2Service = function (key, secret, accountUrl, rootUrl, api) {
+ Service.call(this, key, secret, accountUrl, rootUrl);
this.oauth = new OAuth2(key, secret, api);
}
+util.inherits(OAuth2Service, Service);
+
OAuth2Service.prototype.authorize = function (req, res) {
res.redirect(this.authorizeUrl());
}
View
@@ -1,23 +1,23 @@
-var OAuth = require('oauth').OAuth;
+var OAuth = require('oauth').OAuth,
+ util = require('util');
-var requestTokenURL = 'https://api.twitter.com/oauth/request_token',
- accessTokenURL = 'https://api.twitter.com/oauth/access_token';
+var Service = require('./class');
-
-var Twitter = function (key, secret, url) {
- this.key = key;
- this.secret = secret;
+var Twitter = function (key, secret, accountUrl, url) {
+ Service.call(this, key, secret, accountUrl, url);
this.oauth = new OAuth(
- requestTokenURL,
- accessTokenURL,
+ 'https://api.twitter.com/oauth/request_token',
+ 'https://api.twitter.com/oauth/access_token',
key,
secret,
'1.0A',
null,
'HMAC-SHA1');
}
+util.inherits(Twitter, Service);
+
Twitter.secrets = {};
Twitter.prototype.authorize = function (req, res) {
@@ -22,7 +22,7 @@ describe('routes/services.js', function () {
it ('should send req and res to service to authorize', function (done) {
var deferred = q.defer();
- var stub = sinon.stub(Services, 'get', function (service, key, secret, cb) {
+ var stub = sinon.stub(Services, 'get', function (service, key, secret, url, cb) {
cb(null, {
authorize: function (_req, _res) {
_res.redirect();
@@ -33,7 +33,8 @@ describe('routes/services.js', function () {
body: {
oauth_key: 'key',
oauth_secret: 'secret',
- service: 'name'
+ service: 'name',
+ url: 'http://somewhere.else/user'
}
};
@@ -46,6 +47,12 @@ describe('routes/services.js', function () {
routes.authorize(req, res);
deferred.promise.should.be.fulfilled.then(function () {
+ var arguments = stub.args[0];
+ arguments[0].should.equal('name');
+ arguments[1].should.equal('key');
+ arguments[2].should.equal('secret');
+ arguments[3].should.equal('http://somewhere.else/user');
+
stub.restore();
}).should.notify(done);
Oops, something went wrong.

0 comments on commit 3466ccf

Please sign in to comment.