Skip to content

Instantly share code, notes, and snippets.

View samullen's full-sized avatar

Samuel Mullen samullen

View GitHub Profile
@samullen
samullen / encoding.rb
Created August 19, 2010 18:39
Dumbed down module to show eas encoding/decoding
module Encoding
def aes_encode(string, passphrase)
c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
c.encrypt
c.key = key = Digest::SHA1.hexdigest(passphrase)
e = c.update(string)
e << c.final
end
def aes_decode(string, passphrase)
@samullen
samullen / cidr_validation.rb
Created August 20, 2010 19:37
Testing IP's against CIDR. Whitelisting blocks.
#!/usr/bin/ruby
require 'rubygems'
require 'ipaddr'
cidr = IPAddr.new("172.20.0.0")
# cidr = IPAddr.new("198.6.5.0")
mask = 16
# ip = IPAddr.new("198.6.5.22")
ip = IPAddr.new("172.20.155.101")
@samullen
samullen / setup.sh
Created September 27, 2010 23:57
setting up my preferred terminal workspace layout
rxvt -geometry 80x65-0+0 &
rxvt -geometry 80x65+600+0 &
rxvt -geometry 80x65+100+0 &
rxvt -geometry 180x24+0-25 &
rxvt -geometry 180x24+0+195 &
rxvt -geometry 80x39+0+0 &
@samullen
samullen / buildout.rake
Created September 29, 2010 13:05
Rails to Static site generation and deployment
namespace :buildout do
#----------------------------------------------------------------------------#
desc "Mirror the dynamic site into Rails.env/out"
task :mirror do
outdir = File.join(Rails.root, "out")
Dir.mkdir(outdir) unless (File.exist?(outdir) && File.directory?(outdir))
Dir.chdir(outdir) do
`wget -m -nH http://localhost:3000`
end
@samullen
samullen / ip2int.rb
Created October 11, 2010 20:44
IP to Integer and back
# IP to Integer
ip = IPAddr.new('255.255.255.255')
puts ip.to_i
# Integer to IP
ipnum = 4294967295
ip = IPAddr.new(ipnum, Socket::AF_INET).to_s
# If you store the ipnum in MySQL, make sure to use unsigned into for the field.
@samullen
samullen / timeperiod_to_seconds.rb
Created December 9, 2010 23:24
Calculates seconds to more english friendly time periods (1 hour, 2 days, etc.)
def calculate_seconds(period)
if period.match(/(\d+).+?(second|minute|hour|day|week|month|year)s?/)
$1.to_i.send($2)
end
end
@samullen
samullen / gist_seo_test.rb
Created February 5, 2011 04:26
I want to know if Google can read gists embedded in sites. This block of code will hopefully help me determine that
# Ruby class named after my company. Why not get the possible SEO?
class Phalanx
def foo
puts "Fooman was here"
end
# Since I already have the Google alert set up...
def cofounder
"Samuel Mullen"
end
@samullen
samullen / finders.rb
Created December 26, 2011 16:44
User defined finders for Capybara
Capybara.add_selector(:link) do
xpath {|rel| ".//a[contains(@rel, '#{rel}')]"}
end
@samullen
samullen / absence_validator.rb
Created December 27, 2011 20:09
Validator class for validating the absence of value in a field if other conditions are met.
class AbsenceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return unless value
if options.empty? # don't know why you would want this
record.errors[attribute] << "must be blank"
elsif options[:all] && options[:all].all? {|sym| record.read_attribute(sym)}
record.errors[attribute] << "must be blank if the following attributes are defined: #{options[:all].join(', ')}"
elsif options[:any] && options[:any].any? {|sym| record.read_attribute(sym)}
record.errors[attribute] << "must be blank if any of the following attributes are defined: #{options[:any].join(", ")}"
@samullen
samullen / timeframe.rb
Created August 22, 2013 20:06
Wanting opinions on the each method. Am I doing this right?
class Timeframe
include Enumerable
attr_accessor :start_time, :end_time
ONEDAY = 24 * 60 * 60
def initialize(start_time, end_time)
@start_time = start_time
@end_time = end_time