Skip to content

Commit 8ae2108

Browse files
cassioscabralÆndrew Rininsland
authored andcommitted
Implements the search API. Closes #151.
add Search API informations and examples based on Github API documentation to README.md add search api tests
1 parent 45b22a8 commit 8ae2108

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,51 @@ To comment in a issue
334334
issues.comment(issue, comment,function(err, comment) {});
335335
```
336336

337+
## Search API
338+
339+
```js
340+
var search = github.getSearch(query);
341+
```
342+
343+
### Search repositories
344+
345+
Suppose you want to search for popular Tetris repositories written in Assembly. Your query might look like this:
346+
347+
```js
348+
var search = github.getSearch("tetris+language:assembly&sort=stars&order=desc");
349+
search.repositories(options, function (err, repositories) {});
350+
```
351+
352+
### Search code
353+
354+
Suppose you want to find the definition of the addClass function inside jQuery. Your query would look something like this:
355+
356+
```js
357+
var search = github.getSearch("addClass+in:file+language:js+repo:jquery/jquery");
358+
search.code(options, function (err, codes) {});
359+
```
360+
361+
### Search issues
362+
363+
Let’s say you want to find the oldest unresolved Python bugs on Windows. Your query might look something like this:
364+
365+
```js
366+
var search = github.getSearch("windows+label:bug+language:python+state:open&sort=created&order=asc");
367+
search.issues(options, function (err, issues) {});
368+
```
369+
370+
### Search users
371+
372+
Imagine you’re looking for a list of popular users. You might try out this query:
373+
374+
```js
375+
var search = github.getSearch("tom+repos:%3E42+followers:%3E1000");
376+
search.users(options, function (err, users) {});
377+
```
378+
379+
Here, we’re looking at users with the name Tom. We’re only interested in those with more than 42 repositories, and only if they have over 1,000 followers.
380+
381+
337382
## Change Log
338383

339384
### 0.10.X

github.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,30 @@
885885
};
886886
};
887887

888+
// Search API
889+
// ==========
890+
891+
Github.Search = function(options) {
892+
var path = "/search/";
893+
var query = "?q=" + options.query;
894+
895+
this.repositories = function(options, cb) {
896+
_request("GET", path + "repositories" + query, options, cb);
897+
};
898+
899+
this.code = function(options, cb) {
900+
_request("GET", path + "code" + query, options, cb);
901+
};
902+
903+
this.issues = function(options, cb) {
904+
_request("GET", path + "issues" + query, options, cb);
905+
};
906+
907+
this.users = function(options, cb) {
908+
_request("GET", path + "users" + query, options, cb);
909+
};
910+
};
911+
888912
return Github;
889913
};
890914

@@ -912,5 +936,9 @@
912936
return new Github.Gist({id: id});
913937
};
914938

939+
Github.getSearch = function(query) {
940+
return new Github.Search({query: query});
941+
};
942+
915943
return Github;
916944
}));

test/test.search.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
var test = require('tape'); //jshint ignore:line
4+
var Github = require("../");
5+
var test_user = require('./user.json');
6+
7+
test("User API", function(t) {
8+
var timeout = setTimeout(function () { t.fail(); }, 100000);
9+
var github = new Github({
10+
username: test_user.USERNAME,
11+
password: test_user.PASSWORD,
12+
auth: "basic"
13+
});
14+
// var user = github.getUser();
15+
16+
17+
t.test('Search.repositories', function(q) {
18+
var search = github.getSearch("tetris+language:assembly&sort=stars&order=desc");
19+
var options = null;
20+
21+
search.repositories(options, function (err) {
22+
q.error(err, 'search repositories');
23+
q.end();
24+
});
25+
});
26+
27+
t.test('Search.code', function(q) {
28+
var search = github.getSearch("addClass+in:file+language:js+repo:jquery/jquery");
29+
var options = null;
30+
31+
search.code(options, function (err) {
32+
q.error(err, 'search code');
33+
q.end();
34+
});
35+
});
36+
37+
t.test('Search.issues', function(q) {
38+
var search = github.getSearch("windows+label:bug+language:python+state:open&sort=created&order=asc");
39+
var options = null;
40+
41+
search.issues(options, function (err) {
42+
q.error(err, 'search issues');
43+
q.end();
44+
});
45+
});
46+
47+
t.test('Search.users', function(q) {
48+
var search = github.getSearch("tom+repos:%3E42+followers:%3E1000");
49+
var options = null;
50+
51+
search.users(options, function (err) {
52+
q.error(err, 'search users');
53+
q.end();
54+
});
55+
});
56+
57+
58+
59+
clearTimeout(timeout);
60+
t.end();
61+
});

0 commit comments

Comments
 (0)