1 module
:depends("http");
3 local lom
= require
"lxp.lom";
4 local st
= require
"util.stanza";
5 local json
= require
"util.json";
6 local datetime
= require
"util.datetime".datetime
;
9 local pubsub_service
= module
:depends("pubsub").service
;
10 local node
= module
:get_option("pivotaltracker_node", "tracker");
12 local stanza_mt
= require
"util.stanza".stanza_mt
;
13 local function stanza_from_lom(lom
)
15 local child_tags
, attr
= {}, {};
16 local stanza
= setmetatable({ name
= lom
.tag, attr
= attr
, tags
= child_tags
}, stanza_mt
);
17 for i
, attr_name
in ipairs(lom
.attr
) do
18 attr
[attr_name
] = lom
.attr
[attr_name
]
20 for i
, child
in ipairs(lom
) do
22 child
= stanza_from_lom(child
);
23 child_tags
[#child_tags
+1] = child
;
33 function handle_POST(event
)
34 local data
= lom
.parse(event
.request
.body
);
37 return "Invalid XML. From you of all people...";
40 data
= stanza_from_lom(data
);
42 if data
.name
~= "activity" then
43 return "Unrecognised XML element: "..data
.name
;
46 local activity_id
= data
:get_child("id"):get_text();
47 local description
= data
:get_child("description"):get_text();
48 local author_name
= data
:get_child("author"):get_text();
49 local story
= data
:get_child("stories"):get_child("story");
50 local story_link
= story
:get_child("url"):get_text();
52 local ok
, err
= pubsub_service
:publish(node
, true, "activity", st
.stanza("item", { id
= "activity", xmlns
= "http://jabber.org/protocol/pubsub" })
53 :tag("entry", { xmlns
= "http://www.w3.org/2005/Atom" })
54 :tag("id"):text(activity_id
):up()
55 :tag("title"):text(description
):up()
56 :tag("link", { rel
= "alternate", href
= story_link
}):up()
57 :tag("published"):text(datetime()):up()
59 :tag("name"):text(author_name
):up()
63 module
:log("debug", "Handled POST: \n%s\n", tostring(event
.request
.body
));
64 return "Thank you Pivotal!";
67 module
:provides("http", {
73 function module
.load()
74 if not pubsub_service
.nodes
[node
] then
75 local ok
, err
= pubsub_service
:create(node
, true);
77 module
:log("error", "Error creating node: %s", err
);
79 module
:log("debug", "Node %q created", node
);