Skip to content

Commit e5d0ff1

Browse files
committed
[ProblemSet] Refactor to prepare for other problem sets.
Signed-off-by: Eric Wang <[email protected]>
1 parent eb965d0 commit e5d0ff1

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

lib/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var DEFAULT_CONFIG = {
66
// usually you don't wanna change those
77
URL_BASE: 'https://leetcode.com',
88
URL_LOGIN: 'https://leetcode.com/accounts/login/',
9-
URL_PROBLEMS: 'https://leetcode.com/api/problems/algorithms/',
9+
URL_PROBLEMS: 'https://leetcode.com/api/problems/$category/',
1010
URL_PROBLEM: 'https://leetcode.com/problems/$slug',
1111
URL_TEST: 'https://leetcode.com/problems/$slug/interpret_solution/',
1212
URL_SUBMIT: 'https://leetcode.com/problems/$slug/submit/',

lib/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ core.getProblems = function(cb) {
3636

3737
log.debug('running getProblems');
3838
var user = this.getUser();
39-
client.getProblems(user, function(e, problems) {
39+
client.getProblems('algorithms', user, function(e, problems) {
4040
if (e) return cb(e);
4141

4242
saveUser(user);

lib/leetcode_client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ function requestWithReLogin(opts, cb) {
101101

102102
var leetcodeClient = {};
103103

104-
leetcodeClient.getProblems = function(user, cb) {
105-
var opts = makeOpts(config.URL_PROBLEMS);
104+
leetcodeClient.getProblems = function(category, user, cb) {
105+
var opts = makeOpts(config.URL_PROBLEMS.replace('$category', category));
106106

107107
requestWithReLogin(opts, function(e, resp, body) {
108108
if (e) return cb(e);

test/test_core.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('core', function() {
133133
it('should getProblems w/o cache ok', function(done) {
134134
cache.del('all');
135135

136-
client.getProblems = function(user, cb) {
136+
client.getProblems = function(category, user, cb) {
137137
return cb(null, PROBLEMS);
138138
};
139139

@@ -147,7 +147,7 @@ describe('core', function() {
147147
it('should getProblems w/o cache fail if client error', function(done) {
148148
cache.del('all');
149149

150-
client.getProblems = function(user, cb) {
150+
client.getProblems = function(category, user, cb) {
151151
return cb('client getProblems error');
152152
};
153153

@@ -232,7 +232,7 @@ describe('core', function() {
232232

233233
it('should getProblem fail if getProblems error', function(done) {
234234
cache.del('all');
235-
client.getProblems = function(user, cb) {
235+
client.getProblems = function(category, user, cb) {
236236
return cb('getProblems error');
237237
};
238238

test/test_leetcode_client.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('leetcode_client', function() {
2020
msg: 'session expired, please login again',
2121
statusCode: -1
2222
};
23+
var URL_ALGORITHMS = 'https://leetcode.com/api/problems/algorithms/';
2324

2425
before(function() {
2526
config.init();
@@ -46,10 +47,10 @@ describe('leetcode_client', function() {
4647

4748
it('should ok', function(done) {
4849
config.AUTO_LOGIN = true;
49-
nock(config.URL_PROBLEMS).get('/').reply(403);
50-
nock(config.URL_PROBLEMS).get('/').replyWithFile(200, './test/mock/problems.json.20160911');
50+
nock(URL_ALGORITHMS).get('/').reply(403);
51+
nock(URL_ALGORITHMS).get('/').replyWithFile(200, './test/mock/problems.json.20160911');
5152

52-
client.getProblems(USER, function(e, problems) {
53+
client.getProblems('algorithms', USER, function(e, problems) {
5354
assert.equal(e, null);
5455
assert.equal(problems.length, 377);
5556
done();
@@ -58,19 +59,19 @@ describe('leetcode_client', function() {
5859

5960
it('should fail if no auto login', function(done) {
6061
config.AUTO_LOGIN = false;
61-
nock(config.URL_PROBLEMS).get('/').reply(403);
62+
nock(URL_ALGORITHMS).get('/').reply(403);
6263

63-
client.getProblems(USER, function(e, problems) {
64+
client.getProblems('algorithms', USER, function(e, problems) {
6465
assert.deepEqual(e, EXPIRED_ERROR);
6566
done();
6667
});
6768
});
6869

6970
it('should fail if other error', function(done) {
7071
config.AUTO_LOGIN = true;
71-
nock(config.URL_PROBLEMS).get('/').reply(503);
72+
nock(URL_ALGORITHMS).get('/').reply(503);
7273

73-
client.getProblems(USER, function(e, problems) {
74+
client.getProblems('algorithms', USER, function(e, problems) {
7475
var expected = {
7576
msg: 'http error',
7677
statusCode: 503
@@ -82,13 +83,13 @@ describe('leetcode_client', function() {
8283

8384
it('should fail if http error in relogin', function(done) {
8485
config.AUTO_LOGIN = true;
85-
nock(config.URL_PROBLEMS).get('/').reply(403);
86-
nock(config.URL_PROBLEMS).get('/').reply(403);
86+
nock(URL_ALGORITHMS).get('/').reply(403);
87+
nock(URL_ALGORITHMS).get('/').reply(403);
8788
core.login = function(user, cb) {
8889
return cb('unknown error!');
8990
};
9091

91-
client.getProblems(USER, function(e, problems) {
92+
client.getProblems('algorithms', USER, function(e, problems) {
9293
assert.deepEqual(e, EXPIRED_ERROR);
9394
done();
9495
});
@@ -97,9 +98,9 @@ describe('leetcode_client', function() {
9798

9899
describe('#getProblems', function() {
99100
it('should ok', function(done) {
100-
nock(config.URL_PROBLEMS).get('/').replyWithFile(200, './test/mock/problems.json.20160911');
101+
nock(URL_ALGORITHMS).get('/').replyWithFile(200, './test/mock/problems.json.20160911');
101102

102-
client.getProblems(USER, function(e, problems) {
103+
client.getProblems('algorithms', USER, function(e, problems) {
103104
assert.equal(e, null);
104105
assert.equal(problems.length, 377);
105106
done();
@@ -108,9 +109,9 @@ describe('leetcode_client', function() {
108109

109110
it('should fail if not login', function(done) {
110111
config.AUTO_LOGIN = false;
111-
nock(config.URL_PROBLEMS).get('/').replyWithFile(200, './test/mock/problems.nologin.json.20161015');
112+
nock(URL_ALGORITHMS).get('/').replyWithFile(200, './test/mock/problems.nologin.json.20161015');
112113

113-
client.getProblems(USER, function(e, problems) {
114+
client.getProblems('algorithms', USER, function(e, problems) {
114115
assert.deepEqual(e, EXPIRED_ERROR);
115116
done();
116117
});

0 commit comments

Comments
 (0)