Commit c6a1ff6a authored by Aaron Tidwell's avatar Aaron Tidwell

subdomain support

parent 3324923e
...@@ -14,6 +14,8 @@ var Client = exports.Client = function(options) { ...@@ -14,6 +14,8 @@ var Client = exports.Client = function(options) {
this.options = options; this.options = options;
if (!this.options.version) { this.options.version = 1; } if (!this.options.version) { this.options.version = 1; }
this.setSubdomain(this.options.subdomain);
// 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') {
this.options.get = function(key) { this.options.get = function(key) {
...@@ -24,7 +26,6 @@ var Client = exports.Client = function(options) { ...@@ -24,7 +26,6 @@ var Client = exports.Client = function(options) {
// serialize nested params to tournament[name] style // serialize nested params to tournament[name] style
function serializeProperties(obj) { function serializeProperties(obj) {
console.log(obj)
var compiledParams = ''; var compiledParams = '';
var serializedProperties = []; var serializedProperties = [];
for (var prop in obj) { for (var prop in obj) {
...@@ -46,6 +47,50 @@ function serializeProperties(obj) { ...@@ -46,6 +47,50 @@ function serializeProperties(obj) {
var propertiesToDelete = ['callback', 'path', 'method']; // resources generate props internal to https requests var propertiesToDelete = ['callback', 'path', 'method']; // resources generate props internal to https requests
//all the stuff we have to convert camelCase to underScores
var propertiesToConvert = [
'create_after',
'created_before',
'include_participants',
'include_matches',
'tournament_type',
'open_signup',
'hold_third_place_match',
'pts_for_match_win',
'pts_for_match_tie',
'pts_for_game_win',
'pts_for_game_tie',
'pts_for_bye',
'swiss_rounds',
'ranked_by',
'rr_pts_for_match_win',
'rr_pts_for_match_tie',
'rr_pts_for_game_win',
'rr_pts_for_game_tie',
'accept_attachments',
'hide_forum',
'show_rounds',
'notify_users_when_matches_open',
'notify_users_when_the_tournament_ends',
'sequential_pairings',
'signup_cap',
'challonge_username',
'participant_id',
'scores_csv',
'winner_id',
'player1_votes',
'player2_votes',
];
Client.prototype.setSubdomain = function(subdomain) {
//generate the subdomain URL string if there is one
if (!subdomain) {
this.options.subdomain = '';
} else if (this.options.subdomain.indexOf('-') === -1) {
this.options.subdomain = subdomain + '-';
}
};
// 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
Client.prototype.makeRequest = function(obj) { Client.prototype.makeRequest = function(obj) {
var self = this; var self = this;
......
...@@ -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 = '/'+obj.id + '/matches'; obj.path = '/' + this.options.get('subdomain') + 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 = '/'+obj.id + '/matches/' + obj.matchId; obj.path = '/' + this.options.get('subdomain') + 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 = '/'+obj.id + '/matches/' + obj.matchId; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/participants'; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/participants'; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/participants/' + obj.participantId; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/participants/' + obj.participantId; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/participants/' + obj.participantId; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/participants/randomize'; obj.path = '/' + this.options.get('subdomain') + obj.id + '/participants/randomize';
delete obj.id; delete obj.id;
obj.method = 'POST'; obj.method = 'POST';
this.makeRequest(obj); this.makeRequest(obj);
......
...@@ -12,6 +12,10 @@ var Client = require('./client').Client; ...@@ -12,6 +12,10 @@ var Client = require('./client').Client;
var Tournaments = exports.Tournaments = function(options) { var Tournaments = exports.Tournaments = function(options) {
Client.call(this, options); // call parent constructor Client.call(this, options); // call parent constructor
this.getRawSubdomain = function() {
return this.options.get('subdomain').replace('-','');
};
}; };
// Inherit from Client base object // Inherit from Client base object
...@@ -19,51 +23,61 @@ util.inherits(Tournaments, Client); ...@@ -19,51 +23,61 @@ util.inherits(Tournaments, Client);
Tournaments.prototype.index = function(obj) { Tournaments.prototype.index = function(obj) {
obj.method = 'GET'; obj.method = 'GET';
// generate the subdomain property from the subdomain
// url we generate in the client constructor
if (this.getRawSubdomain()) {
obj.subdomain = this.getRawSubdomain();
}
this.makeRequest(obj); this.makeRequest(obj);
}; };
Tournaments.prototype.create = function(obj) { Tournaments.prototype.create = function(obj) {
// generate the subdomain property from the subdomain
// url we generate in the client constructor
if (this.getRawSubdomain()) {
obj.tournament.subdomain = this.getRawSubdomain();
}
obj.method = 'POST'; obj.method = 'POST';
this.makeRequest(obj); this.makeRequest(obj);
}; };
Tournaments.prototype.show = function(obj) { Tournaments.prototype.show = function(obj) {
obj.path = '/' + obj.id; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/start'; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/finalize'; obj.path = '/' + this.options.get('subdomain') + 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 = '/' + obj.id + '/reset'; obj.path = '/' + this.options.get('subdomain') + obj.id + '/reset';
delete obj.id; delete obj.id;
obj.method = 'POST'; obj.method = 'POST';
this.makeRequest(obj); this.makeRequest(obj);
......
...@@ -20,5 +20,12 @@ exports.createClient = function createClient(options) { ...@@ -20,5 +20,12 @@ exports.createClient = function createClient(options) {
client[endpoint] = new exports[k](options); // store for the user to reference via instance.resource client[endpoint] = new exports[k](options); // store for the user to reference via instance.resource
}); });
client.setSubdomain = function(subdomain) {
parts.forEach(function update(k) {
var endpoint = k.toLowerCase();
client[endpoint].setSubdomain(subdomain);
});
};
return client; return client;
}; };
\ No newline at end of file
...@@ -38,6 +38,7 @@ client.tournaments.create({ ...@@ -38,6 +38,7 @@ client.tournaments.create({
name: 'new_tournament_name', name: 'new_tournament_name',
url: 'new_tournament_url', //also can be used as id url: 'new_tournament_url', //also can be used as id
tournament_type: 'single elimination', tournament_type: 'single elimination',
subdomain: 'challongeOrganizationSubdomain' //optional (must be passed or setSubdomain called for use with organizations), used to automatically pass tournament[subdomain] and prefixes to tournament urls
}, },
callback: function(err,data){ callback: function(err,data){
if (err) { console.log(err); return; } if (err) { console.log(err); return; }
......
...@@ -3,10 +3,10 @@ var challonge = require('./../'); ...@@ -3,10 +3,10 @@ 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 version: 1,
}); });
var tourneyName = 'adfasdfasdf3333'; var tourneyName = 'nodeapitest3';
function index() { function index() {
client.tournaments.index({ client.tournaments.index({
...@@ -122,7 +122,7 @@ function pcreate() { ...@@ -122,7 +122,7 @@ function pcreate() {
function pshow() { function pshow() {
client.participants.show({ client.participants.show({
id: tourneyName, id: tourneyName,
participantId: 10846707, participantId: 11150708,
callback: function(err,data){ callback: function(err,data){
if (err) { console.log(err); return; } if (err) { console.log(err); return; }
console.log(data); console.log(data);
...@@ -133,7 +133,7 @@ function pshow() { ...@@ -133,7 +133,7 @@ function pshow() {
function pupdate() { function pupdate() {
client.participants.update({ client.participants.update({
id: tourneyName, id: tourneyName,
participantId: 10846707, participantId: 11150708,
participant: { participant: {
name: 'updatdguy' name: 'updatdguy'
}, },
...@@ -204,11 +204,12 @@ function mupdate() { ...@@ -204,11 +204,12 @@ function mupdate() {
//index(); //index();
//create(); client.setSubdomain('nodeapitest');
create();
//show(); //show();
//update(); //update();
//destroy(); //destroy();
start(); //start();
//finalize(); //finalize();
//reset(); //reset();
......
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