Skip to content

Instantly share code, notes, and snippets.

View elliotcm's full-sized avatar

Elliot Crosby-McCullough elliotcm

View GitHub Profile
def read_only_collection(section_name)
get "/#{section_name}" do
haml "#{section_name}/index".to_sym
end
get "/#{section_name}/:object_name" do
begin
haml "#{section_name}/#{params[:object_name]}".to_sym
rescue Errno::ENOENT
throw :halt, [404, "Not found"]
@elliotcm
elliotcm / littlejohn.rb
Created December 28, 2010 22:14
Scrape the Daily Mail website for Richard Littlejohn headlines.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
class Archive
def initialize(starting_url)
@pages = [first_page = Page.new(starting_url)]
add_links_from(first_page)
end
require 'fixnum'
describe Fixnum, 'when converting to Roman numerals' do
context "up to 5" do
it "represents units with I" do
1.to_roman_numerals.should == 'I'
2.to_roman_numerals.should == 'II'
3.to_roman_numerals.should == 'III'
end
class Fixnum
def to_roman_numerals
tens, remainder = self.divmod(10)
fives, units = remainder.divmod(5)
('X'*tens) + ('V'*fives) + ('I'*units)
end
end
@elliotcm
elliotcm / imdb_rank.rb
Created March 6, 2011 19:50
Get the top 10 films in your Movies folder ranked by IMDB rating. It ain't pretty, but it works.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
class IMDBRank
def self.rank(directory, number_to_return=10)
Dir["#{File.expand_path(directory)}/*.{avi,mp4,m4v}"].sort.map do |filepath|
File.basename(filepath) =~ /(.*?)(?:\s+.(\d{4}).)?\....$/
new($1, $2)
end.reject { |film| film.score.nil? }.
@elliotcm
elliotcm / application.rb
Created September 4, 2011 17:32
Insert this into your application.rb Application class to get spans instead of divs in form errors. Rails 3.1.0 version.
# Fuck you and your fucking divs
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
include ActionView::Helpers::OutputSafetyHelper
raw %(<span class="field-with-errors">#{html_tag}</span>)
end
@elliotcm
elliotcm / fetch_bugle.rb
Created December 24, 2011 12:08
A script for archiving all of The Bugle from the Times's hosting.
require 'open-uri'
open('http://podcast.timesonline.co.uk/rss/thebuglemp3.rss').each_line do |line|
next unless line =~ /(http.*?\.mp3)/
system "wget #{$1}"
end
@elliotcm
elliotcm / application_helper.rb
Created January 17, 2012 10:03
The latest weapon in my war against floats.
module ApplicationHelper
def without_spaces(&block)
capture(&block).gsub(/>\s+</, '><').html_safe
end
end
@elliotcm
elliotcm / gist:2043168
Created March 15, 2012 09:29
Example for preventing double-enhancing of elements
var addressButtons = $('button').filter('.address:not(.enhanced)').addClass('enhanced');
// Do some things with addressButtons
@elliotcm
elliotcm / gist:2215878
Created March 27, 2012 13:30
Responsive test bookmarklet
javascript:document.write('%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3Cmeta%20charset=%22utf-8%22%3E%3Ctitle%3EResponsive%20Design%20Testing%3C/title%3E%3Cstyle%3Ebody%20%7B%20margin:%2020px;%20font-family:%20sans-serif;%20overflow-x:%20scroll;%20%7D.wrapper%20%7B%20width:%206000px;%20%7D.frame%20%7B%20float:%20left;%20%7Dh2%20%7B%20margin:%200%200%205px%200;%20%7Diframe%20%7B%20margin:%200%2020px%2020px%200;%20border:%201px%20solid%20%23666;%20%7D%3C/style%3E%3C/head%3E%3Cbody%3E%3Cdiv%20class=%22wrapper%22%3E%3Cdiv%20class=%22frame%22%3E%3Ch2%3E240%3Cspan%3E%20x%20320%3C/span%3E%20%3Csmall%3E(mobile)%3C/small%3E%3C/h2%3E%3Ciframe%20src=%22'%20+%20window.location%20+%20'%22%20sandbox=%22allow-same-origin%20allow-forms%22%20seamless%20width=%22240%22%20height=%22320%22%3E%3C/iframe%3E%3C/div%3E%3Cdiv%20class=%22frame%22%3E%3Ch2%3E320%3Cspan%3E%20x%20480%3C/span%3E%20%3Csmall%3E(mobile)%3C/small%3E%3C/h2%3E%3Ciframe%20src=%22'%20+%20window.location%20+%20'%22%20sandbox=%22allow-same-origin%20allow-forms%22%20s