Skip to content

Instantly share code, notes, and snippets.

View stevegraham's full-sized avatar
🛠️
Building @tellerhq

Stevie Graham stevegraham

🛠️
Building @tellerhq
View GitHub Profile
class AppConfig
# Loads a YAML configuration file from RAILS_ROOT/config/. The default file
# it looks for is 'application.yml', although if this doesn't match your
# application, you can pass in an alternative value as an argument
# to AppConfig.load.
# After the file has been loaded, any inline ERB is evaluated and unserialized
# into a hash. For each key-value pair in the hash, class getter and setter methods
# are defined i.e., AppConfig.key => "value"
# This allows you to store your application configuration information e.g., API keys and
# authentication credentials in a convenient manner, external to your application source
class Ability
include CanCan::Ability
def initialize(user)
return if user.nil?
# Collect rights from roles
authorizations = HashWithIndifferentAccess.new []
user.roles.each do |role|
role.resource_rights.each { |resource,actions| authorizations[resource] |= actions }
# Run this file in the same directory as weather.dat
class NOAAParser
def initialize(file)
# Reject all lines in file that don't begin with whitespace and integer
@lines = IO.readlines(file).select { |line| line =~ /^\s+\d/ }
end
def day_with_lowest_temp_spread
# Maybe this regexp could be cleaner?
[NilClass, FalseClass, TrueClass, Symbol, Numeric, Class, Module].each { |klass| klass.class_eval { def duplicable?() false end } }
# Or even…
[NilClass, FalseClass, TrueClass, Symbol, Numeric, Class, Module].each do |klass|
klass.class_eval { def duplicable?() false end }
end
require "rubygems"
require "active_record"
require 'mysqlplus'
Mysql.class_eval { alias :query :async_query }
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:username => "root",
:database => "test",
%w(rubygems eventmachine em-http-request yajl).each { |lib| require lib }
module Twitter
def on_tweet() lambda { |obj| puts obj['text'] }; end
extend self
end
EM::run do
req = EM::HttpRequest.new('http://stream.twitter.com/1/statuses/sample.json').get :head => { 'authorization' => ['eventmachine', 'eventm4chine'] }
parser = Yajl::Parser.new.tap { |p| p.on_parse_complete = Twitter.on_tweet }
run lambda { |env| [302, {'Content-Type'=>'text/plain', 'location' => 'http://blog.shrewple.com'}, Array.new] }
def method_missing(meth, *args, &blk)
if value.include? meth
class << self; self; end.instance_eval { define_method(meth) { value[meth] } }
send meth
else
super
end
end
while self[:slug].nil? && Referral.exists? :slug => self[:slug]
self[:slug] = ActiveSupport::SecureRandom.hex 6
end
require 'open-uri'
def r b; b*38 if URI('http://roulette.engineyard.com/').read[/13/] end