Skip to content

Instantly share code, notes, and snippets.

View heycarsten's full-sized avatar

Carsten Nielsen heycarsten

View GitHub Profile
class Model < ActiveRecord::Base
STATES = %w[ inactive active processing ]
validates_inclusion_of :state, :in => STATES
end
li { color: #f00; }
li span { color: #0f0; }
/* Like a Christmas tree! */
# This is just a naive tree implementation, I couldn't find anything on Google
# that was instantly helpful, so I figured it out on my own. Turns out it was
# not too bad.
class Node
def initialize(name = nil, &block)
@name = name
@children = []
yield self if block_given?
# Now all I have to do is make it work :-/
scraper do |doc|
doc.css('.user-details', :on_missing => :halt) do |n|
n.css('.name') do |nn|
nn.emit do |content|
first, last = *content.split
{ :first_name => first, :last_name => last }
end
end
# Some more pondering for my ScrapeKit normalizer builder. I keep throwing
# different use-cases at it which keeps causing me to change my design. I think
# I am starting to get to a happy place.
normalizer do |doc|
doc.collect :inventories, 'table[width="66%"] tr', :exclude => [0, -1, 'UNKNOWN QUANTITY'], :missing => :halt do |tr|
tr.all 'td[width="25%"]', :include => [1..3] do |(name, _, quantity)|
name.select(:name, :format => [:titlecase])
quantity.select(:quantity, :format => [:to_i])
end
require 'rubygems'
require 'benchmark'
require 'amatch'
def levenshtein(a, b)
return b.length if a.empty?
return a.length if b.empty?
asub = a[1..-1]
bsub = b[1..-1]
offset = (a[0] == b[0] ? 0 : 1)
module LCBO
class SearchSuggest
STOP_WORDS = %w[ woods ]
require 'amatch'
require 'stringex'
def initialize(db)
@db = db
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
<title>LCBO API Examples</title>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js' type='text/javascript'></script>
<script type='text/javascript'>
$(document).ready(function() {
$('form').submit(function(event) {
event.preventDefault();
+-----------------------------------------------------------+
| |
| Kieran Huggins r e f a c t o r y |
| Important Businessman |
| 123 Fake St. M4R 2G4 |
| 1 (416) 943-4483 |
| |
| |
| Surf to us on the information super-highway! |
| HTTP://REFACTORY.ON.CA |
@heycarsten
heycarsten / unit_test_report_thing.rb
Created June 3, 2010 04:47
Just a quickie I did to tell me what my (n) slowest and fastest tests are. Uses output that you can get when you pass TESTOPT="-v" to Rake::TestTask
require 'rainbow'
@tests = File.readlines('test_unit.out').
map { |line| line.strip }.
map { |line| line.sub(/ s: (\.|F)\Z/, '') }.
map { |line| line.split(' : ') }.
map { |test| { :name => test.first, :time => test.last.to_f } }
def report(name, color, limit = 10, &filter)
puts "\n#{limit} #{name} tests:".bright.foreground(color)