@@ -14,48 +14,46 @@ data class Ad(val redirectUrl: String, val text: String)
14
14
data class AdRequest (val contextKeys : List <String >? = null )
15
15
data class AdResponse (val ads : List <Ad >)
16
16
17
- class AdModule {
18
- private val database: Map <String , Ad > = loadDatabase()
19
-
20
- @Verb
21
- @HttpIngress(Method .GET , " /get" )
22
- fun get (context : Context , req : HttpRequest <AdRequest >): HttpResponse <AdResponse , String > {
23
- val ads: List <Ad > = when {
24
- req.body.contextKeys != null -> contextualAds(req.body.contextKeys)
25
- else -> randomAds()
26
- }
27
-
28
- return HttpResponse (status = 200 , headers = emptyMap(), body = AdResponse (ads = ads))
17
+ private val database: Map <String , Ad > = loadDatabase()
18
+
19
+ @Verb
20
+ @HttpIngress(Method .GET , " /get" )
21
+ fun get (context : Context , req : HttpRequest <AdRequest >): HttpResponse <AdResponse , String > {
22
+ val ads: List <Ad > = when {
23
+ req.body.contextKeys != null -> contextualAds(req.body.contextKeys)
24
+ else -> randomAds()
29
25
}
30
26
31
- private fun contextualAds (contextKeys : List <String >): List <Ad > {
32
- return contextKeys.map { database[it] ? : throw Exception (" no ad registered for this context key" ) }
33
- }
27
+ return HttpResponse (status = 200 , headers = emptyMap(), body = AdResponse (ads = ads))
28
+ }
34
29
35
- private fun randomAds (): List <Ad > {
36
- val ads = mutableListOf<Ad >()
37
- val random = Random ()
38
- repeat(MAX_ADS_TO_SERVE ) {
39
- ads.add(database.entries.elementAt(random.nextInt(database.size)).value)
40
- }
41
- return ads
42
- }
30
+ private fun contextualAds (contextKeys : List <String >): List <Ad > {
31
+ return contextKeys.map { database[it] ? : throw Exception (" no ad registered for this context key" ) }
32
+ }
43
33
44
- companion object {
45
- private const val MAX_ADS_TO_SERVE = 2
46
- private val DATABASE = mapOf (
47
- " hair" to Ad (" /product/2ZYFJ3GM2N" , " Hairdryer for sale. 50% off." ),
48
- " clothing" to Ad (" /product/66VCHSJNUP" , " Tank top for sale. 20% off." ),
49
- " accessories" to Ad (" /product/1YMWWN1N4O" , " Watch for sale. Buy one, get second kit for free" ),
50
- " footwear" to Ad (" /product/L9ECAV7KIM" , " Loafers for sale. Buy one, get second one for free" ),
51
- " decor" to Ad (" /product/0PUK6V6EV0" , " Candle holder for sale. 30% off." ),
52
- " kitchen" to Ad (" /product/9SIQT8TOJO" , " Bamboo glass jar for sale. 10% off." )
53
- )
54
-
55
- private fun loadDatabase (): Map <String , Ad > {
56
- return DATABASE
57
- }
58
-
59
- inline fun <reified T > Gson.fromJson (json : String ) = fromJson<T >(json, object : TypeToken <T >() {}.type)
34
+ private fun randomAds (): List <Ad > {
35
+ val ads = mutableListOf<Ad >()
36
+ val random = Random ()
37
+ repeat(MAX_ADS_TO_SERVE ) {
38
+ ads.add(database.entries.elementAt(random.nextInt(database.size)).value)
60
39
}
40
+ return ads
61
41
}
42
+
43
+ private const val MAX_ADS_TO_SERVE = 2
44
+ private val DATABASE = mapOf (
45
+ " hair" to Ad (" /product/2ZYFJ3GM2N" , " Hairdryer for sale. 50% off." ),
46
+ " clothing" to Ad (" /product/66VCHSJNUP" , " Tank top for sale. 20% off." ),
47
+ " accessories" to Ad (" /product/1YMWWN1N4O" , " Watch for sale. Buy one, get second kit for free" ),
48
+ " footwear" to Ad (" /product/L9ECAV7KIM" , " Loafers for sale. Buy one, get second one for free" ),
49
+ " decor" to Ad (" /product/0PUK6V6EV0" , " Candle holder for sale. 30% off." ),
50
+ " kitchen" to Ad (" /product/9SIQT8TOJO" , " Bamboo glass jar for sale. 10% off." )
51
+ )
52
+
53
+ private fun loadDatabase (): Map <String , Ad > {
54
+ return DATABASE
55
+ }
56
+
57
+ inline fun <reified T > Gson.fromJson (json : String ) = fromJson<T >(json, object : TypeToken <T >() {}.type)
58
+
59
+
0 commit comments