Merge: Added contributing guidelines and link from readme
[nit.git] / src / web / model_api.nit
index ba01837..0d4c205 100644 (file)
@@ -18,29 +18,6 @@ import web_base
 import highlight
 import uml
 
-# Group all api handlers in one router.
-class APIRouter
-       super Router
-
-       # Model to pass to handlers.
-       var model: Model
-
-       # ModelBuilder to pass to handlers.
-       var modelbuilder: ModelBuilder
-
-       # Mainmodule to pass to handlers.
-       var mainmodule: MModule
-
-       init do
-               use("/list", new APIList(model, mainmodule))
-               use("/search", new APISearch(model, mainmodule))
-               use("/random", new APIRandom(model, mainmodule))
-               use("/entity/:id", new APIEntity(model, mainmodule))
-               use("/code/:id", new APIEntityCode(model, mainmodule, modelbuilder))
-               use("/uml/:id", new APIEntityUML(model, mainmodule))
-       end
-end
-
 # List all mentities.
 #
 # MEntities can be filtered on their kind using the `k` parameter.
@@ -143,6 +120,51 @@ class APIEntity
        end
 end
 
+# Linearize super definitions of a MClassDef or a MPropDef if any.
+#
+# Example: `GET /entity/core::Array/linearization`
+class APIEntityLinearization
+       super APIHandler
+
+       redef fun get(req, res) do
+               var mentity = mentity_from_uri(req, res)
+               if mentity == null then
+                       res.error 404
+                       return
+               end
+               var lin = mentity.collect_linearization(mainmodule)
+               if lin == null then
+                       res.error 404
+                       return
+               end
+               res.json new JsonArray.from(lin)
+       end
+end
+
+# List definitions of a MEntity.
+#
+# Example: `GET /defs/core::Array`
+class APIEntityDefs
+       super APIHandler
+
+       redef fun get(req, res) do
+               var mentity = mentity_from_uri(req, res)
+               var arr = new JsonArray
+               if mentity isa MModule then
+                       for mclassdef in mentity.mclassdefs do arr.add mclassdef
+               else if mentity isa MClass then
+                       for mclassdef in mentity.mclassdefs do arr.add mclassdef
+               else if mentity isa MClassDef then
+                       for mpropdef in mentity.mpropdefs do arr.add mpropdef
+               else if mentity isa MProperty then
+                       for mpropdef in mentity.mpropdefs do arr.add mpropdef
+               else
+                       res.error 404
+                       return
+               end
+               res.json arr
+       end
+end
 
 # Return a UML representation of MEntity.
 #