Skip to content

Instantly share code, notes, and snippets.

View Stanback's full-sized avatar

Brian Stanback Stanback

  • Demand.io
  • Los Angeles, California
View GitHub Profile
@Stanback
Stanback / phantom_test.js
Last active August 29, 2015 14:00
PhantomJS test of the XContentReady event
/*
* Simple test of PhantomJS and the XContentReady event.
*
* This is the basic pattern used for the ember-prerender project: https://github.com/zipfworks/ember-prerender
* More info on the XContentReady event: https://github.com/n-fuse/the-XContentReady-Event/
*
* Usage: phantomjs phantom_test.js
*/
var page = require('webpage').create();
@Stanback
Stanback / mongodb_curator.js
Created October 15, 2014 18:42
MongoDB Curator
//
// This script is useful for managing time series or log data
// that is stored in MongoDB.
//
// I'm currently using logstash to store system logs in MongoDB, where
// each database name denotes type of log and each collection name
// reflects the date on which the log entries it contains occurred.
//
// This script expects database collections to be in YYYY.MM.DD format.
//
@Stanback
Stanback / If-Modified-Since.php
Created October 27, 2014 17:19
Example of date-based browser caching
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($path_to_image)) {
header('HTTP/1.0 304 Not Modified');
header('Cache-Control: public, max-age=2592000');
header('Last-Modified: Mon, 27 Oct 2014 17:08:41 GMT');
exit(0);
} else {
header('Content-type: image/jpeg');
header('Cache-Control: public, max-age=2592000');
header('Last-Modified: Mon, 27 Oct 2014 17:08:41 GMT');
echo file_get_contents($path_to_image);
#
# Oracle Java8 Dockerfile
#
FROM ubuntu
RUN apt-get update; \
apt-get install -y \
software-properties-common
@Stanback
Stanback / requestLogger.js
Last active November 18, 2015 17:32
Request logger middleware example using winston logger
// To use:
// server.use(requestLogger);
function requestLogger(req, res, next) {
req._startTime = new Date();
var logRequest = function() {
res.removeListener('finish', logRequest);
res.removeListener('close', logRequest);
var status = res.headersSent && res.statusCode || null;
var userAgent = req.headers['user-agent'];
@Stanback
Stanback / emblem2hbs.sh
Created December 1, 2015 05:04
Convert all emblem files to handlebars format (emblem2hbs.sh)
#!/bin/bash
# Install emblem2hbs cli
npm -g install emblem2hbs
# Convert all emblem files to handlebars
for f in $(ls -1 {,**/}*.emblem); do; emblem2hbs $f; done;
# Optionally delete original emblem files
find . -name "*.emblem" -exec rm {} \;
@Stanback
Stanback / api-rc.sh
Last active January 2, 2016 13:09
Example RC script that I've used for a Java/Scala/JVM service
#!/bin/bash
#
# This script provides start/stop/restart/status control
# over the JVM-based API service.
#
# Build Environment
BUILD_NAME="your-build-name"
BASE_DIR="/home/ubuntu/${BUILD_NAME}-deploy"
@Stanback
Stanback / getOrdinal.js
Created August 23, 2016 20:14
Convert integer to an ordinal number (e.g. 1st, 2nd, 3rd, etc)
function getOrdinal(value) {
const suffixes = ['th', 'st', 'nd', 'rd'];
const normalized = value % 100;
return value + (suffixes[(normalized - 20) % 10] || suffixes[normalized] || suffixes[0]);
}
@Stanback
Stanback / Dockerfile
Created March 28, 2015 01:21
PhantomJS 2.0 Dockerfile
#
# Example Dockerfile that builds PhantomJS 2.0
#
# Build with:
# docker build --rm --tag=phantom2.0:latest .
#
# Run with:
# docker run --name=phantom2.0 phantom2.0
#
# Copy the executable to your host machine for distribution:
@Stanback
Stanback / fixuri.scala
Last active December 14, 2016 16:59
Regex for fixing improperly formatted URIs
/*
* Snippet to encode invalid characters from improperly formatted URIs
*
* RFC 3986 defines that URIs may contain the following characters:
* ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=`.```
*/
import java.net.URLEncoder
"""[^A-Za-z0-9-._~:/?#\[\]@!$&'\(\)*+,;=%`]|%[^0-9a-fA-F]{2}]""".r.