Skip to content

Instantly share code, notes, and snippets.

View zachpendleton's full-sized avatar

Zach Pendleton zachpendleton

View GitHub Profile
@zachpendleton
zachpendleton / google_analytics.rb
Created May 7, 2010 20:06
Google Analytics Rack
module Rack
class GoogleAnalytics
def initialize(app,id)
@app = app
@id = id
end
def call(env)
status,headers,response = @app.call(env)
response.each do |part|
class Parser
attr_accessor :results
@@default_services = {}
def parse(document)
document = Nokogiri::XML(document).remove_namespaces!
found = false
@@default_services.each do |service, content|
if document.xpath(content[:identifier]).length > 0
self.send("parse_#{service.to_s}", document)
@zachpendleton
zachpendleton / getParam.js
Created September 15, 2011 17:06
get param from URL (rails friendly)
function getParam(name){
var pathRegex = new RegExp(name + '\/([^\/]+)'),
searchRegex = new RegExp(name + '=([^&]+)'),
match;
match = (window.location.pathname.match(pathRegex) || window.location.search.match(searchRegex));
if (!match) return false;
return match[1];
}
@zachpendleton
zachpendleton / roulette.js
Created November 8, 2011 20:37
setTimeout roulette
(function(){
var old_timeout = setTimeout;
setTimeout = function(fn, timeout) {
if (Math.round(Math.random() * 6) === 1) {
throw 'The cogs of the software industry are greased by the blood of unwary developers. Today that blood is yours.'
} else {
old_timeout(fn, timeout);
}
}
@zachpendleton
zachpendleton / ua_compatible.rb
Created December 6, 2011 23:46
add X-UA-Compatible headers to each request
class UACompatible
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
headers['X-UA-Compatible'] = 'IE=edge,chrome=1'
[status, headers, response]
end
@zachpendleton
zachpendleton / save-current-song.rb
Created June 29, 2012 15:37
Save and format the current Pianobar song for display in tmux.
#!/usr/bin/env ruby
# coding: utf-8
event = ARGV[0]
if event == 'songstart'
d = Hash.new
STDIN.each_line { |line| d.store(*line.chomp.split('=', 2)) }
File.open('/path/to/home/.config/pianobar/current-song.txt', 'w') do |f|
f.write("#[bg=colour39,fg=colour8]♫ #{d['title']} - #{d['artist']} @ #{d['stationName']} #[fg=default,bg=default]")
@zachpendleton
zachpendleton / current-pianobar-song.sh
Created June 29, 2012 15:38
Echo the current Pianobar song for tmux.
#!/bin/zsh
if [ -z `ps aux | grep 'pianobar$'` ]; then
echo ''
else
cat /Users/zpendleton/.config/pianobar/current-song.txt
fi
@zachpendleton
zachpendleton / track-stream.clj
Created July 5, 2012 02:26
Lazy seq of a user's Last.fm tracks
(ns last-fm.track-stream
(require [clj-http.client :as client])
(use cheshire.core))
(def api-key "...")
(def current-song (atom {}))
(def uri "http://ws.audioscrobbler.com/2.0/")
(defn next-song [user]
(decode
@zachpendleton
zachpendleton / pianobar-lastfm.rb
Created July 5, 2012 20:50
Pianobar/LastFM Integration
#!/usr/bin/env ruby
# coding: utf-8
require 'digest/md5'
require 'net/http'
class LastFM
attr_reader :api_key, :secret, :session_key, :base_uri, :namespace