Skip to content

Commit 34308fb

Browse files
committed
final build for npm
1 parent 8f411a8 commit 34308fb

File tree

3 files changed

+154
-34
lines changed

3 files changed

+154
-34
lines changed

example/es5/api.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ var apiSecret = process.env.CFS;
88
Codeforces.setApis(apiKey, apiSecret);
99

1010
var params = {
11-
handles: 'Fefer_Ivan;DmitriyH'
11+
contestId: 566,
12+
from: 1,
13+
count: 2,
14+
showUnofficial:true,
15+
handles: 'rng_58;W4yneb0t'
1216
};
1317

14-
Codeforces.user.info(params,function (err,result) {
18+
Codeforces.contest.standings(params,function (err,result) {
1519

1620
if (err) {
1721
return console.log(err);

src/codeforces.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CF {
2323
API_URL: "http://codeforces.com/api",
2424
API_KEY: "",
2525
API_SECRET: "",
26-
DEFAULT_TIMEOUT: 55000
26+
DEFAULT_TIMEOUT: 60000 //1 minute
2727
};
2828

2929
//user method
@@ -39,10 +39,10 @@ class CF {
3939
//contest method
4040
this.contest = {
4141
hacks: callApi.bind(this,"contest.hacks"),
42-
list: callApi.bind(this,"contest.hacks"),
43-
ratingChanges: callApi.bind(this,"contest.hacks"),
44-
standings: callApi.bind(this,"contest.hacks"),
45-
status: callApi.bind(this,"contest.hacks")
42+
list: callApi.bind(this,"contest.list"),
43+
ratingChanges: callApi.bind(this,"contest.ratingChanges"),
44+
standings: callApi.bind(this,"contest.standings"),
45+
status: callApi.bind(this,"contest.status")
4646
};
4747

4848
//all problemset method
@@ -96,7 +96,7 @@ function callApi(method, params, cb) {
9696

9797
opts.method = method;
9898

99-
//final url of API reuqest
99+
//final url of API request
100100
let url = makeApiUrl(opts,params);
101101

102102

@@ -140,15 +140,14 @@ function makeApiUrl(options,params) {
140140
query.time = curTime;
141141
query.apiKey = options.API_KEY;
142142

143-
//if any parameter given as array, make then string separated by semicolon(;)
143+
//if any parameter given as array, make it string separated by semicolon(;)
144144
for(let key in params){
145145
if( _.isArray(params[key]) ){
146146
params[key] = _.join(params[key],';');
147147
}
148148
}
149149

150150

151-
152151
//sort the parameters according to codeforces API rules
153152
query = _
154153
.chain(query)
@@ -161,11 +160,11 @@ function makeApiUrl(options,params) {
161160
.mapValues('value')
162161
.value();
163162

164-
let apiSig = randomToken + '/' + options.method + '?' + qs.stringify(query) + '#' + options.API_SECRET;
163+
let apiSig = randomToken + '/' + options.method + '?' + qs.stringify(query,{ encode: false }) + '#' + options.API_SECRET;
165164
apiSig = sha512(apiSig).toString();
166165
query.apiSig = randomToken + apiSig;
167166

168-
let url = options.API_URL + '/' + options.method + '?' + qs.stringify(query);
167+
let url = options.API_URL + '/' + options.method + '?' + qs.stringify(query,{ encode: false });
169168

170169
return url;
171170
}

test/test-codeforces.js

Lines changed: 139 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,45 @@ let apiSecret = process.env.CFS;
66

77
Codeforces.setApis(apiKey, apiSecret);
88

9-
let TEST_TIMEOUT = process.env.CFT_TIMEOUT || 50000;
9+
let TEST_TIMEOUT = process.env.CFT_TIMEOUT || 60000; //1 minute
10+
11+
//neccessary informations for testing
12+
let confg = {
13+
blogEntry: {
14+
validId: 79,
15+
invalidId: 1500
16+
},
17+
contest:{
18+
validId: 566,
19+
invalidId: 25000,
20+
}
21+
};
1022

1123
describe('Codeforces', function() {
12-
1324
describe('#blogEntry', function() {
1425
describe('.comments', function() {
1526

1627
this.timeout(TEST_TIMEOUT);
1728

18-
it('should return error when blogEntryId empty', (done) => {
29+
it('should return error when blogEntryId is empty', (done) => {
1930
Codeforces.blogEntry.comments({},function (err,result) {
2031
expect(err).to.not.be.null;
2132
expect(err).to.not.be.undefined;
2233
done();
2334
});
2435
});
2536

26-
it('should successfully return json', (done) => {
27-
Codeforces.blogEntry.comments({ blogEntryId: 79 },function (err,result) {
37+
38+
it('should failed when blogEntryId is invalid', (done) => {
39+
Codeforces.blogEntry.comments({ blogEntryId: confg.blogEntry.invalidId },function (err,result) {
40+
expect(err).to.not.be.null;
41+
expect(err).to.not.be.undefined;
42+
done();
43+
});
44+
});
45+
46+
it('should successfully return json when blogEntryId is valid', (done) => {
47+
Codeforces.blogEntry.comments({ blogEntryId: confg.blogEntry.validId },function (err,result) {
2848
expect(err).to.be.null;
2949
done();
3050
});
@@ -35,16 +55,24 @@ describe('Codeforces', function() {
3555

3656
this.timeout(TEST_TIMEOUT);
3757

38-
it('should return error when blogEntryId empty', (done) => {
58+
it('should return error when blogEntryId is empty', (done) => {
3959
Codeforces.blogEntry.view({},function (err,result) {
4060
expect(err).to.not.be.null;
4161
expect(err).to.not.be.undefined;
4262
done();
4363
});
4464
});
4565

66+
it('should failed when blogEntryId is invalid', (done) => {
67+
Codeforces.blogEntry.view({ blogEntryId: confg.blogEntry.invalidId },function (err,result) {
68+
expect(err).to.not.be.null;
69+
expect(err).to.not.be.undefined;
70+
done();
71+
});
72+
});
73+
4674
it('should successfully return json', (done) => {
47-
Codeforces.blogEntry.view({ blogEntryId: 79 },function (err,result) {
75+
Codeforces.blogEntry.view({ blogEntryId: confg.blogEntry.validId },function (err,result) {
4876
expect(err).to.be.null;
4977
done();
5078
});
@@ -60,41 +88,69 @@ describe('Codeforces', function() {
6088

6189
this.timeout(TEST_TIMEOUT);
6290

63-
it('should return error when contestId empty', (done) => {
91+
it('should return error when contestId is empty', (done) => {
6492
Codeforces.contest.hacks({},function (err,result) {
6593
expect(err).to.not.be.null;
6694
expect(err).to.not.be.undefined;
6795
done();
6896
});
6997
});
7098

71-
it('should successfully return json', (done) => {
72-
Codeforces.contest.hacks({ contestId : 700 },function (err,result) {
99+
it('should failed when contestID is invalid', (done) => {
100+
Codeforces.contest.hacks({ contestId : confg.contest.invalidId },function (err,result) {
101+
expect(err).to.not.be.null;
102+
expect(err).to.not.be.undefined;
103+
done();
104+
});
105+
});
106+
107+
it('should successfully return json when contestId is valid', (done) => {
108+
Codeforces.contest.hacks({ contestId : 1 },function (err,result) {
73109
expect(err).to.be.null;
74110
done();
75111
});
76112
});
77113

78114
});
115+
116+
describe('.list', function() {
117+
118+
this.timeout(TEST_TIMEOUT);
119+
120+
it('should successfully return json', (done) => {
121+
Codeforces.contest.list({ gym : false },function (err,result) {
122+
expect(err).to.be.null;
123+
done();
124+
});
125+
});
126+
});
127+
79128
describe('.ratingChanges', function() {
80129

81130
this.timeout(TEST_TIMEOUT);
82131

83-
it('should return error when contestId empty', (done) => {
132+
it('should return error when contestId is empty', (done) => {
84133
Codeforces.contest.ratingChanges({},function (err,result) {
85134
expect(err).to.not.be.null;
86135
expect(err).to.not.be.undefined;
87136
done();
88137
});
89138
});
90139

140+
it('should failed when contestId is invalid', (done) => {
141+
Codeforces.contest.ratingChanges({ contestId: confg.contest.invalidId },function (err,result) {
142+
expect(err).to.not.be.null;
143+
expect(err).to.not.be.undefined;
144+
done();
145+
});
146+
});
147+
91148
it('should successfully return json', (done) => {
92149
Codeforces.contest.ratingChanges({ contestId: 1 },function (err,result) {
93150
expect(err).to.be.null;
94151
done();
95152
});
96153
});
97-
98154
});
99155

100156
describe('.standings', function() {
@@ -109,13 +165,34 @@ describe('Codeforces', function() {
109165
});
110166
});
111167

112-
it('should successfully return json', (done) => {
168+
it('should successfully return json without handles', (done) => {
113169
Codeforces.contest.standings({ contestId: 1, from:1, count:1, showUnofficial:true },function (err,result) {
114170
expect(err).to.be.null;
115171
done();
116172
});
117173
});
118174

175+
it('should successfully return json without handles and with room', (done) => {
176+
Codeforces.contest.standings({ contestId: 1, from:1, count:1, showUnofficial:true, room: 3 },function (err,result) {
177+
expect(err).to.be.null;
178+
done();
179+
});
180+
});
181+
182+
it('should successfully return json with single handles', (done) => {
183+
Codeforces.contest.standings({ contestId: 1, handles: 'Gullesnuffs', from:1, count:2, showUnofficial:true },function (err,result) {
184+
expect(err).to.be.null;
185+
done();
186+
});
187+
});
188+
189+
it('should successfully return json with array of handles', (done) => {
190+
Codeforces.contest.standings({ contestId: 1, handles: ['Gullesnuffs','uwi'], from:1, count:2, showUnofficial:true },function (err,result) {
191+
expect(err).to.be.null;
192+
done();
193+
});
194+
});
195+
119196
});
120197

121198
describe('.status', function() {
@@ -130,8 +207,15 @@ describe('Codeforces', function() {
130207
});
131208
});
132209

133-
it('should successfully return json', (done) => {
134-
Codeforces.contest.status({ contestId: 1, from:1, count:1, handle: 'tapioca' },function (err,result) {
210+
it('should successfully return json without handle', (done) => {
211+
Codeforces.contest.status({ contestId: 566, from:1, count:1 },function (err,result) {
212+
expect(err).to.be.null;
213+
done();
214+
});
215+
});
216+
217+
it('should successfully return json with handle', (done) => {
218+
Codeforces.contest.status({ contestId: 566, from:1, count:1, handle: 'tapioca' },function (err,result) {
135219
expect(err).to.be.null;
136220
done();
137221
});
@@ -147,13 +231,27 @@ describe('Codeforces', function() {
147231

148232
this.timeout(TEST_TIMEOUT);
149233

150-
it('should successfully return json', (done) => {
234+
it('should successfully return json without tag', (done) => {
235+
Codeforces.problemset.problems({},function (err,result) {
236+
expect(err).to.be.null;
237+
done();
238+
});
239+
});
240+
241+
it('should successfully return json with single tag', (done) => {
151242
Codeforces.problemset.problems({ tags: 'probabilities' },function (err,result) {
152243
expect(err).to.be.null;
153244
done();
154245
});
155246
});
156247

248+
it('should successfully return json with array of tags', (done) => {
249+
Codeforces.problemset.problems({ tags: ['probabilities','two pointers'] },function (err,result) {
250+
expect(err).to.be.null;
251+
done();
252+
});
253+
});
254+
157255
});
158256

159257
describe('.recentStatus', function() {
@@ -227,13 +325,20 @@ describe('Codeforces', function() {
227325

228326
this.timeout(TEST_TIMEOUT);
229327

230-
it('should successfully return json', (done) => {
328+
it('should successfully return json with onlyOnline', (done) => {
231329
Codeforces.user.friends({ onlyOnline : true },function (err,result) {
232330
expect(err).to.be.null;
233331
done();
234332
});
235333
});
236334

335+
it('should successfully return json without onlyOnline', (done) => {
336+
Codeforces.user.friends({ },function (err,result) {
337+
expect(err).to.be.null;
338+
done();
339+
});
340+
});
341+
237342
});
238343

239344
describe('.info', function() {
@@ -256,16 +361,31 @@ describe('Codeforces', function() {
256361
});
257362
});
258363

259-
/*
364+
260365
it('should successfully return json when array of handles passed', (done) => {
261366
Codeforces.user.info({ handles: ['Fefer_Ivan','DmitriyH'] },function (err,result) {
262367
expect(err).to.be.null;
263368
done();
264369
});
265-
});*/
370+
});
266371

267372
});
268373

374+
/* //data is huge
375+
describe('.ratedList', function() {
376+
377+
this.timeout(TEST_TIMEOUT);
378+
379+
it('should successfully return json', (done) => {
380+
Codeforces.user.ratedList({ activeOnly: true },function (err,result) {
381+
expect(err).to.not.be.null;
382+
expect(err).to.not.be.undefined;
383+
done();
384+
});
385+
});
386+
387+
});*/
388+
269389
describe('.rating', function() {
270390

271391
this.timeout(TEST_TIMEOUT);
@@ -307,8 +427,5 @@ describe('Codeforces', function() {
307427
});
308428

309429
});
310-
311430
});
312-
313-
314431
});

0 commit comments

Comments
 (0)