Skip to content

Instantly share code, notes, and snippets.

@abdallaadelessa
Last active March 30, 2017 13:20
Show Gist options
  • Save abdallaadelessa/fb70af2e8a5652585e578e43c3ebd41e to your computer and use it in GitHub Desktop.
Save abdallaadelessa/fb70af2e8a5652585e578e43c3ebd41e to your computer and use it in GitHub Desktop.
public void printTopContributor(GitHubApi gitHubApi, final String repoName) {
// 1. Fetch the repository.
gitHubApi.getRepository(repoName)
// 2. Fetch a list of all contributors.
.flatMap(new Function<Repository, SingleSource<? extends List<Contributor>>>() {
@Override
public SingleSource<? extends List<Contributor>> apply(Repository repository) throws Exception {
return repository.getContributors();
}
})
// 3. Convert the list to an Observable emitting contributors on after another.
.flatMapObservable(new Function<List<Contributor>, ObservableSource<? extends Contributor>>() {
@Override
public ObservableSource<? extends Contributor> apply(List<Contributor> contributors) throws Exception {
return Observable.fromIterable(contributors);
}
})
// 4. Sort emitted values.
.sorted(new Comparator<Contributor>() {
@Override
public int compare(Contributor o1, Contributor o2) {
return o2.getNumberOfCommits() - o1.getNumberOfCommits();
}
})
// 5. Take the first contributor and convert the result to a Single.
.firstOrError()
// 6. Print the top contributor's name.
.subscribe(new Consumer<Contributor>() {
@Override
public void accept(Contributor contributor) throws Exception {
print(contributor.getLoginName());
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
logError(throwable, "Error while trying to find the top contributor.");
}
});
}
fun printTopContributor(gitHubApi: GitHubApi, repoName: String) {
gitHubApi.getRepository(repoName)
.flatMap { repository -> repository.contributors }
.flatMapObservable { contributors -> Observable.fromIterable(contributors) }
.sorted { o1, o2 -> o2.numberOfCommits - o1.numberOfCommits }
.firstOrError()
.subscribe(
{ contributor -> print(contributor.loginName) },
{ throwable -> logError(throwable, "Error while trying to find the top contributor.") }
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment