Skip to content

Commit

Permalink
handle options correcly with the initParams method from request
Browse files Browse the repository at this point in the history
  • Loading branch information
polotek committed Mar 16, 2014
1 parent 242b54b commit b3a467e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 102 deletions.
176 changes: 92 additions & 84 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,30 @@ Yammer.prototype._oauth = function() {
'oauth_verifier="'+this.opts.oauth_verifier+'",' +
'oauth_signature="'+this.opts.oauth_signature+'%26'+this.opts.oauth_token_secret+'"';
}
Yammer.prototype.request = function (opts, cb) {
Yammer.prototype.request = function (uri, opts, cb) {
var args = request.initParams(uri, opts, cb);

// console.log(args);
uri = args.uri;
opts = args.options;
cb = args.callback;

// Remove this ambiguity around which uri is the right one
opts.uri = null;
opts.url = null;

var auth = this._oauth();

if (opts.uri[opts.uri.length - 1] === '.') {
opts.uri += (this.opts.format || 'json');
if (uri[uri.length - 1] === '.') {
uri += (this.opts.format || 'json');
}

if (!opts.headers) { opts.headers = {}; }
if (auth) {
opts.headers.authorization = auth;
}

request(opts, function (e, resp, body) {
request(uri, opts, function (e, resp, body) {
if (e) { return cb(e); }
if (resp.statusCode > 399) {
return cb(new Error('Error status '+resp.statusCode+'\n'), body, resp);
Expand All @@ -125,10 +136,7 @@ Yammer.prototype.request = function (opts, cb) {
} else {
cb(null, body, resp);
}
})
}
Yammer.prototype._get = function (url, cb) {
this.request({uri:url, method:'GET'}, cb);
});
}
Yammer.prototype._formpost = function (url, data, cb) {
this.request({uri:url, method:'POST', headers:{'content-type':'application/x-www-form-urlencoded'}, body:qs.stringify(data)}, cb)
Expand All @@ -146,132 +154,132 @@ Yammer.prototype._boolCb = function(cb) {
return cb(null, body);
};
}
Yammer.prototype.messages = function (cb) {
this._get(this.opts.hostname + '/api/v1/messages.', cb);
Yammer.prototype.messages = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages.', opts, cb);
}
Yammer.prototype.messagesSent = function (cb) {
this._get(this.opts.hostname + '/api/v1/messages/sent.', cb);
Yammer.prototype.messagesSent = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/sent.', opts, cb);
}
Yammer.prototype.messagesReceived = function (cb) {
this._get(this.opts.hostname + '/api/v1/messages/received.', cb);
Yammer.prototype.messagesReceived = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/received.', opts, cb);
}
Yammer.prototype.messagesFollowing = function (cb) {
this._get(this.opts.hostname + '/api/v1/messages/following.', cb);
Yammer.prototype.messagesFollowing = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/following.', opts, cb);
}
Yammer.prototype.messagesFromUser = function (userid, cb) {
this._get(this.opts.hostname + '/api/v1/messages/from_user/'+userid+'.', cb);
Yammer.prototype.messagesFromUser = function (userid, opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/from_user/'+userid+'.', opts, cb);
}
Yammer.prototype.messagesFromBot = function (botid, cb) {
this._get(this.opts.hostname + '/api/v1/messages/bot_user/'+botid+'.', cb);
Yammer.prototype.messagesFromBot = function (botid, opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/bot_user/'+botid+'.', opts, cb);
}
Yammer.prototype.messagesWithTag = function (tagid, cb) {
this._get(this.opts.hostname + '/api/v1/messages/tagged_with/'+tagid+'.', cb);
Yammer.prototype.messagesWithTag = function (tagid, opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/tagged_with/'+tagid+'.', opts, cb);
}
Yammer.prototype.messagesInGroup = function (groupid, cb) {
this._get(this.opts.hostname + '/api/v1/messages/in_group/'+groupid+'.', cb);
Yammer.prototype.messagesInGroup = function (groupid, opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/in_group/'+groupid+'.', opts, cb);
}
Yammer.prototype.messagesFavoritesOf = function (userid, cb) {
this._get(this.opts.hostname + '/api/v1/messages/favorites_of/'+userid+'.', cb);
Yammer.prototype.messagesFavoritesOf = function (userid, opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/favorites_of/'+userid+'.', opts, cb);
}
Yammer.prototype.messagesLikedBy = function (userid, cb) {
this._get(this.opts.hostname + '/api/v1/messages/liked_by/'+userid+'.', cb);
Yammer.prototype.messagesLikedBy = function (userid, opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/liked_by/'+userid+'.', opts, cb);
}
Yammer.prototype.messagesInThread = function (threadid, cb) {
this._get(this.opts.hostname + '/api/v1/messages/in_thread/'+threadid+'.', cb);
Yammer.prototype.messagesInThread = function (threadid, opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/in_thread/'+threadid+'.', opts, cb);
}
Yammer.prototype.messagesAboutTopic = function (id, cb) {
this._get(this.opts.hostname + '/api/v1/messages/about_topic/'+id+'.', cb);
Yammer.prototype.messagesAboutTopic = function (id, opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/about_topic/'+id+'.', opts, cb);
}
Yammer.prototype.messagesPrivate = function (cb) {
this._get(this.opts.hostname + '/api/v1/messages/private.', cb);
Yammer.prototype.messagesPrivate = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/private.', opts, cb);
}
Yammer.prototype.directMessages = Yammer.prototype.messagesPrivate;

Yammer.prototype.messagesInbox = function (cb) {
this._get(this.opts.hostname + '/api/v1/messages/inbox.', cb);
Yammer.prototype.messagesInbox = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/messages/inbox.', opts, cb);
}

Yammer.prototype.groups = function (cb) {
this._get(this.opts.hostname + '/api/v1/groups.', cb);
Yammer.prototype.groups = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/groups.', opts, cb);
}
Yammer.prototype.group = function (id, cb) {
this._get(this.opts.hostname + '/api/v1/groups/'+id+'.', cb);
Yammer.prototype.group = function (id, opts, cb) {
this.request(this.opts.hostname + '/api/v1/groups/'+id+'.', opts, cb);
}

Yammer.prototype.topic = function (id, cb) {
this._get(this.opts.hostname + '/api/v1/topics/'+id+'.', cb);
Yammer.prototype.topic = function (id, opts, cb) {
this.request(this.opts.hostname + '/api/v1/topics/'+id+'.', opts, cb);
}

Yammer.prototype.users = function (cb) {
this._get(this.opts.hostname + '/api/v1/users.', cb);
Yammer.prototype.users = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/users.', opts, cb);
}
Yammer.prototype.user = function (id, cb) {
this._get(this.opts.hostname + '/api/v1/users/'+id+'.', cb);
Yammer.prototype.user = function (id, opts, cb) {
this.request(this.opts.hostname + '/api/v1/users/'+id+'.', opts, cb);
}
Yammer.prototype.userByEmail = function (email, cb) {
this._get(this.opts.hostname + '/api/v1/users/by_email.json?email='+encodeURIComponent(email), cb);
Yammer.prototype.userByEmail = function (email, opts, cb) {
this.request(this.opts.hostname + '/api/v1/users/by_email.json?email='+encodeURIComponent(email), opts, cb);
}

Yammer.prototype.relationships = function (cb) {
this._get(this.opts.hostname + '/api/v1/relationships.', cb);
Yammer.prototype.relationships = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/relationships.', opts, cb);
}

Yammer.prototype.suggestions = function (cb) {
this._get(this.opts.hostname + '/api/v1/suggestions.', cb);
Yammer.prototype.suggestions = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/suggestions.', opts, cb);
}
Yammer.prototype.suggestedUsers = function (cb) {
this._get(this.opts.hostname + '/api/v1/suggestions/users.', cb);
Yammer.prototype.suggestedUsers = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/suggestions/users.', opts, cb);
}

Yammer.prototype.thread = function (id, cb) {
this._get(this.opts.hostname + '/api/v1/threads/'+id+'.', cb);
Yammer.prototype.thread = function (id, opts, cb) {
this.request(this.opts.hostname + '/api/v1/threads/'+id+'.', opts, cb);
}


Yammer.prototype.checkUserSubscription = function (userid, cb) {
var cb = this._boolCb(cb);
this._get(this.opts.hostname + '/api/v1/subscriptions/to_user/'+userid+'.', cb);
Yammer.prototype.checkUserSubscription = function (userid, opts, cb) {
cb = this._boolCb(cb);
this.request(this.opts.hostname + '/api/v1/subscriptions/to_user/'+userid+'.', opts, cb);
}
Yammer.prototype.checkTagSubscription = function (tagid, cb) {
var cb = this._boolCb(cb);
this._get(this.opts.hostname + '/api/v1/subscriptions/to_tag/'+tagid+'.', cb);
Yammer.prototype.checkTagSubscription = function (tagid, opts, cb) {
cb = this._boolCb(cb);
this.request(this.opts.hostname + '/api/v1/subscriptions/to_tag/'+tagid+'.', opts, cb);
}
Yammer.prototype.checkThreadSubscription = function (threadid, cb) {
var cb = this._boolCb(cb);
this._get(this.opts.hostname + '/api/v1/subscriptions/to_thread/'+threadid+'.', cb);
Yammer.prototype.checkThreadSubscription = function (threadid, opts, cb) {
cb = this._boolCb(cb);
this.request(this.opts.hostname + '/api/v1/subscriptions/to_thread/'+threadid+'.', opts, cb);
}
Yammer.prototype.checkTopicSubscription = function (topicid, cb) {
var cb = this._boolCb(cb);
this._get(this.opts.hostname + '/api/v1/subscriptions/to_topic/'+topicid+'.', cb);
Yammer.prototype.checkTopicSubscription = function (topicid, opts, cb) {
cb = this._boolCb(cb);
this.request(this.opts.hostname + '/api/v1/subscriptions/to_topic/'+topicid+'.', opts, cb);
}

Yammer.prototype.search = function (term, cb) {
this._get(this.opts.hostname + '/api/v1/search.json?search='+term, cb);
Yammer.prototype.search = function (term, opts, cb) {
this.request(this.opts.hostname + '/api/v1/search.json?search='+term, opts, cb);
}

Yammer.prototype.networks = function (cb) {
this._get(this.opts.hostname + '/api/v1/networks/current.', cb);
Yammer.prototype.networks = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/networks/current.', opts, cb);
}

Yammer.prototype.invite = function (email, cb) {
this._post(this.opts.hostname + '/api/v1/invitations.', {email:email}, cb);
Yammer.prototype.invite = function (email, opts, cb) {
this._post(this.opts.hostname + '/api/v1/invitations.', {email:email}, opts, cb);
}
Yammer.prototype.createMessage = function (obj, cb) {
this._formpost(this.opts.hostname + '/api/v1/messages.json', obj, cb);
Yammer.prototype.createMessage = function (obj, opts, cb) {
this._formpost(this.opts.hostname + '/api/v1/messages.json', obj, opts, cb);
}
Yammer.prototype.likeMessage = function(obj, cb) {
this._formpost(this.opts.hostname + '/api/v1/messages/liked_by', obj, cb);
Yammer.prototype.likeMessage = function(obj, opts, cb) {
this._formpost(this.opts.hostname + '/api/v1/messages/liked_by', obj, opts, cb);
}

Yammer.prototype.presences = function (cb) {
this._get(this.opts.hostname + '/api/v1/presences.', cb);
Yammer.prototype.presences = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/presences.', opts, cb);
}
Yammer.prototype.presencesByFollowing = function (cb) {
this._get(this.opts.hostname + '/api/v1/presences/by_following.', cb);
Yammer.prototype.presencesByFollowing = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/presences/by_following.', opts, cb);
}

Yammer.prototype.tokens = function (cb) {
this._get(this.opts.hostname + '/api/v1/oauth/tokens.', cb)
Yammer.prototype.tokens = function (opts, cb) {
this.request(this.opts.hostname + '/api/v1/oauth/tokens.', opts, cb)
}

exports.Yammer = Yammer
37 changes: 19 additions & 18 deletions tests.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var test = require('tape')
, mock = require('mock')
, sinon = require('sinon')
, request = require('request')
, nop = function() {}
, requestMock = nop;

var requestModule = function() {
return requestMock.apply(null, arguments);
};
requestModule.initParams = request.initParams;

var yammer = mock('./main', {
request: function() {
return requestMock.apply(null, arguments);
}
request: requestModule
}, require)
, Yammer = yammer.Yammer;

Expand All @@ -23,9 +27,8 @@ test('access_token is added as authorization header', function(t) {
uri: '/test'
}, nop);

t.ok(requestMock.calledWith({
uri: '/test'
, headers: {
t.ok(requestMock.calledWithMatch('/test', {
headers: {
authorization: 'Bearer test_token'
}
}), 'oauth token present in headers');
Expand All @@ -42,14 +45,14 @@ test('json format is added by default', function(t) {
uri: '/test.'
}, nop);

t.ok(requestMock.calledWithMatch({ uri: '/test.json' }), 'url has json format');
t.ok(requestMock.calledWithMatch('/test.json'), 'url has json format');

return t.end();
});


test('json response is parsed automatically', function(t) {
requestMock = sinon.stub().callsArgWith(1
requestMock = sinon.stub().callsArgWith(2
, null
, {
statusCode: 200
Expand All @@ -66,12 +69,12 @@ test('json response is parsed automatically', function(t) {
uri: '/test.json'
}, spy);

t.ok(spy.calledWith(null, { test: 'json' }), 'json response is parsed');
t.ok(spy.calledWithMatch(null, { test: 'json' }), 'json response is parsed');
return t.end();
});

test('400 response returns error', function(t) {
requestMock = sinon.stub().callsArgWith(1
requestMock = sinon.stub().callsArgWith(2
, null
, {
statusCode: 404
Expand All @@ -90,7 +93,7 @@ test('400 response returns error', function(t) {
});

test('500 response returns error', function(t) {
requestMock = sinon.stub().callsArgWith(1
requestMock = sinon.stub().callsArgWith(2
, null
, {
statusCode: 500
Expand All @@ -109,7 +112,7 @@ test('500 response returns error', function(t) {
});

test('boolean callbacks return false for 404', function(t) {
requestMock = sinon.stub().callsArgWith(1
requestMock = sinon.stub().callsArgWith(2
, null
, {
statusCode: 404
Expand Down Expand Up @@ -150,9 +153,8 @@ test('qs property is passed through as query parameters', function(t) {
}
}, nop);

t.ok(requestMock.calledWith({
uri: '/test?test_prop=test_value'
}), 'yam.request sends query parameters');
t.ok(requestMock.calledWithMatch('/test?test_prop=test_value')
, 'yam.request sends query parameters');

yam.messages({
uri: '/test'
Expand All @@ -162,9 +164,8 @@ test('qs property is passed through as query parameters', function(t) {
}
}, nop);

t.ok(requestMock.calledWith({
uri: '/test'
, qs: {
t.ok(requestMock.calledWithMatch('/test', {
qs: {
test_prop: "test_value"
}
}), 'post to yam.messages sends query paramters');
Expand Down

0 comments on commit b3a467e

Please sign in to comment.