UNPKG

xmatters

Version:

Makes calls to the xMatters Communications API

69 lines (52 loc) 1.44 kB
"use strict" var https = require( 'https' ); /* * xMatters client for making REST calls to the Communications API * * opts - Configuration options. Example: { url: "https://company.api.xmatters.com/reapi/2014-06-01/forms/b9ed2ef9-b4ad-4da2-9d2d-c7431931d42e/triggers", api_key: 'key', app_id: 'ID' } * * */ function xMatters( opts ) { exp = /^(.*:)\/\/([a-z\-.]+)(:[0-9]+)?(.*)$/; this.options = { host: opts.url.match( exp )[2], path: opts.url.match( exp )[4], method: 'POST', headers: { 'Content-Type': 'application/json', 'api_key': opts.api_key, 'app_id': opts.app_id } }; /* * makeRequest - inject the event * * body - Event properties * cb - Callback for when complete * */ this.makeRequest = function( body, cb ) { var req = https.request(this.options, function(res) { res.setEncoding('utf8'); var responseString = ''; res.on('data', function (chunk) { responseString += chunk }); res.on('end', function() { console.log("Response string: " + responseString); var responseObject = JSON.parse(responseString); if( cb ) cb( responseString ); }); }); req.write( JSON.stringify( body ) ); req.end(); }; }; exports.xMatters = xMatters;