Skip to content

Instantly share code, notes, and snippets.

View bgreenlee's full-sized avatar

Brad Greenlee bgreenlee

View GitHub Profile
# 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
# 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 February 26, 2009 06:01
Simple Ruby wrapper for encryption/decryption using OpenSSL
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 March 1, 2009 06:01
Displays a mysql-style report for an array of ActiveRecord objects. Stick it in your .irbrc.
# 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 |
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 }
<!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>
<!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 July 28, 2009 23:23
Script for unfollowing Twitter users who aren't following you, # or following users who are following you...or both
#!/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
>> ("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
@bgreenlee
bgreenlee / @
Last active July 21, 2016 14:37
#!/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