Merge: Update basic requirements and document them
[nit.git] / contrib / github_search_for_jni / src / github_search_for_jni.nit
index b2df540..f9927c1 100644 (file)
 # Script to scan Github for repositories possibly using JNI.
 module github_search_for_jni
 
-import github_api
+import github::api
+import json::static
 
-# The proprieties introduced by this redef are to be used only on HashMap
+# The proprieties introduced by this redef are to be used only on a JSON object
 # representing a Github repository.
-#
-# REQUIRE `assert self isa HashMap[String, nullable Object]`
-# REQUIRE `for v in self.values do assert v isa Int
-redef class HashMap[K, V]
+redef class JsonObject
        # The repository has at least 50% Java code
        fun has_lots_of_java: Bool
        do
@@ -35,7 +33,6 @@ redef class HashMap[K, V]
 
                var total = 0
                for k, v in self do
-                       assert k isa String
                        assert v isa Int
                        total += v
                end
@@ -55,8 +52,8 @@ end
 # Query sent to Github
 var main_query = "language:java"
 
-# Curl instance use for all requests
-var curl = new GithubCurl("OAUTH TOKEN (replace with your own)", "JNI project finder (nitlanguage.org)")
+# API client instance use for all requests
+var api = new GithubAPI("OAUTH TOKEN (replace with your own)", "JNI project finder (nitlanguage.org)")
 
 if "NIT_TESTING".environ == "true" then exit 0
 
@@ -65,15 +62,15 @@ var page = 0
 var per_page = 100
 loop
        # Get a page of the main query
-       var uri = "https://api.github.com/search/repositories?q={main_query}&page={page}&per_page={per_page}&sort=stars"
-       var obj = curl.get_and_check(uri).as(HashMap[String, nullable Object])
+       var uri = "/search/repositories?q={main_query}&page={page}&per_page={per_page}&sort=stars"
+       var obj = api.send("GET", uri).parse_json.as(JsonObject)
 
        # Main object has "total_count" and "items"
-       var items = obj["items"].as(Array[nullable Object])
+       var items = obj["items"].as(JsonArray)
 
        # "items" is an array of Json objects
        for item in items do
-               assert item isa HashMap[String, nullable Object]
+               assert item isa JsonObject
 
                # Each item has "name" and "languages_url"
                assert item.keys.has("name")
@@ -81,11 +78,11 @@ loop
 
                # Download the language list
                var lang_url = item["languages_url"].as(String)
-               var langs = curl.get_and_check(lang_url).as(HashMap[String, nullable Object])
+               var langs = api.send("GET", lang_url).parse_json.as(JsonObject)
 
                # The project is of interest if it has lots of Java and at least some C
                var may_be_of_interest = langs.has_lots_of_java and langs.has_some_c
-               if may_be_of_interest then print "{item["name"]}: {item["forks"]}; {langs.keys.join(", ")}; {item["html_url"]}"
+               if may_be_of_interest then print "{item["name"].to_s}: {item["forks"].to_s}; {langs.keys.join(", ")}; {item["html_url"].to_s}"
        end
 
        # If we got less pages than asked for, we are done!