Skip to content

Instantly share code, notes, and snippets.

View jamesmorgan's full-sized avatar

James Morgan jamesmorgan

View GitHub Profile
@jamesmorgan
jamesmorgan / GamesService.ConnectableObservable.ts
Last active April 1, 2016 07:48
GamesService.ConnectableObservable.ts
@Injectable()
export class GamesService {
/** Internal model state */
private games:Object[];
/** Private Subject **/
private _gamesSource:Subject<Object[]> = new Subject<Object[]>();
/** Public Observer **/
@jamesmorgan
jamesmorgan / ConnectableObservable.Subscriber.ts
Created April 1, 2016 07:54
ConnectableObservable.Subscriber.ts
export class GamesComponent implements OnDestroy {
private _gamesSubscription:Subscription;
constructor(private _gamesService:GamesService){
/**
* If we had already emitted values down this observable we would immediately receive them here, without re-requesting them.
* We would be able to quickly populate the component and if need re-request the data from within the ngOnInit lifecycle hook.
*/
this._gamesSubscription = this._gamesService.gamesChanged$.subscribe((games) => {
@jamesmorgan
jamesmorgan / callbackhell.js
Created August 2, 2016 12:30
callbackhell.js
fs.readdir(source, function (err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function (filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function (err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
@jamesmorgan
jamesmorgan / logExceptionToSlack.js
Created May 14, 2017 12:52
Pushes Data from a Firebase DB ref to Slack
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const IncomingWebhook = require('@slack/client').IncomingWebhook;
const url = 'https://hooks.slack.com/services/${SLACK_API}';
const functions = require('firebase-functions');
/**
* Pushes from firebase ref /exception to slack chanel
@jamesmorgan
jamesmorgan / emailProcessor.js
Created May 14, 2017 19:46
Sample flow for email processing
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const functions = require('firebase-functions');
exports.emailProcessor = functions.database.ref('/email/raw/{emailId}').onWrite(event => {
if (event.data.exists()) {
let rawEmail = event.data.val();
@jamesmorgan
jamesmorgan / contactRelationUpdate.js
Last active July 30, 2017 15:11
contactRelationUpdate event hook to persist data to firebase
bot.on("contactRelationUpdate", (message) => {
const userId = message.user.id;
const username = message.user.name || "--";
const channelId = message.address.channelId;
const address = message.address;
// When the users adds the bot
if (message.action === "add") {
// Persist the user to firebase
const userId = session.message.message.user.id;
const channelId = session.message.address.channelId;
// Load the specified user
firebase.database().ref(`chat-users/${channelId}/${userId}`)
.once('value')
.then((snapshot) => {
// Get the ref data
let user = snapshot.val();
@jamesmorgan
jamesmorgan / index.js
Created February 19, 2018 09:07
Firebase cloud function - http trigger example
const functions = require('firebase-functions');
const admin = require('firebase-admin');
// Define a onRequest trigger - this will be invoked from every webhook
exports.coinbaseToFirebase = functions.https.onRequest((req, res) => {
// Simply log the headers & body (Debug only)
console.log(JSON.stringify(req.headers, null, 4));
console.log(JSON.stringify(req.body, null, 4));
@jamesmorgan
jamesmorgan / coinbase-commerce-purchase.json
Created February 19, 2018 13:27
example coinbase commerce product purchase webhook payload
{
"attempt_number": 5,
"event": {
"created_at": "2018-02-16T08:50:20Z",
"data": {
"id": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"name": "Test",
"status": "NEW",
"pricing": {
"BCH": {
{
"title": "Asset Metadata",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Identifies the asset to which this NFT represents",
},
"description": {
"type": "string",