Skip to content

Instantly share code, notes, and snippets.

@Chris927
Chris927 / auntie.js
Created June 12, 2012 10:53
fun exercise around aunt Lily and her cats...
/* fun exercise around aunt Lily and her cats... */
(function (owner) {
function randomInt(i) {
return Math.floor(Math.random() * i);
}
function randomText() {
var texts = [
@Chris927
Chris927 / copyable_attributes.rb
Created March 17, 2013 15:30
Failed attempt to implement generic 'deep copy' for AR... I'm just keeping it to document the thought process and the dead end I've reached.
module CopyableAttributes
def self.included(base)
def copyable_attributes_keys2
attributes.keys - self.class.protected_attributes.to_a - [ "created_at", "updated_at"] - foreign_key_list
# TODO: we also need to remove foreign keys... maybe have class method 'uncopyable_attributes_keys'?
end
private
def relevant_base_class
@Chris927
Chris927 / why.js
Created July 2, 2014 07:13
Nice Little Javascript Scope Challenge
// credit: http://rzrsharp.net/2011/06/27/what-does-coffeescripts-do-do.html
var closures = [];
function createClosures() {
for (var i = 0; i < 5; i++) {
closures[i] = function() {
console.log("i=" + i);
}
}
}
function floor(i, n) {
var r = i % n;
return i - r;
};
function floorToHours(date, nHours) {
var hourInMillis = 1000 * 60 * 60;
var dateInMillis = date.valueOf();
return new Date(floor(dateInMillis, nHours * hourInMillis));
};
// some dummy rows
var rows = [];
for (var i = 0; i < 10000; i++) {
rows.push("this is row " + i);
}
var index = 0;
var currentlyRunning = 0, maxRunning = 200;
function callHandler(row, cb) {
// same as previous gist, but now via 'async' library
var async = require('async');
// some dummy rows
var rows = [];
for (var i = 0; i < 10000; i++) {
rows.push("this is row " + i);
}
var EventsEmitter = require('events').EventEmitter;
pg.connect(..., function(err, client, done) {
var pageSize = 50;
var emitter = new EventsEmitter();
emitter.on('next', function(offset) {
callHandlersPaginated(offset);
});
emitter.on('end', function() {
@Chris927
Chris927 / demo.conf
Created July 24, 2014 20:05
How I got rsyslog to email me when 'demo' or 'test' appears (/etc/rsyslog.d/demo.conf)
$ModLoad /usr/lib/rsyslog/ommail.so
$ActionMailSMTPServer localhost
$ActionMailFrom rsyslog@uber5.com
$template mailSubject,"Rsyslog alert for %hostname% at %timegenerated%"
$template mailBody,"%msg%"
$ActionMailSubject mailSubject
$ActionExecOnlyonceEveryInterval 60
$ActionMailTo chris@uber5.com
if ($msg contains 'demo') or ($msg contains 'test') then :ommail:;mailBody
- certain endpoints are always blocked
if nginx_uri == "/_access_token" or nginx_uri == "/_me" then
ngx.exit(403)
end
-- import requirements
local cjson = require "cjson"
-- setup some app-level vars
local app_id = "APP_ID"
@Chris927
Chris927 / server.js
Last active November 25, 2015 14:40
NodeJS Express, use middleware to protect routes
var express = require('express');
var app = express();
var port = 3100;
app.listen(port, function() {
console.log('listening on port ' + port);
});
/* public routes */