nitweb: migrate /random to /api/random
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 20 May 2016 02:47:32 +0000 (22:47 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 30 May 2016 15:04:02 +0000 (11:04 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/nitweb.nit
src/web/model_api.nit
src/web/web_actions.nit
src/web/web_views.nit

index 96443d4..48aaaa4 100644 (file)
@@ -50,7 +50,6 @@ private class NitwebPhase
                var app = new App
 
                app.use("/api", new APIRouter(model, modelbuilder, mainmodule))
-               app.use("/random", new RandomAction(model))
                app.use("/doc/:namespace", new DocAction(model, mainmodule, modelbuilder))
                app.use("/code/:namespace", new CodeAction(model, modelbuilder))
                app.use("/uml/:namespace", new UMLDiagramAction(model, mainmodule))
index 37213d8..c4fd56d 100644 (file)
@@ -69,6 +69,7 @@ class APIRouter
        init do
                use("/list", new APIList(model, mainmodule))
                use("/search", new APISearch(model, mainmodule))
+               use("/random", new APIRandom(model, mainmodule))
        end
 end
 
@@ -144,3 +145,26 @@ class APIList
                res.json arr
        end
 end
+
+# Return a random list of MEntities.
+#
+# Example: `GET /random?n=10&k=module`
+class APIRandom
+       super APIList
+
+       # Randomize mentities order.
+       fun randomize_mentities(req: HttpRequest, mentities: Array[MEntity]): Array[MEntity] do
+               var res = mentities.to_a
+               res.shuffle
+               return res
+       end
+
+       redef fun get(req, res) do
+               var mentities = list_mentities(req)
+               mentities = limit_mentities(req, mentities)
+               mentities = randomize_mentities(req, mentities)
+               var arr = new JsonArray
+               for mentity in mentities do arr.add mentity
+               res.json arr
+       end
+end
index f1dee04..ffa5649 100644 (file)
@@ -106,34 +106,3 @@ class UMLDiagramAction
                res.send_view(view)
        end
 end
-
-# Return a random list of MEntities.
-class RandomAction
-       super ModelHandler
-
-       redef fun get(req, res) do
-               var n = req.int_arg("n") or else 10
-               var k = req.string_arg("k") or else "modules"
-               var model = init_model_view(req)
-               var mentities: Array[MEntity]
-               if k == "modules" then
-                       mentities = model.mmodules.to_a
-               else if k == "classdefs" then
-                       mentities = model.mclassdefs.to_a
-               else
-                       mentities = model.mpropdefs.to_a
-               end
-               mentities.shuffle
-               mentities = mentities.sub(0, n)
-               if req.is_json_asked then
-                       var json = new JsonArray
-                       for mentity in mentities do
-                               json.add mentity.to_json
-                       end
-                       res.json(json)
-                       return
-               end
-               var view = new HtmlResultPage("random", mentities)
-               res.send_view(view)
-       end
-end
index 58016e2..67e6925 100644 (file)
@@ -35,34 +35,6 @@ class HtmlHomePage
        end
 end
 
-# Display a search results list.
-class HtmlResultPage
-       super NitView
-
-       # Initial query.
-       var query: String
-
-       # Result set
-       var results: Array[MEntity]
-
-       redef fun render do
-               var tpl = new Template
-               tpl.add new Header(1, "Results for {query}")
-               if results.is_empty then
-                       tpl.add "<p>No result for {query}.<p>"
-                       return tpl
-               end
-               var list = new UnorderedList
-               for mentity in results do
-                       var link = mentity.html_link
-                       link.text = mentity.html_full_name
-                       list.add_li new ListItem(link)
-               end
-               tpl.add list
-               return tpl
-       end
-end
-
 # Display the source for each mentities
 class HtmlSourcePage
        super NitView