Skip to content

Instantly share code, notes, and snippets.

Avatar

David James djsun

View GitHub Profile
View example.rb
# before
case requested_state
when "draft"
state_method = :redraft!
when "published"
state_method = :publish!
end
# after
state_method = case requested_state
View friendly_bundler.rb
require 'rubygems'
begin
require 'bundler'
Bundler.setup
rescue LoadError
puts "Please run `gem install bundler` and `bundle install`"
end
View Gemfile
source "http://rubygems.org"
gem 'rake', '~> 0.8.7'
group :development, :test do
gem 'rspec', '~> 2.0.0.beta.20'
end
View cms-resources.rb
class ResourceGroup
has_many :resource_entries
end
class ResourceEntry
belongs_to :resource, :polymorphic => true
end
# A type of resource
class Image < ActiveRecord::Base
View mongo_leak_finder.rb
require 'rubygems'
require 'mongo'
class MongoLeakFinder
def initialize
db = Mongo::Connection.new.db("cgp")
@coll = db.collection("records")
@coll.create_index("n", :unique => true)
end
View ca-gov.txt
# http://www.ca.gov/CaSearch/Agencies.aspx
# From Firefox Http Live Headers Add-On
POST /CaSearch/Agencies.aspx __EVENTTARGET=ctl00%24leftColumn%24dicLetters%24lnkLetterA_C&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=%2FwEPDwUJMTc5MTE0ODI4DxYEHhNDdXJyZW50U2VhcmNoU3RyaW5nZR4OQ3VycmVudExldHRlcnMFBUEsQixDFgJmD2QWAgIED2QWAmYPZBYEAgEPZBYCAgEPFgIeBWNsYXNzBQZhY3RpdmVkAgkPFgIeBFRleHQF4%2FICPHRhYmxlIGNlbGxwYWRkaW5nPSIwIiBjZWxsc3BhY2luZz0iMCIgaWQ9ImFnZW5jeV9saXN0Ij48dHI%2BPHRkIGNsYXNzPSJhZ2VuY3lfbGV0dGVyIiBjb2xzcGFuPSIyIj5BPC90ZD48L3RyPjx0ciA%2BPHRkPjxhIGhyZWY9Imh0dHA6Ly93d3cuYWltLmNhLmdvdi9Ib21lL2RlZmF1bHQuYXNweCI%2BQWNjZXNzIGZvciBJbmZhbnRzIGFuZCBNb3RoZXJzPC9hPjwvdGQ%2BPHRkIGNsYXNzPSJhZ2VuY3lfc29jaWFsIj48aW1nIHNyYz0iaW1hZ2VzL3NlYXJjaF9hcHAvYndfZW1haWxfaWNvbi5qcGciIGFsdD0iRW1haWwiIC8%2BJm5ic3A7Jm5ic3A7PGltZyBzcmM9ImltYWdlcy9zZWFyY2hfYXBwL2J3X3R3aXR0ZXJfaWNvbi5qcGciIGFsdD0iVHdpdHRlciIgLz4mbmJzcDsmbmJzcDs8aW1nIHNyYz0iaW1hZ2VzL3NlYXJjaF9hcHAvYndfZmFjZWJvb2tfaWNvbi5qcGciIGFsdD0iRmFjZWJvb2siIC8%2BJm5ic3A7Jm5ic3A7PGltZyBzcmM9I
View new_importer_style.rb
class Puller
def initialize(handler)
@handler = handler
end
def run
@handler.organization("DOE")
@handler.organization("NASA")
@handler.source("California Earthquakes")
@handler.source("Hawaii Earthquakes")
View parsing_snippet.rb
# The information is somewhat jumbled together. This method takes
# advantage that headings are wrapped in <b> tags. The result is
# an array of strings, which is easier to parse than one big string
# (which is what we would have gotten if we used `start.content`)
#
# Sample result:
# [
# "Description:",
# "Operating and Maintenance Petitions. When the annual allowable increase does not completely cover the landlord's yearly increase in operating and maintenance expenses for a property, a landlord may petition for an additional base rent increase of up to 7%. This is known as an Operating and Maintenance or O&M increase",
# "Agency Name:",
View each.rb
rows = []
tables.each do |table|
rows.concat(table.css("tr"))
end
rows
View _environment.txt
$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10]
$ gem list | grep json
json (1.2.0)
json_pure (1.2.0)
$ gem list | grep activesupport
activesupport (2.3.5)