nitweb: add /api/inheritance/:id service
[nit.git] / src / web / model_api.nit
index 5b24282..a9f0c75 100644 (file)
@@ -120,6 +120,22 @@ class APIEntity
        end
 end
 
+# List ancestors, parents, child and descendants of MEntity
+#
+# Example: `GET /entity/core::Array/inheritance`
+class APIEntityInheritance
+       super APIHandler
+
+       redef fun get(req, res) do
+               var mentity = mentity_from_uri(req, res)
+               if mentity == null then
+                       res.error 404
+                       return
+               end
+               res.json mentity.hierarchy_poset(view)[mentity]
+       end
+end
+
 # Linearize super definitions of a MClassDef or a MPropDef if any.
 #
 # Example: `GET /entity/core::Array/linearization`
@@ -141,6 +157,31 @@ class APIEntityLinearization
        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.
 #
 # Example: `GET /entity/core::Array/uml`