Commit 3324923e authored by Aaron Tidwell's avatar Aaron Tidwell

fixing lingering problems

parent b5b6ccdf
......@@ -12,6 +12,7 @@ var errorHandler = require('./error-handler');
*/
var Client = exports.Client = function(options) {
this.options = options;
if (!this.options.version) { this.options.version = 1; }
// add a getter to the options passed in - DO NOT mess with instance configs in resources
if (typeof this.options.get !== 'function') {
......@@ -21,6 +22,28 @@ var Client = exports.Client = function(options) {
}
};
// serialize nested params to tournament[name] style
function serializeProperties(obj) {
console.log(obj)
var compiledParams = '';
var serializedProperties = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (typeof(obj[prop]) === 'object') {
for (var attr in obj[prop]) {
compiledParams += '&';
compiledParams += prop + '[' + attr + ']=' + encodeURIComponent(obj[prop][attr]);
}
serializedProperties.push(prop);
}
}
}
return {
serialized: compiledParams,
properties: serializedProperties
};
}
var propertiesToDelete = ['callback', 'path', 'method']; // resources generate props internal to https requests
// cleans the passed in object, generates the API url/query-string, makes the request, delegates errors and calls callbacks
......@@ -34,19 +57,11 @@ Client.prototype.makeRequest = function(obj) {
// massage camel to underscore
obj.api_key = this.options.get('apiKey'); //convert for url
// serialize nested params to tournament[name] style
var compiledParams = '';
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (typeof(obj[prop]) === 'object') {
for (var attr in obj[prop]) {
compiledParams += '&';
compiledParams += prop + '[' + attr + ']=' + encodeURIComponent(obj[prop][attr]);
}
propertiesToDelete.push(prop);
}
}
}
//serialize the properties
var serialized = serializeProperties(obj);
var compiledParams = serialized.serialized;
//merge the stuff to remove
propertiesToDelete = propertiesToDelete.concat(serialized.properties);
// remove params
propertiesToDelete.forEach(function(prop) {
......@@ -54,9 +69,12 @@ Client.prototype.makeRequest = function(obj) {
});
// generate path
path = path + '.' + this.options.get('format') + '?' + qs.stringify(obj) + compiledParams;
var versionPaths = {
1: '/v1/tournaments'
};
path = versionPaths[this.options.get('version')] + (path ? path : '') + '.' + this.options.get('format') + '?' + qs.stringify(obj) + compiledParams;
// opts for the https call
// create options for the https call
var options = {
hostname: 'api.challonge.com',
path: path,
......@@ -66,8 +84,6 @@ Client.prototype.makeRequest = function(obj) {
}
};
console.log('making request to ', options);
var req = https.request(options, function(res) {
// store the chunked data as it comes back
var resData = '';
......@@ -78,7 +94,7 @@ Client.prototype.makeRequest = function(obj) {
res.on('end', function() {
// error
if (res.statusCode !== 200) {
errorHandler.handle(res, resData, callback);
errorHandler.handle(res, resData, callback, self.options.get('format'));
return;
}
// 200 ok
......
......@@ -6,7 +6,7 @@
// 404 - Object not found within your account scope
// 422 - Validation error(s) for create or update method
exports.handle = function(res, resData, callback) {
exports.handle = function(res, resData, callback, format) {
var err;
// 401 - Invalid API key
if (res.statusCode === 401) {
......
......@@ -13,14 +13,14 @@ var Matches = exports.Matches = function(options) {
util.inherits(Matches, Client);
Matches.prototype.index = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/matches';
obj.path = '/'+obj.id + '/matches';
delete obj.id;
obj.method = 'GET';
this.makeRequest(obj);
};
Matches.prototype.show = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/matches/'+obj.matchId;
obj.path = '/'+obj.id + '/matches/' + obj.matchId;
delete obj.id;
delete obj.matchId;
obj.method = 'GET';
......@@ -28,7 +28,7 @@ Matches.prototype.show = function(obj) {
};
Matches.prototype.update = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/matches/'+obj.matchId;
obj.path = '/'+obj.id + '/matches/' + obj.matchId;
delete obj.id;
delete obj.matchId;
obj.method = 'PUT';
......
......@@ -21,21 +21,21 @@ var Participants = exports.Participants = function(options) {
util.inherits(Participants, Client);
Participants.prototype.index = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants';
obj.path = '/' + obj.id + '/participants';
delete obj.id;
obj.method = 'GET';
this.makeRequest(obj);
};
Participants.prototype.create = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants';
obj.path = '/' + obj.id + '/participants';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
};
Participants.prototype.show = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants/'+obj.participantId;
obj.path = '/' + obj.id + '/participants/' + obj.participantId;
delete obj.id;
delete obj.participantId;
obj.method = 'GET';
......@@ -43,7 +43,7 @@ Participants.prototype.show = function(obj) {
};
Participants.prototype.update = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants/'+obj.participantId;
obj.path = '/' + obj.id + '/participants/' + obj.participantId;
delete obj.id;
delete obj.participantId;
obj.method = 'PUT';
......@@ -51,7 +51,7 @@ Participants.prototype.update = function(obj) {
};
Participants.prototype.destroy = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants/'+obj.participantId;
obj.path = '/' + obj.id + '/participants/' + obj.participantId;
delete obj.id;
delete obj.participantId;
obj.method = 'DELETE';
......@@ -59,7 +59,7 @@ Participants.prototype.destroy = function(obj) {
};
Participants.prototype.randomize = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/participants/randomize';
obj.path = '/' + obj.id + '/participants/randomize';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
......
......@@ -18,54 +18,52 @@ var Tournaments = exports.Tournaments = function(options) {
util.inherits(Tournaments, Client);
Tournaments.prototype.index = function(obj) {
obj.path = '/v1/tournaments';
obj.method = 'GET';
this.makeRequest(obj);
};
Tournaments.prototype.create = function(obj) {
obj.path = '/v1/tournaments';
obj.method = 'POST';
this.makeRequest(obj);
};
Tournaments.prototype.show = function(obj) {
obj.path = '/v1/tournaments/'+obj.id;
obj.path = '/' + obj.id;
delete obj.id;
obj.method = 'GET';
this.makeRequest(obj);
};
Tournaments.prototype.update = function(obj) {
obj.path = '/v1/tournaments/'+obj.id;
obj.path = '/' + obj.id;
delete obj.id;
obj.method = 'PUT';
this.makeRequest(obj);
};
Tournaments.prototype.destroy = function(obj) {
obj.path = '/v1/tournaments/'+obj.id;
obj.path = '/' + obj.id;
delete obj.id;
obj.method = 'DELETE';
this.makeRequest(obj);
};
Tournaments.prototype.start = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/start';
obj.path = '/' + obj.id + '/start';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
};
Tournaments.prototype.finalize = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/finalize';
obj.path = '/' + obj.id + '/finalize';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
};
Tournaments.prototype.reset = function(obj) {
obj.path = '/v1/tournaments/'+obj.id+'/reset';
obj.path = '/' + obj.id + '/reset';
delete obj.id;
obj.method = 'POST';
this.makeRequest(obj);
......
......@@ -51,10 +51,6 @@ client.tournaments.create({
###Challonge docs: http://api.challonge.com/v1
##TODO
* move /v1/tournaments to config
* abstract path suffix for partcipants, matches
* abstract destroy of qs params
2. support camelCase -> under_score params
3. validate required params
4. docs
......
......@@ -2,10 +2,11 @@ var challonge = require('./../');
var client = challonge.createClient({
apiKey: require('./../key.js'),
format: 'json'
format: 'json',
version: 1
});
var tourneyName = '710101';
var tourneyName = 'adfasdfasdf3333';
function index() {
client.tournaments.index({
......@@ -43,7 +44,7 @@ function show() {
function update() {
client.tournaments.update({
id: 'test-tourney',
id: tourneyName,
tournament: {
name: 'renamed test tournet'
},
......@@ -207,7 +208,7 @@ function mupdate() {
//show();
//update();
//destroy();
//start();
start();
//finalize();
//reset();
......@@ -216,7 +217,7 @@ function mupdate() {
//pshow();
//pupdate();
//pdestroy();
prandomize();
//prandomize();
//mindex();
//mshow();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment