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 dartiverse_search; | |
6 | |
7 | |
8 class GithubSearchEngine { | |
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((response) { | |
22 if (response.statusCode != HttpStatus.OK) throw "Bad status code."; | |
23 var json = JSON.decode(response.body); | |
24 json.putIfAbsent('items', () => []); | |
25 json['items'] | |
26 .expand((item) { | |
27 if (item['description'] == null || | |
28 item['description'].isEmpty) { | |
29 // Skip results without a description. | |
30 return []; | |
31 } | |
32 return [item]; | |
33 }) | |
34 .take(3) | |
35 .forEach((item) { | |
36 controller.add(new SearchResult( | |
37 item['description'], item['html_url'])); | |
38 }); | |
39 }) | |
40 .catchError(controller.addError) | |
41 .whenComplete(controller.close); | |
42 return controller.stream; | |
43 } | |
44 } | |
OLD | NEW |