Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a request helper, and added a gateway which will call backend s…

…ervers to register for notification events
  • Loading branch information...
commit 2d9535cac3e5b240b0d7cb5515f251d2bf924a3c 1 parent a6453d9
@keithnlarsen authored
View
1  .travis.yml
@@ -1,4 +1,3 @@
language: node_js
node_js:
- - 0.4
- 0.6
View
37 gateways/notificationRegistrationGateway.js
@@ -0,0 +1,37 @@
+module.exports = (function() {
+ var client;
+ var reqHelper;
+ var eventNotificationUrl = 'localhost:3000/events';
+
+ return {
+
+ init: function( webClient, requestHelper ) {
+ client = webClient;
+ reqHelper = requestHelper || require( '../libs/requestHelper' );
+ },
+
+ register: function( registration, notificationType, callBack ) {
+ var url = require( 'url' ).parse( notificationType.registrationUrl );
+ client = client || require( http ).createClient( url.port, url.hostname );
+ var request = client.request( 'GET', url.path, {'Host': url.hostname, 'Accept': 'application/json', 'Content-Type': 'application/json'} );
+
+ var message = {
+ 'registrationKey': registration.key,
+ 'eventNotificationUrl': eventNotificationUrl,
+ 'notificationType': notificationType.name
+ };
+
+ request.write( JSON.stringify(message) );
+
+ reqHelper( request, function( response ) {
+ switch ( response.statusCode ) {
+ case 200:
+ callBack( null, response.body );
+ break;
+ default:
+ callBack( new Error( 'Error registering notification at:' + notificationType.registrationUrl + '. StatusCode ' + response.statusCode + ': ' + response.body ), null );
+ }
+ } );
+ }
+ }
+}());
View
17 libs/requestHelper.js
@@ -0,0 +1,17 @@
+module.exports = function( 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( response );
+ } );
+ } );
+
+ request.end();
+};
View
18 tests/integration/controllers/notificationTypeControllerTest.js
@@ -9,23 +9,7 @@ describe( 'Nodification.Tests.Integration.Controllers.NotificationTypeController
var app;
var localhost = http.createClient( 3000, 'localhost' );
- var requestHelper = function( request, fn ) {
- request.end();
-
- request.on( 'response', function ( response ) {
- var responseBody = "";
- response.setEncoding( 'utf8' );
-
- response.addListener( "data", function( chunk ) {
- responseBody += chunk;
- } );
-
- response.on( 'end', function() {
- response.body = responseBody;
- fn( response );
- } );
- } );
- };
+ var requestHelper = require('../../../libs/requestHelper');
before( function( done ) {
var app = require( '../../../app' );
View
18 tests/integration/controllers/registrationControllerTest.js
@@ -13,23 +13,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationController', f
var app;
var localhost = http.createClient( 3000, 'localhost' );
- var requestHelper = function( request, fn ) {
- request.end();
-
- request.on( 'response', function ( response ) {
- var responseBody = "";
- response.setEncoding( 'utf8' );
-
- response.addListener( "data", function( chunk ) {
- responseBody += chunk;
- } );
-
- response.on( 'end', function() {
- response.body = responseBody;
- fn( response );
- } );
- } );
- };
+ var requestHelper = require('../../../libs/requestHelper');
before( function( done ) {
var app = require( '../../../app' );
View
18 tests/integration/controllers/registrationDeviceControllerTest.js
@@ -15,23 +15,7 @@ describe( 'Nodification.Tests.Integration.Controllers.RegistrationDeviceControll
var app;
var localhost = http.createClient( 3000, 'localhost' );
- var requestHelper = function( request, fn ) {
- request.end();
-
- request.on( 'response', function ( response ) {
- var responseBody = "";
- response.setEncoding( 'utf8' );
-
- response.addListener( "data", function( chunk ) {
- responseBody += chunk;
- } );
-
- response.on( 'end', function() {
- response.body = responseBody;
- fn( response );
- } );
- } );
- };
+ var requestHelper = require('../../../libs/requestHelper');
before( function( done ) {
var app = require( '../../../app' );
Please sign in to comment.
Something went wrong with that request. Please try again.