Created
July 8, 2014 19:12
-
-
Save bverbeken/a2058b8d43c713266cf8 to your computer and use it in GitHub Desktop.
slanger client (nodejs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Slanger = (function() { | |
var crypto = require('crypto'); | |
var request = require('request'); | |
var Slanger = function (options) { | |
this.options = options; | |
this.options.domain = this.options.domain || 'localhost'; | |
this.options.domain = this.options.domain || 4567; | |
this.options.scheme = this.options.scheme || 'http'; | |
return this; | |
}; | |
Slanger.prototype.trigger = function (channel, event, message, callback) { | |
Slanger.modules = { | |
request: request, | |
crypto: crypto | |
}; | |
var eventData = message; | |
var timestamp = parseInt(new Date().getTime() / 1000, 10); | |
var requestBody = JSON.stringify( eventData ); | |
var hash = Slanger.modules.crypto.createHash('md5').update(new Buffer(requestBody).toString('binary')).digest('hex'); | |
var params = [ | |
'auth_key=', this.options.key, | |
'&auth_timestamp=', timestamp, | |
'&auth_version=', '1.0', | |
'&body_md5=', hash, | |
'&name=', event | |
]; | |
var queryString = params.join(''); | |
var path = '/apps/' + this.options.appId + '/channels/' + channel + '/events'; | |
var signData = ['POST', path, queryString].join('\n'); | |
var signature = Slanger.modules.crypto.createHmac('sha256', this.options.secret).update(signData).digest('hex'); | |
path = path + '?' + queryString + '&auth_signature=' + signature; | |
var url = this.options.scheme + '://' + this.options.domain + ( this.options.port === 80? '' : ':' + this.options.port ) + path; | |
Slanger.modules.request.post({ | |
url: url, | |
headers: { | |
'content-type': 'application/json' | |
}, | |
body: requestBody | |
}, function( err, res, body ) { | |
// although using request module the callback signature | |
// needs to be maintained | |
if( typeof callback === 'function' ) { | |
callback( err, this.req, res ); | |
} | |
}); | |
return this; | |
}; | |
return Slanger; | |
})(); | |
var slanger = new Slanger({ | |
appId: 'AAABBBCCC', | |
key: 'AAABBBCCC', | |
secret: 'secret', | |
domain: 'localhost', | |
port: 4567 | |
}); | |
slanger.trigger('event654', "booked", ["Q-70", "Q-72"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment