comparison mod_pubsub_pivotaltracker/mod_pubsub_pivotaltracker.lua @ 859:9922e8cdf4a4

mod_pubsub_pivotaltracker: Accept activity notifications from Pivotal Tracker web hooks and relay them to a pubsub node
author Matthew Wild <mwild1@gmail.com>
date Wed, 21 Nov 2012 23:30:14 -0500
parents
children 7dbde05b48a9
comparison
equal deleted inserted replaced
858:28dba9608499 859:9922e8cdf4a4
1 module:depends("http");
2
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;
7
8
9 local pubsub_service = module:depends("pubsub").service;
10 local node = module:get_option("pivotaltracker_node", "tracker");
11
12 local stanza_mt = require "util.stanza".stanza_mt;
13 local function stanza_from_lom(lom)
14 if lom.tag then
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]
19 end
20 for i, child in ipairs(lom) do
21 if child.tag then
22 child = stanza_from_lom(child);
23 child_tags[#child_tags+1] = child;
24 end
25 stanza[i] = child;
26 end
27 return stanza;
28 else
29 return lom;
30 end
31 end
32
33 function handle_POST(event)
34 local data = lom.parse(event.request.body);
35
36 if not data then
37 return "Invalid XML. From you of all people...";
38 end
39
40 data = stanza_from_lom(data);
41
42 if data.name ~= "activity" then
43 return "Unrecognised XML element: "..data.name;
44 end
45
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();
51
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()
58 :tag("author")
59 :tag("name"):text(author_name):up()
60 :up()
61 );
62
63 module:log("debug", "Handled POST: \n%s\n", tostring(event.request.body));
64 return "Thank you Pivotal!";
65 end
66
67 module:provides("http", {
68 route = {
69 POST = handle_POST;
70 };
71 });
72
73 function module.load()
74 if not pubsub_service.nodes[node] then
75 local ok, err = pubsub_service:create(node, true);
76 if not ok then
77 module:log("error", "Error creating node: %s", err);
78 else
79 module:log("debug", "Node %q created", node);
80 end
81 end
82 end