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

fixing lingering problems

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