Commit b7429ce5 authored by Aaron Tidwell's avatar Aaron Tidwell

fixing camel-underscore and back

parent 1aac05e9
......@@ -12,7 +12,13 @@ var errorHandler = require('./error-handler');
*/
var Client = exports.Client = function(options) {
this.options = options;
if (!this.options.version) { this.options.version = 1; }
//defaults - todo convert to an object merge
if (!this.options.version) {
this.options.version = 1;
}
if (!this.options.massageProperties) {
this.options.massageProperties = true;
}
this.setSubdomain(this.options.subdomain);
......@@ -30,7 +36,7 @@ function serializeProperties(obj) {
var serializedProperties = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (typeof(obj[prop]) === 'object') {
if (typeof(obj[prop]) === 'object' && obj[prop] !== null) {
for (var attr in obj[prop]) {
compiledParams += '&';
compiledParams += prop + '[' + attr + ']=' + encodeURIComponent(obj[prop][attr]);
......@@ -48,26 +54,36 @@ function serializeProperties(obj) {
// 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();
return str.replace(/\W+/g, '-')
.replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase();
}
function UnderscoreToCamel(str) {
return str.replace(/_([a-z])/g, function(g) {
return g[1].toUpperCase();
});
}
function convertPropertyCamelCaseToUnderscore(obj) {
function convertProperties(obj, converionFunction) {
//determine which we want to check with to see if we should convert
var checkRegex = converionFunction === UnderscoreToCamel ? /_/ : /[A-Z]/;
console.log(checkRegex);
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
//objects recurse
if (typeof obj[prop] === 'object') {
obj[camelToUnderscore(prop)] = convertPropertyCamelCaseToUnderscore(obj[prop]);
if (typeof obj[prop] === 'object' && obj[prop] !== null) {
console.log('recursing', prop)
obj[converionFunction(prop)] = convertProperties(obj[prop], converionFunction);
}
//otherwise, if the underscore version is in the list we convert it
else if (propertiesToConvert.indexOf(camelToUnderscore(prop)) !== -1) {
obj[camelToUnderscore(prop)] = obj[prop];
else if (prop.search(checkRegex) > -1) {
obj[converionFunction(prop)] = obj[prop];
//remove it
console.log('removing ', prop)
delete obj[prop];
}
console.log('failed', prop, prop.search(checkRegex))
//otherwise leave it alone
}
}
......@@ -91,11 +107,12 @@ Client.prototype.makeRequest = function(obj) {
var path = obj.path;
var method = obj.method;
// massage camel to underscore
// normalize the rest of the properties
obj = convertProperties(obj, camelToUnderscore);
// Add on the api key
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
......@@ -141,6 +158,10 @@ Client.prototype.makeRequest = function(obj) {
// 200 ok
if (self.options.get('format') == 'json') {
resData = JSON.parse(resData);
if (self.options.get('massageProperties')) {
console.log('converting', resData);
resData = convertProperties(resData,UnderscoreToCamel);
}
}
callback(null, resData); //no error, so no err object
});
......
//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
......@@ -6,7 +6,7 @@ var client = challonge.createClient({
version: 1,
});
var tourneyName = 'nodeapitestcamel';
var tourneyName = 'nodeapite3stcamel';
function index() {
client.tournaments.index({
......@@ -108,9 +108,8 @@ function pindex() {
function pcreate() {
client.participants.create({
id: tourneyName,
participantId: 'arbitraryid',
participant: {
name: 'Tidwell'
name: 'Tidwell345678901239331'
},
callback: function(err,data){
if (err) { console.log(err); return; }
......@@ -191,8 +190,8 @@ function mupdate() {
id: tourneyName,
matchId: 15606254,
match: {
scores_csv: '3-0',
winner_id: 10847219
scoresCsv: '3-0',
winnerId: 10847219
},
callback: function(err,data){
if (err) { console.log(err); return; }
......@@ -215,6 +214,7 @@ create();
//pindex();
//pcreate();
//pshow();
//pupdate();
//pdestroy();
......
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