Merge: Nitsmell : Adding new code smells and print console updated
[nit.git] / src / web / api_catalog.nit
index 72d207f..678f514 100644 (file)
@@ -17,33 +17,48 @@ module api_catalog
 import web_base
 import catalog
 
-# Group all api handlers in one router.
-class APICatalogRouter
-       super Router
-
-       # Model to pass to handlers.
-       var model: Model
-
-       # Mainmodule to pass to handlers.
-       var mainmodule: MModule
+redef class NitwebConfig
 
        # Catalog to pass to handlers.
-       var catalog: Catalog
-
-       init do
-               use("/highlighted", new APICatalogHighLighted(model, mainmodule, catalog))
-               use("/required", new APICatalogMostRequired(model, mainmodule, catalog))
-               use("/bytags", new APICatalogByTags(model, mainmodule, catalog))
-               use("/contributors", new APICatalogContributors(model, mainmodule, catalog))
-               use("/stats", new APICatalogStats(model, mainmodule, catalog))
+       var catalog: Catalog is noinit
+
+       # Build the catalog
+       #
+       # This method should be called at nitweb startup.
+       fun build_catalog do
+               var catalog = new Catalog(modelbuilder)
+               for mpackage in model.mpackages do
+                       catalog.deps.add_node(mpackage)
+                       for mgroup in mpackage.mgroups do
+                               for mmodule in mgroup.mmodules do
+                                       for imported in mmodule.in_importation.direct_greaters do
+                                               var ip = imported.mpackage
+                                               if ip == null or ip == mpackage then continue
+                                               catalog.deps.add_edge(mpackage, ip)
+                                       end
+                               end
+                       end
+                       catalog.git_info(mpackage)
+                       catalog.package_page(mpackage)
+               end
+               self.catalog = catalog
+       end
+end
+
+redef class APIRouter
+       redef init do
+               super
+               use("/catalog/highlighted", new APICatalogHighLighted(config))
+               use("/catalog/required", new APICatalogMostRequired(config))
+               use("/catalog/bytags", new APICatalogByTags(config))
+               use("/catalog/contributors", new APICatalogContributors(config))
+               use("/catalog/stats", new APICatalogStats(config))
        end
 end
 
 abstract class APICatalogHandler
        super APIHandler
 
-       var catalog: Catalog
-
        # List the 10 best packages from `cpt`
        fun list_best(cpt: Counter[MPackage]): JsonArray do
                var res = new JsonArray
@@ -74,13 +89,13 @@ class APICatalogStats
 
        redef fun get(req, res) do
                var obj = new JsonObject
-               obj["packages"] = model.mpackages.length
-               obj["maintainers"] = catalog.maint2proj.length
-               obj["contributors"] = catalog.contrib2proj.length
-               obj["modules"] = catalog.mmodules.sum
-               obj["classes"] = catalog.mclasses.sum
-               obj["methods"] = catalog.mmethods.sum
-               obj["loc"] = catalog.loc.sum
+               obj["packages"] = config.model.mpackages.length
+               obj["maintainers"] = config.catalog.maint2proj.length
+               obj["contributors"] = config.catalog.contrib2proj.length
+               obj["modules"] = config.catalog.mmodules.sum
+               obj["classes"] = config.catalog.mclasses.sum
+               obj["methods"] = config.catalog.mmethods.sum
+               obj["loc"] = config.catalog.loc.sum
                res.json obj
        end
 end
@@ -88,17 +103,17 @@ end
 class APICatalogHighLighted
        super APICatalogHandler
 
-       redef fun get(req, res) do res.json list_best(catalog.score)
+       redef fun get(req, res) do res.json list_best(config.catalog.score)
 end
 
 class APICatalogMostRequired
        super APICatalogHandler
 
        redef fun get(req, res) do
-               if catalog.deps.not_empty then
+               if config.catalog.deps.not_empty then
                        var reqs = new Counter[MPackage]
-                       for p in model.mpackages do
-                               reqs[p] = catalog.deps[p].smallers.length - 1
+                       for p in config.model.mpackages do
+                               reqs[p] = config.catalog.deps[p].smallers.length - 1
                        end
                        res.json list_best(reqs)
                        return
@@ -110,7 +125,7 @@ end
 class APICatalogByTags
        super APICatalogHandler
 
-       redef fun get(req, res) do res.json list_by(catalog.tag2proj)
+       redef fun get(req, res) do res.json list_by(config.catalog.tag2proj)
 end
 
 class APICatalogContributors
@@ -118,21 +133,19 @@ class APICatalogContributors
 
        redef fun get(req, res) do
                var obj = new JsonObject
-               obj["maintainers"] = new JsonArray.from(catalog.maint2proj.keys)
-               obj["contributors"] = new JsonArray.from(catalog.contrib2proj.keys)
+               obj["maintainers"] = new JsonArray.from(config.catalog.maint2proj.keys)
+               obj["contributors"] = new JsonArray.from(config.catalog.contrib2proj.keys)
                res.json obj
        end
 end
 
 redef class Person
-       super Jsonable
+       super Serializable
 
-       redef fun to_json do
-               var obj = new JsonObject
-               obj["name"] = name
-               obj["email"] = email
-               obj["page"] = page
-               obj["hash"] = (email or else "").md5.to_lower
-               return obj.to_json
+       redef fun core_serialize_to(v) do
+               v.serialize_attribute("name", name)
+               v.serialize_attribute("email", email)
+               v.serialize_attribute("page", page)
+               v.serialize_attribute("hash", (email or else "").md5.to_lower)
        end
 end