Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Pretty swamped with work-work

Brad Greenlee bgreenlee

💭
Pretty swamped with work-work
View GitHub Profile
View snippet.txt
# class to construct conditions for AR queries
# cc = ConditionsConstructor.new('foobar > ?', 7)
# cc.add('blah is not null')
# cc.add('baz in (?)', [1,2,3])
# cc.conditions
# # => ["(foobar > ?) AND (blah is not null) AND (baz in (?))", 7, [1, 2, 3]]
class ConditionsConstructor
def initialize(*args)
add *args unless args.blank?
end
View gist:59103
# base for "smart" constant classes
class OptionSet
def self.[](id)
if id.is_a?(Fixnum)
return local_constants.find {|c| id == const_get(c)}
else
return const_get(id.upcase)
end
end
@bgreenlee
bgreenlee / crypto.rb
Created Feb 26, 2009
Simple Ruby wrapper for encryption/decryption using OpenSSL
View crypto.rb
class Crypto
# encrypts data with the given key. returns a binary data with the
# unhashed random iv in the first 16 bytes
def self.encrypt(data, key)
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
cipher.encrypt
cipher.key = key = Digest::SHA256.digest(key)
random_iv = cipher.random_iv
cipher.iv = Digest::SHA256.digest(random_iv + key)[0..15]
encrypted = cipher.update(data)
@bgreenlee
bgreenlee / report.rb
Created Mar 1, 2009
Displays a mysql-style report for an array of ActiveRecord objects. Stick it in your .irbrc.
View report.rb
# mysql-style output for an array of ActiveRecord objects
#
# Usage:
# report(records) # displays report with all fields
# report(records, :field1, :field2, ...) # displays report with given fields
#
# Example:
# >> report(records, :id, :amount, :created_at)
# +------+-----------+--------------------------------+
# | id | amount | created_at |
View gist:123258
require 'open-uri' ; require 'rss/2.0' ; require 'cgi'; require 'openssl'
delicious_user='mattb'
instapaper_user='me@example.com'
instapaper_password='whatever'
$VERBOSE=nil
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE # lame, but effective; can't verify properly w/ open-uri in Ruby 1.8
RSS::Parser.parse(open("http://feeds.delicious.com/v2/rss/network/#{delicious_user}?count=15").read).items.each { |item| open("https://www.instapaper.com/api/add?username=#{instapaper_user}&password=#{instapaper_password}&url=#{CGI.escape(item.link)}&auto-title=1").read }
View isObjectLiteral.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<!-- Online here: http://ejohn.org/files/bugs/isObjectLiteral/ -->
<title>isObjectLiteral</title>
<style>
li { background: green; } li.FAIL { background: red; }
iframe { display: none; }
</style>
View isObjectLiteral.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<!-- Online here: http://ejohn.org/files/bugs/isObjectLiteral/ -->
<title>isObjectLiteral</title>
<style>
li { background: green; } li.FAIL { background: red; }
iframe { display: none; }
</style>
@bgreenlee
bgreenlee / twitdiff.rb
Created Jul 28, 2009
Script for unfollowing Twitter users who aren't following you, # or following users who are following you...or both
View twitdiff.rb
#!/usr/bin/env ruby
# twitdiff.rb
# Quickie script for unfollowing users who aren't following you,
# or following users who are following you...or both
# Requires Grackle (http://github.com/hayesdavis/grackle/tree/master):
# sudo gem sources -a http://gems.github.com
# sudo gem install hayesdavis-grackle
View snippet.txt
>> ("0".."10").to_a
=> ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
>> ("1".."10").to_a
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
>> ("2".."10").to_a
=> []
>> ("2".."9").to_a
View @
#!/bin/bash
# "@Pad"
# An easy commandline time-stamped log/notepad
# Derived from https://web.archive.org/web/20120118122636/http://blog.rubybestpractices.com/posts/jamesbritt/James_will_be_right_back_after_these_interruptions.html
#
# Usage:
# @ something or other - log the timestamped message "something or other"
# @ . - open the @ scratchpad with a new timestamp and
# no message with your default editor