OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of search_engine; | |
6 | |
7 | |
8 class GithubSearchEngine implements SearchEngine { | |
9 String get name => 'Github'; | |
10 | |
11 Stream<SearchResult> search(String input) { | |
12 var query = { | |
13 'q': 'language:dart $input' | |
14 }; | |
15 var searchUri = new Uri.https( | |
16 'api.github.com', | |
17 '/search/repositories', | |
18 query); | |
19 var controller = new StreamController(); | |
20 http_client.get(searchUri) | |
21 .then((http_client.Response response) { | |
22 if (response.statusCode != HttpStatus.OK) { | |
23 throw "Bad status code: ${response.statusCode}, " | |
24 "message: ${response.reasonPhrase}"; | |
25 } | |
26 var json = JSON.decode(response.body); | |
27 json.putIfAbsent('items', () => []); | |
28 json['items'] | |
29 .expand((item) { | |
30 if (item['description'] == null || | |
31 item['description'].isEmpty) { | |
32 // Skip results without a description. | |
33 return []; | |
34 } | |
35 return [item]; | |
36 }) | |
37 .take(3) | |
38 .forEach((item) { | |
39 controller.add(new SearchResult( | |
40 item['description'], item['html_url'])); | |
41 }); | |
42 }) | |
43 .catchError(controller.addError) | |
44 .whenComplete(controller.close); | |
45 return controller.stream; | |
46 } | |
47 } | |
OLD | NEW |