Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Add command to star a problem #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/commands/star.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var log = require('loglevel');
var core = require('../core');
var h = require('../helper');

var cmd = {
command: 'star <keyword>',
desc: 'star problem by name or index'
};

cmd.handler = function(argv) {
core.getProblem(argv.keyword, function(e, problem) {
if (e) return log.fail(e);
core.starProblem(problem, function(e) {
if (e) return log.fail(e);
log.info(h.prettyText(' Starred', true));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks dude, overall LGTM. Just wondering what if run 'lc star 1' twice, will it be un-starred in the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skygragon nope, the api is idempotent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unstar is done by sending a DELETE request to the same endpoint

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think that is good enough for the moment. I guess there will be more patches to address related functions like 'unstar'. Also want to update local cache whenever is_favor is updated in problem json.

});
});
};

module.exports = cmd;
1 change: 1 addition & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var DEFAULT_CONFIG = {
URL_SUBMISSIONS: 'https://leetcode.com/problems/$key/submissions/',
URL_SUBMISSION: 'https://leetcode.com/submissions/detail/$id/',
URL_VERIFY: 'https://leetcode.com/submissions/detail/$id/check/',
URL_STAR: 'https://leetcode.com/problems/favor/',

// but you will want change these
LANG: 'cpp', // avail: [c,cpp,csharp,golang,java,javascript,python,ruby,swift]
Expand Down
4 changes: 4 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ core.updateProblem = function(problem, kv) {
return cache.set('all', problems);
};

core.starProblem = function(problem, cb) {
client.starProblem(problem, cb);
};

core.login = function(user, cb) {
var self = this;
client.login(user, function(e, user) {
Expand Down
15 changes: 15 additions & 0 deletions lib/leetcode_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,19 @@ leetcodeClient.submitProblem = function(problem, cb) {
});
};

leetcodeClient.starProblem = function(problem, cb) {
var opts = makeOpts(config.URL_STAR);
opts.method = 'POST';
opts.headers.Origin = config.URL_BASE;
opts.headers.Referer = problem.link;
opts.headers['X-Requested-With'] = 'XMLHttpRequest';
opts.json = true;
opts.body = {'qid': problem.id};

requestWithReLogin(opts, function(e, resp, body) {
e = checkError(e, resp, 200);
cb(e);
});
};

module.exports = leetcodeClient;
11 changes: 11 additions & 0 deletions test/test_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ describe('core', function() {
});
});

it('should starProblem ok', function(done) {
client.starProblem = function(problem, cb) {
return cb(null);
};

core.starProblem(PROBLEMS[0], function(e) {
assert.equal(e, null);
done();
});
});

// dummy test
it('should testProblem ok', function(done) {
client.testProblem = function(problem, cb) {
Expand Down
22 changes: 22 additions & 0 deletions test/test_leetcode_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,28 @@ describe('leetcode_client', function() {
});
}); // #submitProblem

describe('#starProblem', function() {
it('should ok', function(done) {
var problem = {
id: 389,
name: 'Find the Difference',
key: 'find-the-difference',
link: 'https://leetcode.com/problems/find-the-difference',
locked: false,
file: '/dev/null'
};

nock('https://leetcode.com')
.post('/problems/favor/')
.reply(200, '{"is_favor": true}');

client.starProblem(problem, function(e) {
assert.equal(e, null);
done();
});
});
}); // #starProblem

describe('#getSubmissions', function() {
it('should ok', function(done) {
var problem = {
Expand Down