Skip to content

Instantly share code, notes, and snippets.

@shanna
shanna / ad_version_1.rb
Created May 21, 2012 07:23
Simple example of why mocks are stupid.
# First iteration of Ad
class Ad
attr_accessor :message
def view
"For sale #{message} call #{owner.name}"
end
end # Ad
@shanna
shanna / path.rb
Created May 17, 2012 05:40
JSONPath like access to Oj JSON structs.
require 'delegate'
require 'oj'
require 'strscan'
# JSONPath like access to Oj JSON structs.
#
# @note Array access is identical to Array#[]
class Oj::Path < SimpleDelegator
class Error < StandardError; end
class MissingError < Error; end
@shanna
shanna / uri.js
Created April 21, 2012 04:04 — forked from jlong/uri.js
URI Parsing with Javascript
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.host; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
(function ($) {
var providers = {};
/*
Given a URL find a provider that can OEmbed the url given.
*/
function find (url) {
var site = url.match(/^https?:\/\/([^/]+)/)[1];
for (var domain in providers)
if (site.indexOf(domain) > -1)
@shanna
shanna / util.hpp
Created February 24, 2012 02:59
Casting to and from strings.
#pragma once
#include <iostream>
#include <sstream>
#include <string>
template<typename T>
T string_to(const std::string& p_string) {
std::istringstream iss(p_string);
T x;
@shanna
shanna / gist:1873425
Created February 21, 2012 03:29
Tiny PHP MySQL abstraction.
<?php
# Mysql
#
# require_once('mysql.php');
# $db = new Mysql(dbname, server, user, pass);
# $sth = $db->query('select * from blah where name = ? limit 1', 'fred') || die($db->error());
# while ($row = mysql_fetch_assoc($sth))
# ...
#
@shanna
shanna / layout
Created August 2, 2011 09:24
Basic application structure.
/
/bin
#{project}
#{project}-cli
#{project}-db
#{project}-job
#{project}-web
#{project}-config
...
* We stole the CLI command hierarchy from git. E.g. ./bin/project web (start|stop|status) [options]
@shanna
shanna / installation.sh
Created June 20, 2011 16:38 — forked from mikhailov/installation.sh
Nginx+passenger application config: ssl redirection, http headers, passenger optimal settings. see details: http://mikhailov.posterous.com/nginx
$ cd /usr/src
$ wget http://nginx.org/download/nginx-0.8.52.tar.gz
$ tar xzvf ./nginx-0.8.52.tar.gz
$ rm ./nginx-0.8.52.tar.gz
$ gem install s3sync capistrano capistrano-ext passenger --no-ri --no-rdoc
$ passenger-install-nginx-module
# Automatically download and install Nginx? 2. No: I want to customize my Nginx installation
# Where is your Nginx source code located?: /usr/src/nginx-0.8.52
# Where do you want to install Nginx to?: /opt/nginx
@shanna
shanna / gist:945986
Created April 28, 2011 07:51
Sinatra::Map
require 'sinatra/base'
require 'rack/utils'
module Sinatra
class Base
module Map
def map path, klass
path = map_compile path
[:get, :put, :post, :delete, :head].each do |verb|
send(verb, path) do
@shanna
shanna / gist:884377
Created March 24, 2011 01:12
shingles.rb
require 'set'
#==== See
# * http://github.com/matpalm/resemblance
class String
module Shingles
N_GRAM_LENGTH = 3
def shingles
@shingles ||= -> do