Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:keithnlarsen/nodification
- Loading branch information
Showing
10 changed files
with
265 additions
and
84 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- 0.4 | ||
- 0.6 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// This is an implementation of the Revealing Module Pattern | ||
// ref: http://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript | ||
// | ||
// ---- Example of how to call ---- | ||
// ---- You only need to call init if you want to overide the default options ---- | ||
// var options = { client: someClient, requestHandler: someHandler, eventNotificationUrl: 'someUrl' }; | ||
// gateway.init( options ); | ||
// gateway.register( registrationData, voiceMailType, function ( err, success) { ... }); | ||
// | ||
module.exports = (function() { | ||
var client; | ||
var requestHandler = require( '../libs/requestHandler' ); | ||
var eventNotificationUrl = 'http://localhost:3000/events'; | ||
|
||
function init ( options ) { | ||
client = options.client || client; | ||
requestHandler = options.requestHandler || requestHandler; | ||
eventNotificationUrl = options.eventNotificationUrl || eventNotificationUrl; | ||
} | ||
|
||
function createRequest ( registration, notificationType ) { | ||
var url = require( 'url' ).parse( notificationType.registrationUrl ); | ||
var http = (url.protocol == 'http') ? require( 'http' ) : require( 'https' ); | ||
|
||
client = client || http.createClient( url.port, url.hostname ); | ||
|
||
var requestOptions = { | ||
'Host': url.hostname, | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
}; | ||
|
||
var message = { | ||
'registrationKey': registration.key, | ||
'eventNotificationUrl': eventNotificationUrl, | ||
'notificationType': notificationType.name | ||
}; | ||
|
||
var request = client.request( 'GET', url.path, requestOptions ); | ||
request.write( JSON.stringify( message ) ); | ||
return request; | ||
} | ||
|
||
function register ( registration, notificationType, callBack ) { | ||
requestHandler.handle( createRequest( registration, notificationType ), function( err, response ) { | ||
if ( response.statusCode >= 200 && response.statusCode < 300 ) { | ||
callBack( null, true ); | ||
} else { | ||
callBack( new Error( 'Error registering notification at:' + notificationType.registrationUrl + '. StatusCode ' + response.statusCode + ': ' + response.body ), false ); | ||
} | ||
} ); | ||
} | ||
|
||
return { | ||
init: init, | ||
register: register | ||
}; | ||
}()); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = ( function() { | ||
|
||
function handle( request, callBack ) { | ||
request.on( 'response', function ( response ) { | ||
var responseBody = ""; | ||
response.setEncoding( 'utf8' ); | ||
|
||
response.addListener( "data", function( chunk ) { | ||
responseBody += chunk; | ||
} ); | ||
|
||
response.on( 'end', function() { | ||
response.body = responseBody; | ||
callBack( null, response ); | ||
} ); | ||
} ); | ||
|
||
request.end(); | ||
} | ||
|
||
return { | ||
handle : handle | ||
} | ||
}()); | ||
|
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
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
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
Oops, something went wrong.