Skip to content

Commit

Permalink
Did this on holiday; loads to talk about.
Browse files Browse the repository at this point in the history
- Added support for setting a secret
- Refactored the whole project into a module
- Added a bin script to connect and monitor on demand
- Added a gulp script to build with browserify (see dist directory)
  • Loading branch information
Codesleuth committed Jun 22, 2015
1 parent bf07be8 commit ca8b07c
Show file tree
Hide file tree
Showing 7 changed files with 7,202 additions and 58 deletions.
57 changes: 57 additions & 0 deletions bin/push-client.js
@@ -0,0 +1,57 @@
#!/usr/bin/env node

var log = require('../logger'),
pushclient = require('../index'),
request = require('request'),
minimist = require('minimist');

var argv = minimist(process.argv.slice(2), {
string: 'secret',
default: {
secret: false,
url: 'https://push-broker.herokuapp.com'
}
});

if (!argv.secret) {
log.error('Please specify a secret with: --secret "your secret"')
process.exit(1);
}

var client = pushclient.create({
url: argv.url,
secret: argv.secret,
onsecret: function () {
log.info('Secret has been set.');
},
onpush: function (data) {
console.log('PushEvent received: %s', data);

var options = {
url: 'http://localhost:3000/pushed-it',
headers: {
'X-Github-Event' : 'push',
'X-Github-Delivery' : data.headers['X-Github-Delivery'],
'X-Hub-Signature' : data.headers['X-Hub-Signature'],
'User-Agent' : data.headers['User-Agent']
},
form: {
payload: data.body
}
};

log.info('Sending PushEvent to CI...');
request.post(options, function (err, res, body) {
if (err)
return log.error('CI error:', err);

if (res.statusCode !== 200) {
log.error('CI returned status code: %d', res.statusCode);
log.error('body: %s', body);
return;
}

log.info('Sent PushEvent to CI successfully.');
});
}
});
43 changes: 43 additions & 0 deletions dist/push-client.js
@@ -0,0 +1,43 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pushclient = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var wdw = typeof window === 'undefined' ? {} : window;
var io = wdw.io || require('socket.io-client');

var defaultUrl = 'https://push-broker.herokuapp.com';

function ConnectClient(url, secret, onpush, onsecret) {
var socket = io(url, { multiplex: true });

socket.on('connect', function () {
socket.emit('secret', secret, function () {
onsecret(secret);
});
})

socket.on('PushEvent', function (data) {
onpush(data);
});

this.socket = socket;
};

function PushClient() {
}

PushClient.prototype.disconnect = function() {
this.socket.disconnect();
};


module.exports.create = function (options) {
options = options || {};
url = options.url || defaultUrl;
secret = options.secret || '';
onpush = options.onpush || function () {};
onsecret = options.onsecret || function () {};

var client = new PushClient();
ConnectClient.call(client, url, secret, onpush, onsecret);
return client;
}
},{"socket.io-client":undefined}]},{},[1])(1)
});

0 comments on commit ca8b07c

Please sign in to comment.