Merge: Added contributing guidelines and link from readme
[nit.git] / src / web / api_catalog.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 module api_catalog
16
17 import web_base
18 import catalog
19
20 # Group all api handlers in one router.
21 class APICatalogRouter
22 super Router
23
24 # Model to pass to handlers.
25 var model: Model
26
27 # Mainmodule to pass to handlers.
28 var mainmodule: MModule
29
30 # Catalog to pass to handlers.
31 var catalog: Catalog
32
33 init do
34 use("/highlighted", new APICatalogHighLighted(model, mainmodule, catalog))
35 use("/required", new APICatalogMostRequired(model, mainmodule, catalog))
36 use("/bytags", new APICatalogByTags(model, mainmodule, catalog))
37 use("/contributors", new APICatalogContributors(model, mainmodule, catalog))
38 use("/stats", new APICatalogStats(model, mainmodule, catalog))
39 end
40 end
41
42 abstract class APICatalogHandler
43 super APIHandler
44
45 var catalog: Catalog
46
47 # List the 10 best packages from `cpt`
48 fun list_best(cpt: Counter[MPackage]): JsonArray do
49 var res = new JsonArray
50 var best = cpt.sort
51 for i in [1..10] do
52 if i > best.length then break
53 res.add best[best.length-i]
54 end
55 return res
56 end
57
58 # List packages by group.
59 fun list_by(map: MultiHashMap[Object, MPackage]): JsonObject do
60 var res = new JsonObject
61 var keys = map.keys.to_a
62 alpha_comparator.sort(keys)
63 for k in keys do
64 var projs = map[k].to_a
65 alpha_comparator.sort(projs)
66 res[k.to_s.html_escape] = new JsonArray.from(projs)
67 end
68 return res
69 end
70 end
71
72 class APICatalogStats
73 super APICatalogHandler
74
75 redef fun get(req, res) do
76 var obj = new JsonObject
77 obj["packages"] = model.mpackages.length
78 obj["maintainers"] = catalog.maint2proj.length
79 obj["contributors"] = catalog.contrib2proj.length
80 obj["modules"] = catalog.mmodules.sum
81 obj["classes"] = catalog.mclasses.sum
82 obj["methods"] = catalog.mmethods.sum
83 obj["loc"] = catalog.loc.sum
84 res.json obj
85 end
86 end
87
88 class APICatalogHighLighted
89 super APICatalogHandler
90
91 redef fun get(req, res) do res.json list_best(catalog.score)
92 end
93
94 class APICatalogMostRequired
95 super APICatalogHandler
96
97 redef fun get(req, res) do
98 if catalog.deps.not_empty then
99 var reqs = new Counter[MPackage]
100 for p in model.mpackages do
101 reqs[p] = catalog.deps[p].smallers.length - 1
102 end
103 res.json list_best(reqs)
104 return
105 end
106 res.json new JsonArray
107 end
108 end
109
110 class APICatalogByTags
111 super APICatalogHandler
112
113 redef fun get(req, res) do res.json list_by(catalog.tag2proj)
114 end
115
116 class APICatalogContributors
117 super APICatalogHandler
118
119 redef fun get(req, res) do
120 var obj = new JsonObject
121 obj["maintainers"] = new JsonArray.from(catalog.maint2proj.keys)
122 obj["contributors"] = new JsonArray.from(catalog.contrib2proj.keys)
123 res.json obj
124 end
125 end
126
127 redef class Person
128 super Jsonable
129
130 redef fun to_json do
131 var obj = new JsonObject
132 obj["name"] = name
133 obj["email"] = email
134 obj["page"] = page
135 obj["hash"] = (email or else "").md5.to_lower
136 return obj.to_json
137 end
138 end