Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Challonge
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Challonge
Commits
a8c818eb
Commit
a8c818eb
authored
Mar 19, 2017
by
Aaron Tidwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update to const/let and resolve formatting issues
parent
91559004
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
145 additions
and
137 deletions
+145
-137
README.md
README.md
+15
-16
lib/api/client.js
lib/api/client.js
+52
-55
lib/api/error-handler.js
lib/api/error-handler.js
+12
-10
lib/api/matches.js
lib/api/matches.js
+13
-8
lib/api/participants.js
lib/api/participants.js
+16
-11
lib/api/tournaments.js
lib/api/tournaments.js
+18
-17
lib/challonge.js
lib/challonge.js
+17
-18
lib/util.js
lib/util.js
+0
-0
package.json
package.json
+2
-2
No files found.
README.md
View file @
a8c818eb
...
...
@@ -15,7 +15,6 @@ var client = challonge.createClient({
apiKey
:
'
***yourAPIKey***
'
});
client
.
tournaments
.
index
({
callback
:
function
(
err
,
data
){
if
(
err
)
{
console
.
log
(
err
);
return
;
}
...
...
lib/api/client.js
View file @
a8c818eb
'
use strict
'
;
var
qs
=
require
(
'
querystring
'
);
var
https
=
require
(
'
https
'
);
var
errorHandler
=
require
(
'
./error-handler
'
);
/*
### function Client (options)
#### @options {Object} Options for this instance
Constructor function for the Client base responsible
for communicating with Challonge API
*/
var
Client
=
exports
.
Client
=
function
(
options
)
{
const
qs
=
require
(
'
querystring
'
);
const
https
=
require
(
'
https
'
);
const
errorHandler
=
require
(
'
./error-handler
'
);
/**
* @class Client(options)
* @param {object} options configuration options for this instance
* @description
* Constructor function for the Client base responsible for communicating with Challonge API
*/
const
Client
=
exports
.
Client
=
function
(
options
)
{
this
.
options
=
options
||
{};
//defaults - todo convert to an object merge
//
defaults - todo convert to an object merge
if
(
!
this
.
options
.
version
)
{
this
.
options
.
version
=
1
;
}
...
...
@@ -27,7 +25,7 @@ var Client = exports.Client = function(options) {
// add a getter to the options passed in - DO NOT mess with instance configs in resources
if
(
typeof
this
.
options
.
get
!==
'
function
'
)
{
this
.
options
.
get
=
function
(
key
)
{
this
.
options
.
get
=
function
(
key
)
{
// not an arrow function to maintain "this" reference
return
this
[
key
];
};
}
...
...
@@ -35,19 +33,16 @@ var Client = exports.Client = function(options) {
// serialize nested params to tournament[name] style
function
serializeProperties
(
obj
)
{
var
compiledParams
=
''
;
var
serializedProperties
=
[];
for
(
var
prop
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
prop
))
{
if
(
typeof
(
obj
[
prop
])
===
'
object
'
&&
obj
[
prop
]
!==
null
)
{
for
(
var
attr
in
obj
[
prop
])
{
compiledParams
+=
'
&
'
;
compiledParams
+=
prop
+
'
[
'
+
attr
+
'
]=
'
+
encodeURIComponent
(
obj
[
prop
][
attr
]);
let
compiledParams
=
''
;
let
serializedProperties
=
[];
for
(
let
prop
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
prop
)
&&
typeof
obj
[
prop
]
===
'
object
'
&&
obj
[
prop
]
!==
null
)
{
for
(
let
attr
in
obj
[
prop
])
{
compiledParams
+=
'
&
'
+
prop
+
'
[
'
+
attr
+
'
]=
'
+
encodeURIComponent
(
obj
[
prop
][
attr
]);
}
serializedProperties
.
push
(
prop
);
}
}
}
return
{
serialized
:
compiledParams
,
properties
:
serializedProperties
...
...
@@ -55,31 +50,29 @@ function serializeProperties(obj) {
}
// resources generate props internal to https requests
var
propertiesToDelete
=
[
'
callback
'
,
'
path
'
,
'
method
'
];
let
propertiesToDelete
=
[
'
callback
'
,
'
path
'
,
'
method
'
];
function
camelToUnderscore
(
str
)
{
return
str
.
replace
(
/
\W
+/g
,
'
-
'
)
.
replace
(
/
([
a-z
\d])([
A-Z
])
/g
,
'
$1_$2
'
).
toLowerCase
();
.
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
();
});
return
str
.
replace
(
/_
([
a-z
])
/g
,
g
=>
g
[
1
].
toUpperCase
());
}
function
convertProperties
(
obj
,
converionFunction
)
{
//determine which we want to check with to see if we should convert
var
checkRegex
=
conver
ionFunction
===
UnderscoreToCamel
?
/_/
:
/
[
A-Z
]
/
;
for
(
var
prop
in
obj
)
{
function
convertProperties
(
obj
,
conver
s
ionFunction
)
{
//
determine which we want to check with to see if we should convert
const
checkRegex
=
convers
ionFunction
===
UnderscoreToCamel
?
/_/
:
/
[
A-Z
]
/
;
for
(
let
prop
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
prop
))
{
//objects recurse
//
objects recurse
if
(
typeof
obj
[
prop
]
===
'
object
'
&&
obj
[
prop
]
!==
null
)
{
obj
[
conver
ionFunction
(
prop
)]
=
convertProperties
(
obj
[
prop
],
conver
ionFunction
);
obj
[
conver
sionFunction
(
prop
)]
=
convertProperties
(
obj
[
prop
],
convers
ionFunction
);
}
else
if
(
prop
.
search
(
checkRegex
)
>
-
1
)
{
obj
[
converionFunction
(
prop
)]
=
obj
[
prop
];
//remove it
delete
obj
[
prop
];
obj
[
conversionFunction
(
prop
)]
=
obj
[
prop
];
delete
obj
[
prop
];
// remove it
}
//otherwise leave it alone
...
...
@@ -89,7 +82,7 @@ function convertProperties(obj, converionFunction) {
}
Client
.
prototype
.
setSubdomain
=
function
(
subdomain
)
{
//generate the subdomain URL string if there is one
//
generate the subdomain URL string if there is one
if
(
!
subdomain
)
{
this
.
options
.
subdomain
=
''
;
}
else
if
(
subdomain
[
subdomain
.
length
-
1
]
!==
'
-
'
)
{
...
...
@@ -101,11 +94,11 @@ Client.prototype.setSubdomain = function(subdomain) {
// cleans the passed in object, generates the API url/query-string, makes the request, delegates errors and calls callbacks
Client
.
prototype
.
makeRequest
=
function
(
obj
)
{
var
self
=
this
;
const
self
=
this
;
// cache vars that are about to be removed
var
callback
=
obj
.
callback
;
var
path
=
obj
.
path
;
var
method
=
obj
.
method
;
const
callback
=
obj
.
callback
;
const
method
=
obj
.
method
;
let
path
=
obj
.
path
;
// normalize the rest of the properties
obj
=
convertProperties
(
obj
,
camelToUnderscore
);
...
...
@@ -113,25 +106,29 @@ Client.prototype.makeRequest = function(obj) {
// Add on the api key
obj
.
api_key
=
this
.
options
.
get
(
'
apiKey
'
);
//convert for url
obj
.
cache_bust
=
Math
.
random
();
//serialize the properties
var
serialized
=
serializeProperties
(
obj
);
var
compiledParams
=
serialized
.
serialized
;
//merge the stuff to remove
// serialize the properties
const
serialized
=
serializeProperties
(
obj
);
// get the non-standard-formatted properties (this is to support the tournament[score] kind of params the api expects)
const
compiledParams
=
serialized
.
serialized
;
// merge the stuff to remove
propertiesToDelete
=
propertiesToDelete
.
concat
(
serialized
.
properties
);
// remove params
propertiesToDelete
.
forEach
(
function
(
prop
)
{
propertiesToDelete
.
forEach
(
(
prop
)
=>
{
delete
obj
[
prop
];
});
// generate path
var
versionPaths
=
{
const
versionPaths
=
{
1
:
'
/v1/tournaments
'
};
path
=
versionPaths
[
this
.
options
.
get
(
'
version
'
)]
+
(
path
?
path
:
''
)
+
'
.
'
+
this
.
options
.
get
(
'
format
'
)
+
'
?
'
+
qs
.
stringify
(
obj
)
+
compiledParams
;
// create options for the https call
var
options
=
{
const
options
=
{
hostname
:
'
api.challonge.com
'
,
path
:
path
,
method
:
method
,
...
...
@@ -140,14 +137,14 @@ Client.prototype.makeRequest = function(obj) {
}
};
var
req
=
https
.
request
(
options
,
function
(
res
)
{
const
req
=
https
.
request
(
options
,
(
res
)
=>
{
// store the chunked data as it comes back
var
resData
=
''
;
res
.
on
(
'
data
'
,
function
(
chunk
)
{
let
resData
=
''
;
res
.
on
(
'
data
'
,
(
chunk
)
=>
{
resData
+=
chunk
;
});
res
.
on
(
'
end
'
,
function
()
{
res
.
on
(
'
end
'
,
()
=>
{
// error
if
(
res
.
statusCode
!==
200
)
{
errorHandler
.
handle
(
res
,
resData
,
callback
,
self
.
options
.
get
(
'
format
'
));
...
...
lib/api/error-handler.js
View file @
a8c818eb
// Response Codes
// The following HTTP response codes are issued by the API. All other codes are the result of a request not reaching the application.
// 200 - OK
// 401 - Invalid API key
// 404 - Object not found within your account scope
// 422 - Validation error(s) for create or update method
/**
* Response code handler
* @param {object} res the http response object
* @param {object} resData the data contained in the response
* @param {Function} callback the users callback function to call
* @param {string} format the format of the response data (json, xml, etc)
* @description
* The following HTTP response codes are issued by the API.
* All other codes are the result of a request not reaching the application.
*/
exports
.
handle
=
function
(
res
,
resData
,
callback
,
format
)
{
var
err
;
let
err
;
// 401 - Invalid API key
if
(
res
.
statusCode
===
401
)
{
err
=
{
...
...
@@ -34,7 +36,7 @@ exports.handle = function(res, resData, callback, format) {
// 422 - Validation error(s) for create or update method
if
(
res
.
statusCode
===
422
)
{
if
(
format
==
'
json
'
)
{
if
(
format
==
=
'
json
'
)
{
resData
=
JSON
.
parse
(
resData
);
}
err
=
{
...
...
lib/api/matches.js
View file @
a8c818eb
// index GET tournaments/:tournament/matches
// show GET tournaments/:tournament/matches/:match_id
// update PUT tournaments/:tournament/matches/:match_id
const
util
=
require
(
'
util
'
);
const
Client
=
require
(
'
./client
'
).
Client
;
var
util
=
require
(
'
util
'
);
var
Client
=
require
(
'
./client
'
).
Client
;
var
Matches
=
exports
.
Matches
=
function
(
options
)
{
/**
* @class Matches(options)
* @param {object} options configuration options for this instance
* @description
* Constructor function for the class to query Matches endpoints
* index GET tournaments/:tournament/matches
* show GET tournaments/:tournament/matches/:match_id
* update PUT tournaments/:tournament/matches/:match_id
*/
const
Matches
=
exports
.
Matches
=
function
(
options
)
{
Client
.
call
(
this
,
options
);
// call parent constructor
};
//
I
nherit from Client base object
//
i
nherit from Client base object
util
.
inherits
(
Matches
,
Client
);
Matches
.
prototype
.
index
=
function
(
obj
)
{
...
...
lib/api/participants.js
View file @
a8c818eb
// index GET tournaments/:tournament/participants
// create POST tournaments/:tournament/participants
// show GET tournaments/:tournament/participants/:participant_id
// update PUT tournaments/:tournament/participants/:participant_id
// destroy DELETE tournaments/:tournament/participants/:participant_id
// randomize GET tournaments/:tournament/participants/randomize
const
util
=
require
(
'
util
'
);
const
Client
=
require
(
'
./client
'
).
Client
;
var
util
=
require
(
'
util
'
);
var
Client
=
require
(
'
./client
'
).
Client
;
var
Participants
=
exports
.
Participants
=
function
(
options
)
{
/**
* @class Participants(options)
* @param {object} options configuration options for this instance
* @description
* Constructor function for the class to query Participants endpoints
* index GET tournaments/:tournament/participants
* create POST tournaments/:tournament/participants
* show GET tournaments/:tournament/participants/:participant_id
* update PUT tournaments/:tournament/participants/:participant_id
* destroy DELETE tournaments/:tournament/participants/:participant_id
* randomize GET tournaments/:tournament/participants/randomize
*/
const
Participants
=
exports
.
Participants
=
function
(
options
)
{
Client
.
call
(
this
,
options
);
// call parent constructor
};
//
I
nherit from Client base object
//
i
nherit from Client base object
util
.
inherits
(
Participants
,
Client
);
Participants
.
prototype
.
index
=
function
(
obj
)
{
...
...
lib/api/tournaments.js
View file @
a8c818eb
// // index GET tournaments
// // create POST tournaments
// // show GET tournaments/:tournament
// // update PUT tournaments/:tournament
// // destroy DELETE tournaments/:tournament
// // start POST tournaments/:tournament/start
// // finalize POST tournaments/:tournament/finalize
// // reset POST tournaments/:tournament/reset
const
util
=
require
(
'
util
'
);
const
Client
=
require
(
'
./client
'
).
Client
;
var
util
=
require
(
'
util
'
);
var
Client
=
require
(
'
./client
'
).
Client
;
var
Tournaments
=
exports
.
Tournaments
=
function
(
options
)
{
/**
* @class Participants(options)
* @param {object} options configuration options for this instance
* @description
* Constructor function for the class to query Participants endpoints
* index GET tournaments
* create POST tournaments
* show GET tournaments/:tournament
* update PUT tournaments/:tournament
* destroy DELETE tournaments/:tournament
* start POST tournaments/:tournament/start
* finalize POST tournaments/:tournament/finalize
* reset POST tournaments/:tournament/reset
*/
const
Tournaments
=
exports
.
Tournaments
=
function
(
options
)
{
Client
.
call
(
this
,
options
);
// call parent constructor
this
.
getRawSubdomain
=
function
()
{
...
...
@@ -22,13 +27,11 @@ var Tournaments = exports.Tournaments = function(options) {
};
};
//
I
nherit from Client base object
//
i
nherit from Client base object
util
.
inherits
(
Tournaments
,
Client
);
Tournaments
.
prototype
.
index
=
function
(
obj
)
{
obj
.
method
=
'
GET
'
;
// generate the subdomain property from the subdomain
// url we generate in the client constructor
if
(
this
.
getRawSubdomain
())
{
obj
.
subdomain
=
this
.
getRawSubdomain
();
}
...
...
@@ -36,8 +39,6 @@ Tournaments.prototype.index = 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
();
}
...
...
lib/challonge.js
View file @
a8c818eb
'
use strict
'
;
const
endpoints
=
[
'
Client
'
,
'
Tournaments
'
,
'
Participants
'
,
'
Matches
'
]
;
var
parts
=
[
'
Client
'
,
'
Tournaments
'
,
'
Participants
'
,
'
Matches
'
];
parts
.
forEach
(
function
forEach
(
k
)
{
exports
[
k
]
=
require
(
'
./api/
'
+
k
.
toLowerCase
())[
k
];
endpoints
.
forEach
(
endpointName
=>
{
exports
[
endpointName
]
=
require
(
'
./api/
'
+
endpointName
.
toLowerCase
())[
endpointName
];
});
/*
### function createClient(options)
#### @options {Object} options for the clients
Generates a new API client.
*/
/**
* @function createClient(options)
* @param {object} options configuration options for this instance
* @returns {object} new api client instance
* @description
* Generates a new API client.
*/
exports
.
createClient
=
function
createClient
(
options
)
{
var
client
=
{};
const
client
=
{};
// require each lib in ./api and instantiate a new instance of each object, passing the options we were passed
parts
.
forEach
(
function
generate
(
k
)
{
var
endpoint
=
k
.
toLowerCase
();
client
[
endpoint
]
=
new
exports
[
k
](
options
);
// store for the user to reference via instance.resource
endpoints
.
forEach
(
endpointName
=>
{
// store for the user to reference via instance.resource
client
[
endpoint
Name
.
toLowerCase
()]
=
new
exports
[
endpointName
](
options
);
});
client
.
setSubdomain
=
function
(
subdomain
)
{
parts
.
forEach
(
function
update
(
k
)
{
var
endpoint
=
k
.
toLowerCase
();
client
[
endpoint
].
setSubdomain
(
subdomain
);
client
.
setSubdomain
=
subdomain
=>
{
endpoints
.
forEach
(
endpointName
=>
{
client
[
endpointName
.
toLowerCase
()].
setSubdomain
(
subdomain
);
});
};
...
...
lib/util.js
deleted
100644 → 0
View file @
91559004
package.json
View file @
a8c818eb
...
...
@@ -3,7 +3,7 @@
"description"
:
"Wrapper for the challong api"
,
"author"
:
"Aaron Tiwell <aaron.tidwell@gmail.com>"
,
"main"
:
"./lib/challonge.js"
,
"version"
:
"
1.2
.0"
,
"version"
:
"
2.0
.0"
,
"contributors"
:
[
{
"name"
:
"Ricardo Reis"
,
...
...
@@ -49,5 +49,5 @@
"integrate"
:
"npm run-script test && npm run-script format && npm run-script lint"
},
"engine"
:
"node >=
0.1
0.x"
"engine"
:
"node >=
6.
0.x"
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment