1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 # Group all api handlers in one router.
21 class APICatalogRouter
24 # Catalog to pass to handlers.
28 use
("/highlighted", new APICatalogHighLighted(config
, catalog
))
29 use
("/required", new APICatalogMostRequired(config
, catalog
))
30 use
("/bytags", new APICatalogByTags(config
, catalog
))
31 use
("/contributors", new APICatalogContributors(config
, catalog
))
32 use
("/stats", new APICatalogStats(config
, catalog
))
36 abstract class APICatalogHandler
41 # List the 10 best packages from `cpt`
42 fun list_best
(cpt
: Counter[MPackage]): JsonArray do
43 var res
= new JsonArray
46 if i
> best
.length
then break
47 res
.add best
[best
.length-i
]
52 # List packages by group.
53 fun list_by
(map
: MultiHashMap[Object, MPackage]): JsonObject do
54 var res
= new JsonObject
55 var keys
= map
.keys
.to_a
56 alpha_comparator
.sort
(keys
)
58 var projs
= map
[k
].to_a
59 alpha_comparator
.sort
(projs
)
60 res
[k
.to_s
.html_escape
] = new JsonArray.from
(projs
)
67 super APICatalogHandler
69 redef fun get
(req
, res
) do
70 var obj
= new JsonObject
71 obj
["packages"] = config
.model
.mpackages
.length
72 obj
["maintainers"] = catalog
.maint2proj
.length
73 obj
["contributors"] = catalog
.contrib2proj
.length
74 obj
["modules"] = catalog
.mmodules
.sum
75 obj
["classes"] = catalog
.mclasses
.sum
76 obj
["methods"] = catalog
.mmethods
.sum
77 obj
["loc"] = catalog
.loc
.sum
82 class APICatalogHighLighted
83 super APICatalogHandler
85 redef fun get
(req
, res
) do res
.json list_best
(catalog
.score
)
88 class APICatalogMostRequired
89 super APICatalogHandler
91 redef fun get
(req
, res
) do
92 if catalog
.deps
.not_empty
then
93 var reqs
= new Counter[MPackage]
94 for p
in config
.model
.mpackages
do
95 reqs
[p
] = catalog
.deps
[p
].smallers
.length
- 1
97 res
.json list_best
(reqs
)
100 res
.json
new JsonArray
104 class APICatalogByTags
105 super APICatalogHandler
107 redef fun get
(req
, res
) do res
.json list_by
(catalog
.tag2proj
)
110 class APICatalogContributors
111 super APICatalogHandler
113 redef fun get
(req
, res
) do
114 var obj
= new JsonObject
115 obj
["maintainers"] = new JsonArray.from
(catalog
.maint2proj
.keys
)
116 obj
["contributors"] = new JsonArray.from
(catalog
.contrib2proj
.keys
)
125 var obj
= new JsonObject
129 obj
["hash"] = (email
or else "").md5
.to_lower