Skip to content

Instantly share code, notes, and snippets.

@nycdavid
nycdavid / gist:0424f674b928f48dd406
Created May 31, 2014 02:24
Mastering Node.js: Chap.1 Events and Emitters
var EventEmitter = require('events').EventEmitter;
// Create the initial Counter class, that emits the named event
var Counter = function(init) {
this.increment = function() {
init++;
this.emit('incrementer', init);
}
};
var fs = require('fs'),
path = '/path/to/sql/file'; // This represents a 4-5 gb file, way too large for a buffer in memory
// Without Streams
fs.readFile(path, 'utf8', function(err, data) { // This will fail because the size is too large for BUffer creation
if (err) throw err;
console.log(data);
});
// With Streams
@nycdavid
nycdavid / gist:ea8a653e054d6b696e0a
Created June 6, 2014 14:12
Salud: Import script for JSON foods
var fs = require('fs'),
mongoClient = require('mongodb').MongoClient,
foodsJson = JSON.parse(fs.readFileSync('./foods-2011-10-03.json'));
mongoClient.connect('mongodb://127.0.0.1:27017/salud', function(err, db) {
if (err) throw err;
var collection = db.collection('foods-orig');
foodsJson.forEach(function(el, i) {
@nycdavid
nycdavid / gist:cd6723a483f458b50fa4
Created August 10, 2015 16:52
Example jwt auth middleware
require "pry"
class JwtAuthentication
def initialize(app)
@app = app
end
def call(env)
req = Rack::Request.new(env)
secret = ";Xsc2}M48w33xUvL7N>SG-V}OnU??BG&O&|!az,)yHpLgm/|g,kj^iVNwr<SFqy"
@nycdavid
nycdavid / project_folder_generator.rb
Last active January 7, 2016 17:45
ProjectFolderGenerator class
require "csv"
require "fileutils"
TYPES = ["Instrumental", "Vocal"]
GENRES = [
"Ambient", "Americana", "Blues", "Childrens", "Classical",
"Country", "Disco", "Easy Listening", "Electronica", "Folk",
"Funk", "Gospel", "Hip Hop", "Holiday", "Jazz", "Latin",
"Lounge", "None", "Orchestral", "Pop", "R&B", "Reggae",
@nycdavid
nycdavid / avid_mover.rb
Last active June 10, 2016 16:22
AvidMover class
require "fileutils"
class AvidMover
Bin = Struct.new(:type, :genre, :filename)
def initialize(project_path)
@project_path = project_path
end
def move_bins
@nycdavid
nycdavid / test.go
Last active January 20, 2016 22:39
template.Parse("<h1>Foo</h1>")
template.Parse(`<h1>Foo</h1>`)
console.log($('body'));
@nycdavid
nycdavid / info.MD
Last active October 25, 2017 15:36
Grokking Faktory
  • Getting a CLI Shell to talk to the Faktory job server: cat <(echo "HELLO {\"wid\":\"4qpc2443vpvai\", \"labels\": [\"golang\"]}") - | nc localhost 7419

Enqueuing a test job from command line:

  1. First, open a persistent connection via Netcat to the Faktory job server:
      $> cat <(echo "HELLO {\"wid\":\"4qpc2443vpvai\", \"labels\": [\"golang\"]}") - | nc localhost 7419
      #=> +HI {"v":"1"}
      #=> +OK
    
  2. Then, PUSH a job to the end of the queue, with at least the minimum parameters of jid, jobtype, args:
@nycdavid
nycdavid / valuer.go
Created November 9, 2017 22:11 — forked from jmoiron/valuer.go
Example uses of sql.Scanner and driver.Valuer
package main
import (
"bytes"
"compress/gzip"
"database/sql/driver"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"