Commit e545b700 authored by Aaron Tidwell's avatar Aaron Tidwell

cameCase to under_score property conversion

parent c6a1ff6a
...@@ -45,42 +45,34 @@ function serializeProperties(obj) { ...@@ -45,42 +45,34 @@ function serializeProperties(obj) {
}; };
} }
var propertiesToDelete = ['callback', 'path', 'method']; // resources generate props internal to https requests // resources generate props internal to https requests
var propertiesToDelete = ['callback', 'path', 'method'];
//all the stuff we have to convert camelCase to underScores
var propertiesToConvert = [ // all the stuff we have to convert camelCase to under_scores
'create_after', var propertiesToConvert = require('./properties-to-convert');
'created_before',
'include_participants', function camelToUnderscore(str) {
'include_matches', return str.replace(/\W+/g, '-')
'tournament_type', .replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase();
'open_signup', }
'hold_third_place_match', function convertPropertyCamelCaseToUnderscore(obj) {
'pts_for_match_win', for (var prop in obj) {
'pts_for_match_tie', if (obj.hasOwnProperty(prop)) {
'pts_for_game_win', //objects recurse
'pts_for_game_tie', if (typeof obj[prop] === 'object') {
'pts_for_bye', obj[camelToUnderscore(prop)] = convertPropertyCamelCaseToUnderscore(obj[prop]);
'swiss_rounds', }
'ranked_by', //otherwise, if the underscore version is in the list we convert it
'rr_pts_for_match_win', else if (propertiesToConvert.indexOf(camelToUnderscore(prop)) !== -1) {
'rr_pts_for_match_tie', obj[camelToUnderscore(prop)] = obj[prop];
'rr_pts_for_game_win', //remove it
'rr_pts_for_game_tie', delete obj[prop];
'accept_attachments', }
'hide_forum', //otherwise leave it alone
'show_rounds', }
'notify_users_when_matches_open', }
'notify_users_when_the_tournament_ends', return obj;
'sequential_pairings', }
'signup_cap',
'challonge_username',
'participant_id',
'scores_csv',
'winner_id',
'player1_votes',
'player2_votes',
];
Client.prototype.setSubdomain = function(subdomain) { Client.prototype.setSubdomain = function(subdomain) {
//generate the subdomain URL string if there is one //generate the subdomain URL string if there is one
...@@ -102,6 +94,10 @@ Client.prototype.makeRequest = function(obj) { ...@@ -102,6 +94,10 @@ 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
// normalize the rest of the properties
obj = convertPropertyCamelCaseToUnderscore(obj);
console.log(obj)
//serialize the properties //serialize the properties
var serialized = serializeProperties(obj); var serialized = serializeProperties(obj);
var compiledParams = serialized.serialized; var compiledParams = serialized.serialized;
......
//all the stuff we have to convert camelCase to under_scores
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',
];
module.exports = propertiesToConvert;
\ No newline at end of file
...@@ -52,10 +52,9 @@ client.tournaments.create({ ...@@ -52,10 +52,9 @@ client.tournaments.create({
###Challonge docs: http://api.challonge.com/v1 ###Challonge docs: http://api.challonge.com/v1
##TODO ##TODO
2. support camelCase -> under_score params 1. validate required params
3. validate required params 2. docs
4. docs 3. tests
5. tests
--- ---
......
...@@ -6,7 +6,7 @@ var client = challonge.createClient({ ...@@ -6,7 +6,7 @@ var client = challonge.createClient({
version: 1, version: 1,
}); });
var tourneyName = 'nodeapitest3'; var tourneyName = 'nodeapitestCamel';
function index() { function index() {
client.tournaments.index({ client.tournaments.index({
...@@ -22,8 +22,8 @@ function create() { ...@@ -22,8 +22,8 @@ function create() {
tournament: { tournament: {
name: 'name-'+tourneyName, name: 'name-'+tourneyName,
url: tourneyName, url: tourneyName,
signup_cap: 8, signupCap: 8,
tournament_type: 'single elimination', tournamentType: 'single elimination',
}, },
callback: function(err,data){ callback: function(err,data){
if (err) { console.log(err); return; } if (err) { console.log(err); return; }
...@@ -204,7 +204,7 @@ function mupdate() { ...@@ -204,7 +204,7 @@ function mupdate() {
//index(); //index();
client.setSubdomain('nodeapitest'); //client.setSubdomain('nodeapitest');
create(); create();
//show(); //show();
//update(); //update();
......
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