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
3324923e
Commit
3324923e
authored
Nov 25, 2013
by
Aaron Tidwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixing lingering problems
parent
b5b6ccdf
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
56 additions
and
45 deletions
+56
-45
lib/api/client.js
lib/api/client.js
+34
-18
lib/api/error-handler.js
lib/api/error-handler.js
+1
-1
lib/api/matches.js
lib/api/matches.js
+3
-3
lib/api/participants.js
lib/api/participants.js
+6
-6
lib/api/tournaments.js
lib/api/tournaments.js
+6
-8
readme.md
readme.md
+0
-4
test/test.js
test/test.js
+6
-5
No files found.
lib/api/client.js
View file @
3324923e
...
...
@@ -12,6 +12,7 @@ var errorHandler = require('./error-handler');
*/
var
Client
=
exports
.
Client
=
function
(
options
)
{
this
.
options
=
options
;
if
(
!
this
.
options
.
version
)
{
this
.
options
.
version
=
1
;
}
// add a getter to the options passed in - DO NOT mess with instance configs in resources
if
(
typeof
this
.
options
.
get
!==
'
function
'
)
{
...
...
@@ -21,6 +22,28 @@ var Client = exports.Client = function(options) {
}
};
// serialize nested params to tournament[name] style
function
serializeProperties
(
obj
)
{
console
.
log
(
obj
)
var
compiledParams
=
''
;
var
serializedProperties
=
[];
for
(
var
prop
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
prop
))
{
if
(
typeof
(
obj
[
prop
])
===
'
object
'
)
{
for
(
var
attr
in
obj
[
prop
])
{
compiledParams
+=
'
&
'
;
compiledParams
+=
prop
+
'
[
'
+
attr
+
'
]=
'
+
encodeURIComponent
(
obj
[
prop
][
attr
]);
}
serializedProperties
.
push
(
prop
);
}
}
}
return
{
serialized
:
compiledParams
,
properties
:
serializedProperties
};
}
var
propertiesToDelete
=
[
'
callback
'
,
'
path
'
,
'
method
'
];
// resources generate props internal to https requests
// cleans the passed in object, generates the API url/query-string, makes the request, delegates errors and calls callbacks
...
...
@@ -34,19 +57,11 @@ Client.prototype.makeRequest = function(obj) {
// massage camel to underscore
obj
.
api_key
=
this
.
options
.
get
(
'
apiKey
'
);
//convert for url
// serialize nested params to tournament[name] style
var
compiledParams
=
''
;
for
(
var
prop
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
prop
))
{
if
(
typeof
(
obj
[
prop
])
===
'
object
'
)
{
for
(
var
attr
in
obj
[
prop
])
{
compiledParams
+=
'
&
'
;
compiledParams
+=
prop
+
'
[
'
+
attr
+
'
]=
'
+
encodeURIComponent
(
obj
[
prop
][
attr
]);
}
propertiesToDelete
.
push
(
prop
);
}
}
}
//serialize the properties
var
serialized
=
serializeProperties
(
obj
);
var
compiledParams
=
serialized
.
serialized
;
//merge the stuff to remove
propertiesToDelete
=
propertiesToDelete
.
concat
(
serialized
.
properties
);
// remove params
propertiesToDelete
.
forEach
(
function
(
prop
)
{
...
...
@@ -54,9 +69,12 @@ Client.prototype.makeRequest = function(obj) {
});
// generate path
path
=
path
+
'
.
'
+
this
.
options
.
get
(
'
format
'
)
+
'
?
'
+
qs
.
stringify
(
obj
)
+
compiledParams
;
var
versionPaths
=
{
1
:
'
/v1/tournaments
'
};
path
=
versionPaths
[
this
.
options
.
get
(
'
version
'
)]
+
(
path
?
path
:
''
)
+
'
.
'
+
this
.
options
.
get
(
'
format
'
)
+
'
?
'
+
qs
.
stringify
(
obj
)
+
compiledParams
;
//
opt
s for the https call
//
create option
s for the https call
var
options
=
{
hostname
:
'
api.challonge.com
'
,
path
:
path
,
...
...
@@ -66,8 +84,6 @@ Client.prototype.makeRequest = function(obj) {
}
};
console
.
log
(
'
making request to
'
,
options
);
var
req
=
https
.
request
(
options
,
function
(
res
)
{
// store the chunked data as it comes back
var
resData
=
''
;
...
...
@@ -78,7 +94,7 @@ Client.prototype.makeRequest = function(obj) {
res
.
on
(
'
end
'
,
function
()
{
// error
if
(
res
.
statusCode
!==
200
)
{
errorHandler
.
handle
(
res
,
resData
,
callback
);
errorHandler
.
handle
(
res
,
resData
,
callback
,
self
.
options
.
get
(
'
format
'
)
);
return
;
}
// 200 ok
...
...
lib/api/error-handler.js
View file @
3324923e
...
...
@@ -6,7 +6,7 @@
// 404 - Object not found within your account scope
// 422 - Validation error(s) for create or update method
exports
.
handle
=
function
(
res
,
resData
,
callback
)
{
exports
.
handle
=
function
(
res
,
resData
,
callback
,
format
)
{
var
err
;
// 401 - Invalid API key
if
(
res
.
statusCode
===
401
)
{
...
...
lib/api/matches.js
View file @
3324923e
...
...
@@ -13,14 +13,14 @@ var Matches = exports.Matches = function(options) {
util
.
inherits
(
Matches
,
Client
);
Matches
.
prototype
.
index
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/matches
'
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/matches
'
;
delete
obj
.
id
;
obj
.
method
=
'
GET
'
;
this
.
makeRequest
(
obj
);
};
Matches
.
prototype
.
show
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/matches/
'
+
obj
.
matchId
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/matches/
'
+
obj
.
matchId
;
delete
obj
.
id
;
delete
obj
.
matchId
;
obj
.
method
=
'
GET
'
;
...
...
@@ -28,7 +28,7 @@ Matches.prototype.show = function(obj) {
};
Matches
.
prototype
.
update
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/matches/
'
+
obj
.
matchId
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/matches/
'
+
obj
.
matchId
;
delete
obj
.
id
;
delete
obj
.
matchId
;
obj
.
method
=
'
PUT
'
;
...
...
lib/api/participants.js
View file @
3324923e
...
...
@@ -21,21 +21,21 @@ var Participants = exports.Participants = function(options) {
util
.
inherits
(
Participants
,
Client
);
Participants
.
prototype
.
index
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/participants
'
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/participants
'
;
delete
obj
.
id
;
obj
.
method
=
'
GET
'
;
this
.
makeRequest
(
obj
);
};
Participants
.
prototype
.
create
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/participants
'
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/participants
'
;
delete
obj
.
id
;
obj
.
method
=
'
POST
'
;
this
.
makeRequest
(
obj
);
};
Participants
.
prototype
.
show
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/participants/
'
+
obj
.
participantId
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/participants/
'
+
obj
.
participantId
;
delete
obj
.
id
;
delete
obj
.
participantId
;
obj
.
method
=
'
GET
'
;
...
...
@@ -43,7 +43,7 @@ Participants.prototype.show = function(obj) {
};
Participants
.
prototype
.
update
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/participants/
'
+
obj
.
participantId
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/participants/
'
+
obj
.
participantId
;
delete
obj
.
id
;
delete
obj
.
participantId
;
obj
.
method
=
'
PUT
'
;
...
...
@@ -51,7 +51,7 @@ Participants.prototype.update = function(obj) {
};
Participants
.
prototype
.
destroy
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/participants/
'
+
obj
.
participantId
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/participants/
'
+
obj
.
participantId
;
delete
obj
.
id
;
delete
obj
.
participantId
;
obj
.
method
=
'
DELETE
'
;
...
...
@@ -59,7 +59,7 @@ Participants.prototype.destroy = function(obj) {
};
Participants
.
prototype
.
randomize
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/participants/randomize
'
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/participants/randomize
'
;
delete
obj
.
id
;
obj
.
method
=
'
POST
'
;
this
.
makeRequest
(
obj
);
...
...
lib/api/tournaments.js
View file @
3324923e
...
...
@@ -18,54 +18,52 @@ var Tournaments = exports.Tournaments = function(options) {
util
.
inherits
(
Tournaments
,
Client
);
Tournaments
.
prototype
.
index
=
function
(
obj
)
{
obj
.
path
=
'
/v1/tournaments
'
;
obj
.
method
=
'
GET
'
;
this
.
makeRequest
(
obj
);
};
Tournaments
.
prototype
.
create
=
function
(
obj
)
{
obj
.
path
=
'
/v1/tournaments
'
;
obj
.
method
=
'
POST
'
;
this
.
makeRequest
(
obj
);
};
Tournaments
.
prototype
.
show
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
;
obj
.
path
=
'
/
'
+
obj
.
id
;
delete
obj
.
id
;
obj
.
method
=
'
GET
'
;
this
.
makeRequest
(
obj
);
};
Tournaments
.
prototype
.
update
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
;
obj
.
path
=
'
/
'
+
obj
.
id
;
delete
obj
.
id
;
obj
.
method
=
'
PUT
'
;
this
.
makeRequest
(
obj
);
};
Tournaments
.
prototype
.
destroy
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
;
obj
.
path
=
'
/
'
+
obj
.
id
;
delete
obj
.
id
;
obj
.
method
=
'
DELETE
'
;
this
.
makeRequest
(
obj
);
};
Tournaments
.
prototype
.
start
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/start
'
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/start
'
;
delete
obj
.
id
;
obj
.
method
=
'
POST
'
;
this
.
makeRequest
(
obj
);
};
Tournaments
.
prototype
.
finalize
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/finalize
'
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/finalize
'
;
delete
obj
.
id
;
obj
.
method
=
'
POST
'
;
this
.
makeRequest
(
obj
);
};
Tournaments
.
prototype
.
reset
=
function
(
obj
)
{
obj
.
path
=
'
/
v1/tournaments/
'
+
obj
.
id
+
'
/reset
'
;
obj
.
path
=
'
/
'
+
obj
.
id
+
'
/reset
'
;
delete
obj
.
id
;
obj
.
method
=
'
POST
'
;
this
.
makeRequest
(
obj
);
...
...
readme.md
View file @
3324923e
...
...
@@ -51,10 +51,6 @@ client.tournaments.create({
###Challonge docs: http://api.challonge.com/v1
##TODO
*
move /v1/tournaments to config
*
abstract path suffix for partcipants, matches
*
abstract destroy of qs params
2.
support camelCase -> under_score params
3.
validate required params
4.
docs
...
...
test/test.js
View file @
3324923e
...
...
@@ -2,10 +2,11 @@ var challonge = require('./../');
var
client
=
challonge
.
createClient
({
apiKey
:
require
(
'
./../key.js
'
),
format
:
'
json
'
format
:
'
json
'
,
version
:
1
});
var
tourneyName
=
'
710101
'
;
var
tourneyName
=
'
adfasdfasdf3333
'
;
function
index
()
{
client
.
tournaments
.
index
({
...
...
@@ -43,7 +44,7 @@ function show() {
function
update
()
{
client
.
tournaments
.
update
({
id
:
'
test-tourney
'
,
id
:
tourneyName
,
tournament
:
{
name
:
'
renamed test tournet
'
},
...
...
@@ -207,7 +208,7 @@ function mupdate() {
//show();
//update();
//destroy();
//
start();
start
();
//finalize();
//reset();
...
...
@@ -216,7 +217,7 @@ function mupdate() {
//pshow();
//pupdate();
//pdestroy();
prandomize
();
//
prandomize();
//mindex();
//mshow();
...
...
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