Skip to content

Instantly share code, notes, and snippets.

View jakeonrails's full-sized avatar

Jake Moffatt jakeonrails

View GitHub Profile
@jakeonrails
jakeonrails / parser_spec.rb
Created January 25, 2014 00:38
Reverse Polish Notation parser
require 'spec_helper'
class Parser
def parse(string)
stack = []
tokens = string.split(' ')
tokens.each do |token|
if token =~ /\d/
value = token.to_i
@jakeonrails
jakeonrails / json_api_dsl.rb
Created August 13, 2013 03:24
JSON API test DSL
describe 'POST api/v1/sessions' do
def self.request(options)
let(:api_response) {
send(
options[:method].downcase,
options[:path],
options[:json],
)
last_response
}
@jakeonrails
jakeonrails / fix_newrelic_pow.md
Created April 11, 2013 21:12
Newrelic developer mode returns "no route matches" when you hit /newrelic and you happen to be running pow.
@jakeonrails
jakeonrails / ping_pong.rb
Created February 27, 2013 20:08
PING PONG ENUMERATOR!
pinger = Enumerator.new do |yielder|
loop
yielder << "PING"
end
end
pinger.each do |ping|
puts ping
puts "PONG"
end
# Assuming all the methods called here also return true/false,
# use the logical composition operators to create a final result
# which can short-circuit if one of the methods fails:
def composed_method
frobulate_widgets and refrobulate_widgets and confribulate_frobulations
end
describe '#game_won?' do
winning = [
<<-BOARD
.x...
ox...
oxo..
oxo..
BOARD
]
@jakeonrails
jakeonrails / benchmark.rb
Created November 6, 2012 23:15 — forked from panthomakos/benchmark.rb
Benchmark Your Bundle
#!/usr/bin/env ruby
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]
@jakeonrails
jakeonrails / gist:3895690
Created October 15, 2012 21:34
Postgres table and index sizes
SELECT CASE WHEN total IS NULL THEN '' ELSE name END AS table,
index,
pg_size_pretty(size) AS size,
CASE WHEN total IS NULL THEN '' ELSE pg_size_pretty(total) END AS total
FROM
(SELECT name,
index,
size,
total
FROM
@jakeonrails
jakeonrails / robot_user_agents.rb
Created October 15, 2012 21:37 — forked from Sjors/robot_user_agents.rb
Recognize search engines and spammers using user-agents.org
require 'net/http'
require 'xmlsimple'
url = "http://www.user-agents.org/allagents.xml"
xml_data = Net::HTTP.get_response(URI.parse(url)).body
data = XmlSimple.xml_in(xml_data)
agents = data['user-agent'].select{|agent| type = agent["Type"].first; type.include?("R") || type.include?("S")}
agent_names = agents.collect {|agent| agent["String"].first}
@jakeonrails
jakeonrails / xml_parser.rb
Created February 11, 2012 04:23 — forked from kmile/xml_parser.rb
A small nokogiri xml reader DSL.
# A small DSL for helping parsing documents using Nokogiri::XML::Reader. The
# XML Reader is a good way to move a cursor through a (large) XML document fast,
# but is not as cumbersome as writing a full SAX document handler. Read about
# it here: http://nokogiri.org/Nokogiri/XML/Reader.html
#
# Just pass the reader in this parser and specificy the nodes that you are interested
# in in a block. You can just parse every node or only look inside certain nodes.
#
# A small example:
#