Menu

[r10]: / trunk / Protocols / REST.lua  Maximize  Restore  History

Download this file

92 lines (83 with data), 2.0 kB

 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
---------
-- REST.inc
--
-- Handle REST requests.
--
-- This file is part of Vigie project and covered by
-- Creative Commons Attribution-NonCommercial 3.0 License
-- (http://creativecommons.org/licenses/by-nc/3.0/)
-- professional or commercial usage REQUIRES a commercial licence.
--
-- Copyright 2013 Laurent Faillie
--
-- Expected format is
-- ==================
-- <xxx>
-- <xxx2>
-- <myinfo>
-- <dt>
-- <f1>...</f1>
-- <f2>...</f2>
-- </dt>
-- <dt>
-- <f1>...</f1>
-- <f2>...</f2>
-- </dt>
-- <dt2>
-- <f1>...</f1>
-- <f2>...</f2>
-- </dt2>
-- </myinfo>
-- </xxx2>
-- </xxx>
-- ==================
-- xxx and xxx2 : ignored because outside <myinfo>
-- myinfo : this is the container of information we are looking for.
-- If the file contains this object more than once, only the first
-- one is took in account. (Value of 'start' parameter)
-- dt : alert data we are looking for (Value of 'alert_tag')
-- dt2 : ignored as doesn't matching 'alert_tag' field
-- f1, f2 : field exposed in Vigie GUI.
-- again, all alerts must contains the same fields as the 1st member ; the
-- order doesn't matter.
--
---------
--
-- 05/04/2013 First version
-- 02/05/2013 Switch to CC BY-NC 3.0 licence.
--
---------
local M = { }
function M.get( Mobj )
local http = require("socket.http")
local lom = require("lxp.lom")
local r,c,h = http.request( Mobj.url )
if c ~= 200 then
return nil;
end
local tab = { lom.parse(r) };
local info
for _, v in ipairs(tab) do -- Loop to find start tag
if v.tag == Mobj.container then
info = v
break
end
end
if not info then -- start wasn't found
return nil
end
local res = {}
for _,v in ipairs(info) do
if v.tag == Mobj.alert_tag then
f = {}
for i,content in ipairs(v) do
if content.tag then -- field w/o tag are ignored (noise due to \n)
f[content['tag']] = content[1]
end
end
table.insert(res, f )
end
end
return res
end
return M
Oh no! Some styles failed to load. 😵 Please try reloading this page