description and source-codefunction TinderClient() {
var xAuthToken = null;
this.lastActivity = new Date();
var _this = this;
this.userId = null;
/**
* Helper for getting the request headers
*/
var getRequestHeaders = function() {
var headers = {
'User-Agent' : 'Tinder Android Version 4.5.5',
'os_version' : '23',
'platform' : 'android',
'app-version' : '854',
'Accept-Language' : 'en'
};
if (xAuthToken) {
headers['X-Auth-Token'] = xAuthToken;
}
return headers;
};
/**
* Issues a GET request to the Tinder API
* @param {String} path the relative path
* @param {Object} data an object containing extra values
* @param {Function} callback the callback to invoke when the request completes
*/
var tinderGet = function(path, data, callback) {
request.get(TINDER_HOST + path)
.set(getRequestHeaders())
.end(callback)
};
/**
* Issues a POST request to the Tinder API
* @param {String} path the relative path
* @param {Object} data an object containing extra values
* @param {Function} callback the callback to invoke when the request completes
*/
var tinderPost = function(path, data, callback) {
request.post(TINDER_HOST + path)
.set(getRequestHeaders())
.send(data)
.end(callback)
};
/**
* Issues a PUT request to the Tinder API
* @param {String} path the relative path
* @param {Object} data an object containing extra values
* @param {Function} callback the callback to invoke when the request completes
*/
var tinderPut = function(path, data, callback) {
request.put(TINDER_HOST + path)
.set(getRequestHeaders())
.send(data)
.end(callback)
};
/**
* Issues a DELETE request to the Tinder API
* @param {String} path the relative path
* @param {Object} data an object containing extra values
* @param {Function} callback the callback to invoke when the request completes
*/
var tinderDelete = function(path, data, callback) {
request.del(TINDER_HOST + path)
.set(getRequestHeaders())
.send(data)
.end(callback)
};
/**
* Helper for transforming the request callback values
* @param {Function} callback the callback
*/
var makeTinderCallback = function(callback) {
return function(error, res) {
var data = null;
if (!error) {
if (typeof res.body === "string")
{
try
{
data = JSON.parse(res.body);
} catch (err) {
error = data;
}
}
else if (typeof res.body === "object") {
data = res.body;
}
}
if (data && data.status && data.status !== 200) {
error = data;
}
if (callback) {
callback(error, data);
}
};
};
/**
* Authorize this tinder client
* @param {String} fbToken the Facebook token. This will be obtained when authenticating the user
* @param {String} fbId the Facebook user id.
* @param {Function} callback the callback to invoke when the request completes
*/
this.authorize = function(fbToken, fbId, callback) {
tinderPost('auth',
{
facebook_token: fbToken,
facebook_id: fbId,
locale: 'en'
},
function(error, res) {
// If no body is passed back, return an error
if(res.body === undefined){
error = new Error('No token passed back from Tinder')
}
var body = res.body || { 'token': null };
if (!error && body.token) {
xAuthToken = body.token;
_this.userId = body.user._id;
_this.defaults = body;
callback = makeTinderCallback(callback);
callback(error, res);
} else if (body.error){
error = "Failed to authenticate: " + body.error
callback(error, res);
} else {
callback(error, res);
}
});
};
/**
* Set auth token if you have it saved, no need to do fb log ...