Skip to content

Commit

Permalink
final build for npm
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-dinar committed Oct 2, 2016
1 parent 8f411a8 commit 34308fb
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 34 deletions.
8 changes: 6 additions & 2 deletions example/es5/api.js
Expand Up @@ -8,10 +8,14 @@ var apiSecret = process.env.CFS;
Codeforces.setApis(apiKey, apiSecret);

var params = {
handles: 'Fefer_Ivan;DmitriyH'
contestId: 566,
from: 1,
count: 2,
showUnofficial:true,
handles: 'rng_58;W4yneb0t'
};

Codeforces.user.info(params,function (err,result) {
Codeforces.contest.standings(params,function (err,result) {

if (err) {
return console.log(err);
Expand Down
19 changes: 9 additions & 10 deletions src/codeforces.js
Expand Up @@ -23,7 +23,7 @@ class CF {
API_URL: "http://codeforces.com/api",
API_KEY: "",
API_SECRET: "",
DEFAULT_TIMEOUT: 55000
DEFAULT_TIMEOUT: 60000 //1 minute
};

//user method
Expand All @@ -39,10 +39,10 @@ class CF {
//contest method
this.contest = {
hacks: callApi.bind(this,"contest.hacks"),
list: callApi.bind(this,"contest.hacks"),
ratingChanges: callApi.bind(this,"contest.hacks"),
standings: callApi.bind(this,"contest.hacks"),
status: callApi.bind(this,"contest.hacks")
list: callApi.bind(this,"contest.list"),
ratingChanges: callApi.bind(this,"contest.ratingChanges"),
standings: callApi.bind(this,"contest.standings"),
status: callApi.bind(this,"contest.status")
};

//all problemset method
Expand Down Expand Up @@ -96,7 +96,7 @@ function callApi(method, params, cb) {

opts.method = method;

//final url of API reuqest
//final url of API request
let url = makeApiUrl(opts,params);


Expand Down Expand Up @@ -140,15 +140,14 @@ function makeApiUrl(options,params) {
query.time = curTime;
query.apiKey = options.API_KEY;

//if any parameter given as array, make then string separated by semicolon(;)
//if any parameter given as array, make it string separated by semicolon(;)
for(let key in params){
if( _.isArray(params[key]) ){
params[key] = _.join(params[key],';');
}
}



//sort the parameters according to codeforces API rules
query = _
.chain(query)
Expand All @@ -161,11 +160,11 @@ function makeApiUrl(options,params) {
.mapValues('value')
.value();

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

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

return url;
}
Expand Down
161 changes: 139 additions & 22 deletions test/test-codeforces.js
Expand Up @@ -6,25 +6,45 @@ let apiSecret = process.env.CFS;

Codeforces.setApis(apiKey, apiSecret);

let TEST_TIMEOUT = process.env.CFT_TIMEOUT || 50000;
let TEST_TIMEOUT = process.env.CFT_TIMEOUT || 60000; //1 minute

//neccessary informations for testing
let confg = {
blogEntry: {
validId: 79,
invalidId: 1500
},
contest:{
validId: 566,
invalidId: 25000,
}
};

describe('Codeforces', function() {

describe('#blogEntry', function() {
describe('.comments', function() {

this.timeout(TEST_TIMEOUT);

it('should return error when blogEntryId empty', (done) => {
it('should return error when blogEntryId is empty', (done) => {
Codeforces.blogEntry.comments({},function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});

it('should successfully return json', (done) => {
Codeforces.blogEntry.comments({ blogEntryId: 79 },function (err,result) {

it('should failed when blogEntryId is invalid', (done) => {
Codeforces.blogEntry.comments({ blogEntryId: confg.blogEntry.invalidId },function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});

it('should successfully return json when blogEntryId is valid', (done) => {
Codeforces.blogEntry.comments({ blogEntryId: confg.blogEntry.validId },function (err,result) {
expect(err).to.be.null;
done();
});
Expand All @@ -35,16 +55,24 @@ describe('Codeforces', function() {

this.timeout(TEST_TIMEOUT);

it('should return error when blogEntryId empty', (done) => {
it('should return error when blogEntryId is empty', (done) => {
Codeforces.blogEntry.view({},function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});

it('should failed when blogEntryId is invalid', (done) => {
Codeforces.blogEntry.view({ blogEntryId: confg.blogEntry.invalidId },function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});

it('should successfully return json', (done) => {
Codeforces.blogEntry.view({ blogEntryId: 79 },function (err,result) {
Codeforces.blogEntry.view({ blogEntryId: confg.blogEntry.validId },function (err,result) {
expect(err).to.be.null;
done();
});
Expand All @@ -60,41 +88,69 @@ describe('Codeforces', function() {

this.timeout(TEST_TIMEOUT);

it('should return error when contestId empty', (done) => {
it('should return error when contestId is empty', (done) => {
Codeforces.contest.hacks({},function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});

it('should successfully return json', (done) => {
Codeforces.contest.hacks({ contestId : 700 },function (err,result) {
it('should failed when contestID is invalid', (done) => {
Codeforces.contest.hacks({ contestId : confg.contest.invalidId },function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});

it('should successfully return json when contestId is valid', (done) => {
Codeforces.contest.hacks({ contestId : 1 },function (err,result) {
expect(err).to.be.null;
done();
});
});

});

describe('.list', function() {

this.timeout(TEST_TIMEOUT);

it('should successfully return json', (done) => {
Codeforces.contest.list({ gym : false },function (err,result) {
expect(err).to.be.null;
done();
});
});
});

describe('.ratingChanges', function() {

this.timeout(TEST_TIMEOUT);

it('should return error when contestId empty', (done) => {
it('should return error when contestId is empty', (done) => {
Codeforces.contest.ratingChanges({},function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});

it('should failed when contestId is invalid', (done) => {
Codeforces.contest.ratingChanges({ contestId: confg.contest.invalidId },function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});

it('should successfully return json', (done) => {
Codeforces.contest.ratingChanges({ contestId: 1 },function (err,result) {
expect(err).to.be.null;
done();
});
});

});

describe('.standings', function() {
Expand All @@ -109,13 +165,34 @@ describe('Codeforces', function() {
});
});

it('should successfully return json', (done) => {
it('should successfully return json without handles', (done) => {
Codeforces.contest.standings({ contestId: 1, from:1, count:1, showUnofficial:true },function (err,result) {
expect(err).to.be.null;
done();
});
});

it('should successfully return json without handles and with room', (done) => {
Codeforces.contest.standings({ contestId: 1, from:1, count:1, showUnofficial:true, room: 3 },function (err,result) {
expect(err).to.be.null;
done();
});
});

it('should successfully return json with single handles', (done) => {
Codeforces.contest.standings({ contestId: 1, handles: 'Gullesnuffs', from:1, count:2, showUnofficial:true },function (err,result) {
expect(err).to.be.null;
done();
});
});

it('should successfully return json with array of handles', (done) => {
Codeforces.contest.standings({ contestId: 1, handles: ['Gullesnuffs','uwi'], from:1, count:2, showUnofficial:true },function (err,result) {
expect(err).to.be.null;
done();
});
});

});

describe('.status', function() {
Expand All @@ -130,8 +207,15 @@ describe('Codeforces', function() {
});
});

it('should successfully return json', (done) => {
Codeforces.contest.status({ contestId: 1, from:1, count:1, handle: 'tapioca' },function (err,result) {
it('should successfully return json without handle', (done) => {
Codeforces.contest.status({ contestId: 566, from:1, count:1 },function (err,result) {
expect(err).to.be.null;
done();
});
});

it('should successfully return json with handle', (done) => {
Codeforces.contest.status({ contestId: 566, from:1, count:1, handle: 'tapioca' },function (err,result) {
expect(err).to.be.null;
done();
});
Expand All @@ -147,13 +231,27 @@ describe('Codeforces', function() {

this.timeout(TEST_TIMEOUT);

it('should successfully return json', (done) => {
it('should successfully return json without tag', (done) => {
Codeforces.problemset.problems({},function (err,result) {
expect(err).to.be.null;
done();
});
});

it('should successfully return json with single tag', (done) => {
Codeforces.problemset.problems({ tags: 'probabilities' },function (err,result) {
expect(err).to.be.null;
done();
});
});

it('should successfully return json with array of tags', (done) => {
Codeforces.problemset.problems({ tags: ['probabilities','two pointers'] },function (err,result) {
expect(err).to.be.null;
done();
});
});

});

describe('.recentStatus', function() {
Expand Down Expand Up @@ -227,13 +325,20 @@ describe('Codeforces', function() {

this.timeout(TEST_TIMEOUT);

it('should successfully return json', (done) => {
it('should successfully return json with onlyOnline', (done) => {
Codeforces.user.friends({ onlyOnline : true },function (err,result) {
expect(err).to.be.null;
done();
});
});

it('should successfully return json without onlyOnline', (done) => {
Codeforces.user.friends({ },function (err,result) {
expect(err).to.be.null;
done();
});
});

});

describe('.info', function() {
Expand All @@ -256,16 +361,31 @@ describe('Codeforces', function() {
});
});

/*

it('should successfully return json when array of handles passed', (done) => {
Codeforces.user.info({ handles: ['Fefer_Ivan','DmitriyH'] },function (err,result) {
expect(err).to.be.null;
done();
});
});*/
});

});

/* //data is huge
describe('.ratedList', function() {
this.timeout(TEST_TIMEOUT);
it('should successfully return json', (done) => {
Codeforces.user.ratedList({ activeOnly: true },function (err,result) {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
done();
});
});
});*/

describe('.rating', function() {

this.timeout(TEST_TIMEOUT);
Expand Down Expand Up @@ -307,8 +427,5 @@ describe('Codeforces', function() {
});

});

});


});

0 comments on commit 34308fb

Please sign in to comment.