Posting to Pusher via agent

Hey gang -

Just wanted to share some code that I had written to publish actions from my imp directly to Pusher’s REST services (http://pusher.com/docs/rest_api), in case it helps to save anyone else time in the future. Definitely rough, but the building of the authentication signature is probably the bit you all might find most useful.

Of course, any thoughts or feedback are always welcome.

`// Agent code

AUTH_KEY <- “YOUR AUTH KEY HERE”;
AUTH_SECRET <- “YOUR APP SECRET HERE”;
APP_ID <- “YOUR APP ID HERE”;

device.on(“click_event”, function(data) {

local body = "{\"name\" : \"click_event\",\"channels\" :[\"test_channel\"],\"data\":\"{\\\\\"type\\\\\":\\\\\""+ data.type+"\\\\\",\\\\\"player\\\\\":"+data.player+"}\"}";

local url = buildURL(body);
local req = http.post(url,{"Content-Type":"application/json"},body);
local res = req.sendsync();

if(res.statuscode != 200) {
    server.log("error sending message: "+res.body);
}

});

function buildURL( body ){

local baseurl = "http://api.pusherapp.com";
local method = "POST"
local action = format( "/apps/%s/events" , APP_ID );
local auth_timestamp = time().tostring()
local auth_version = "1.0";
local body_md5 = BlobToHexString( http.hash.md5( body ) );

local authstring_format = "%s\

%s
auth_key=%s&auth_timestamp=%s&auth_version=%s&body_md5=%s";
local authstring = format( authstring_format , method , action , AUTH_KEY, auth_timestamp , auth_version , body_md5 );

local auth_signature = BlobToHexString( http.hash.hmacsha256( authstring , AUTH_SECRET ) );

local url_format = "%s%s?auth_key=%s&auth_timestamp=%s&auth_version=%s&body_md5=%s&auth_signature=%s";
local url = format( url_format , baseurl , action, AUTH_KEY , auth_timestamp , auth_version , body_md5 , auth_signature );

return url;

}

function BlobToHexString(data) {
local str = “”;
foreach (b in data) str += format("%02x", b);
return str;
}`

Hi metajunkie
Is there a full library for Pusher? Like how to subscribe and receive messages?
Br
Sandeep