Skip to content

Instantly share code, notes, and snippets.

View troyk's full-sized avatar

Troy Kruthoff troyk

View GitHub Profile
@troyk
troyk / pgrecon.js
Created November 17, 2010 19:01
postgres log outputter
var fs = require("fs"),
spawn = require('child_process').spawn;
var filename = process.ARGV[2];
if (!filename)
return console.log("Usage: node <pgrecon.js> <filename>");
var tail = spawn("tail", ["-f", filename]);
console.log("start tailing");
@troyk
troyk / iptables-block-apache-out
Created February 15, 2011 02:00
Apache user should not be allowed to talk to people
iptables --append OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# create a new chain
iptables --new-chain chk_apache_user
# use new chain to process packets generated by apache (replace apache with uid)
iptables -A OUTPUT -m owner --uid-owner apache -j chk_apache_user
# Allow 143 (IMAP) and 25 so that webmail works :)
iptables -A chk_apache_user -p tcp --syn -d 127.0.0.1 --dport 143 -j RETURN
@troyk
troyk / date.rb
Created April 15, 2011 17:07
Date.parse() with Ruby 1.9 is now defaulting to the European date style, John Wayne would be sad, and our apps don't work right, so this fixes it
# Date.parse() with Ruby 1.9 is now defaulting to the European date style where the format is DD/MM/YYYY, not MM/DD/YYYY
# patch it to use US format by default
class Date
class << self
alias :euro_parse :_parse
def _parse(str,comp=false)
str = str.to_s.strip
if str == ''
{}
elsif str =~ /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{2,4})/
@troyk
troyk / whois.rb
Created May 4, 2011 15:37
Ruby script to find available domain names
#!/usr/bin/env ruby
require "whois"
require "hirb"
$results = []
$alphebet = (97..122).map {|x| x.chr}
$base = "fit"
$alphebet.each do |letter|
["#{letter}#{$base}.com","#{$base}#{letter}.com"].each do |domain|
@troyk
troyk / ruby1.9.2ubuntu
Created June 29, 2011 12:12
Installing Ruby 1.9.2 on ubuntu
apt-get install gcc g++ build-essential libssl-dev libreadline5-dev zlib1g-dev linux-headers-generic libsqlite3-dev
wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz
./configure && make && make install
@troyk
troyk / gist:1098836
Created July 22, 2011 03:34
How to install RMagick on OS X
# download ImageMagick source from http://www.imagemagick.org/script/advanced-unix-installation.php
# get and install libjpeg
curl -O http://www.ijg.org/files/jpegsrc.v8c.tar.gz
cd jpeg-8c
./configure --enable-shared --prefix=/usr/local
make
sudo make install
# install imagemagick
@troyk
troyk / whyme.js
Created October 12, 2011 17:32
Get a list of email addresses from your gmail sent folder using node
// Based off an example from node-imap, will output a list of email addresses from your
// gmail sent folder (so you can copy/paste to the BCC field and let people know your
// sorry for using the same password all over the internet resulting in your buddies
// getting a lame spam message)
var ImapConnection = require('imap').ImapConnection,
imap = new ImapConnection({
username: '{{replace}}@gmail.com',
password: '{{replace}}',
host: 'imap.gmail.com',
@troyk
troyk / gist:1408891
Created November 30, 2011 12:23
How to save your ass from bundle install hell
If you ever run across this error again:
uninitialized constant Psych::Syck (NameError)
sudo gem update --system
IS NOT THE FULL ANSWER DUDE. Although it appears to work, worse problems lie ahead. The real cure is downloading/installing libyaml and re-installing ruby. Atleast that solved my hell.
$ wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
$ tar xzvf yaml-0.1.4.tar.gz
@troyk
troyk / sti_associations_demo.rb
Created March 9, 2012 10:51
Neat little STI association trick, works with to_json but finder include needs some love
# Objective: An association using STI should only query the db once for the base class
class Contact < ActiveRecord::Base
has_many :info, :class_name=>'::Contact::ContactInfo'
has_many :phones, :class_name=>'::Contact::Phone'
has_many :emails, :class_name=>'::Contact::Email'
has_many :addresses, :class_name=>'::Contact::Address'
# load all info's thorugh info
# this is possible due to how associations are mixed into the class via a module
@troyk
troyk / dbo.contacts.sql.js
Created March 20, 2012 16:17
plv8 example
CREATE OR REPLACE FUNCTION dbo.contacts(account_id integer, user_id text, sync text) RETURNS text AS $$
if (typeof DBO !== 'function') executeSql('SELECT dbo.init();');
var Contacts = DBO.Contacts || DBO.extend('Contacts',{
stiContact: function stiContact(contact) {
if (contact.type === 'company') {
delete contact.first_name;
delete contact.last_name;
delete contact.title;