-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathadmediator_customHtml.lua
101 lines (72 loc) · 2.49 KB
/
admediator_customHtml.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
------------------------------------------------------------
------------------------------------------------------------
-- Ad Mediator for Corona
--
-- Ad network mediation module for Ansca Corona
-- by Deniz Aydinoglu
--
-- he2apps.com
--
-- GitHub repository and documentation:
-- https://github.com/deniza/Ad-Mediator-for-Corona
------------------------------------------------------------
------------------------------------------------------------
local instance = {}
local requestParams
local requestType
local requestUrl
local rawResponse
local metaTag = AdMediator.viewportMetaTagForPlatform()
local function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
local function adRequestListener(event)
local available = true
local i,f,imageUrl,adUrl
local htmlContent = ""
if event.isError or event.response == "" then
available = false
else
if rawResponse then
htmlContent = event.response
else
htmlContent = '<html><head>'..metaTag..'</head><body style="margin:0; padding:0;">'..event.response..'</body></html>'
end
end
Runtime:dispatchEvent({name="adMediator_adResponse",available=available,htmlContent=htmlContent})
end
function instance:init(networkParams)
requestUrl = networkParams.requestUrl
requestType = networkParams.requestType or "GET"
requestParams = networkParams.requestParams or {}
rawResponse = networkParams.rawResponse or false
print("customHtml init:",requestServer)
end
function instance:requestAd()
if requestType == "POST" then
local headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Accept"] = "text/html"
local params = {}
params.headers = headers
params.body = ""
for key,value in pairs(requestParams) do
params.body = params.body .. key .. "=" .. value .. "&"
end
network.request(requestUrl,"POST",adRequestListener,params)
else
-- GET request
local requestUri = requestUrl .. "?"
for key,value in pairs(requestParams) do
requestUri = requestUri .. key .. "=" .. urlencode(value) .. "&"
end
network.request(requestUri,"GET",adRequestListener)
end
end
return instance