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) {
};
}
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',
];
// resources generate props internal to https requests
var propertiesToDelete = ['callback', 'path', 'method'];
// all the stuff we have to convert camelCase to under_scores
var propertiesToConvert = require('./properties-to-convert');
function camelToUnderscore(str) {
return str.replace(/\W+/g, '-')
.replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase();
}
function convertPropertyCamelCaseToUnderscore(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
//objects recurse
if (typeof obj[prop] === 'object') {
obj[camelToUnderscore(prop)] = convertPropertyCamelCaseToUnderscore(obj[prop]);
}
//otherwise, if the underscore version is in the list we convert it
else if (propertiesToConvert.indexOf(camelToUnderscore(prop)) !== -1) {
obj[camelToUnderscore(prop)] = obj[prop];
//remove it
delete obj[prop];
}
//otherwise leave it alone
}
}
return obj;
}
Client.prototype.setSubdomain = function(subdomain) {
//generate the subdomain URL string if there is one
......@@ -102,6 +94,10 @@ Client.prototype.makeRequest = function(obj) {
// massage camel to underscore
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
var serialized = serializeProperties(obj);
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({
###Challonge docs: http://api.challonge.com/v1
##TODO
2. support camelCase -> under_score params
3. validate required params
4. docs
5. tests
1. validate required params
2. docs
3. tests
---
......
......@@ -6,7 +6,7 @@ var client = challonge.createClient({
version: 1,
});
var tourneyName = 'nodeapitest3';
var tourneyName = 'nodeapitestCamel';
function index() {
client.tournaments.index({
......@@ -22,8 +22,8 @@ function create() {
tournament: {
name: 'name-'+tourneyName,
url: tourneyName,
signup_cap: 8,
tournament_type: 'single elimination',
signupCap: 8,
tournamentType: 'single elimination',
},
callback: function(err,data){
if (err) { console.log(err); return; }
......@@ -204,7 +204,7 @@ function mupdate() {
//index();
client.setSubdomain('nodeapitest');
//client.setSubdomain('nodeapitest');
create();
//show();
//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