---------
-- 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