Inspired by Cory Booker

The closing plenary of the Personal Democracy Forum — an hour long — was a great discussion between Cory Booker, Arianna Huffington, RNC tech chair Saul Anuzis, PDF founder Andrew Rasiej, NYT Times tech writer Nick Bilton and Tim O’Reilly.

Cory Booker was by far the highlight. This man is inspiring.

“The biggest thing we’re going to have to repent for as a generation is not the violent wars and violent actions of the bad people, but the appalling silence and inaction of the good people.”
— Cory Booker, paraphrashing Martin Luther King, Jr.

rake task for extracting database contents

This is the inverse of db:fixtures:load. I modified the version I found to take the FIXTURES environment variable as a parameter. Saved as gist 440058.
# extract_fixtures.rake # by Paul Schreiber <paulschreiber at gmail.com> # 15 June 2010 # # I got this from Peter Gulezian <http://metoca.net/> # Looks like another version is here # <http: //redmine.rubyforge.org/svn/trunk/lib/tasks/extract_fixtures.rake> # # This is the inverse of the built-in rake db:fixtures:load # # Usage: rake db:fixtures:extract # rake db:fixtures:extract FIXTURES=foo # rake db:fixtures:extract FIXTURES=foo,bar # desc ‘Create YAML test fixtures from data in an existing database. Defaults to development database. Set RAILS_ENV to override.’ namespace :db do namespace :fixtures do task :extract => :environment do sql = “SELECT * FROM %s” skip_tables = [“schema_migrations”] ActiveRecord::Base.establish_connection if ENV[“FIXTURES”] tables = ENV[“FIXTURES”].split(“,”) else tables = (ActiveRecord::Base.connection.tables – skip_tables) end tables.each do |table_name| i = “000” File.open(“#{RAILS_ROOT}/test/fixtures/#{table_name}.yml”, ‘w’) do |file| data = ActiveRecord::Base.connection.select_all(sql % table_name) file.write data.inject({}) { |hash, record| hash[“#{table_name}_#{i.succ!}”] = record hash }.to_yaml end end end end end

TIFF file dimensions in Ruby

Using Ruby, I wanted to get the dimensions of image files without using a full-on image library. I found handy code for PNG, BMP, GIF and JPEG and adapted the JPEG code for TIFF. Saved as gist 431893.
# TIFF.rb # by Paul Schreiber <paulschreiber at gmail.com> # 09 June 2010 # # Based on JPEG.rb by Remco van’t Veer and imagesize by Keisuke Minami <keisuke at rccn.com> # http://snippets.dzone.com/posts/show/805 # http://blog.remvee.net/2005/12/02/hoogte_en_breedte_van_een_JPEG (JPEG # http://rubyforge.org/projects/imagesize/ class TIFF attr_reader :width, :height def initialize(file) if file.kind_of? IO examine(file) else File.open(file, ‘rb’) { |io| examine(io) } end end private def def_read_o(io) io.seek(0, 0) # define Singleton-method definition to IO (byte, offset) def io.read_o(length = 1, offset = nil) self.seek(offset, 0) if offset ret = self.read(length) raise “cannot read!!” unless ret ret end io end def examine(io) img_top = io.read(1024) if not (img_top[0, 4] == “MM\x00\x2a” or img_top[0, 4] == “II\x2a\x00”) raise ‘malformed TIFF’ end img_io = def_read_o(io) endian = if (img_io.read_o(4) =~ /II\x2a\x00/o) then ‘v’ else ‘n’ end # ‘v’ little-endian ‘n’ default to big-endian packspec = [ nil, # nothing (shouldn’t happen) ‘C’, # BYTE (8-bit unsigned integer) nil, # ASCII endian, # SHORT (16-bit unsigned integer) endian.upcase, # LONG (32-bit unsigned integer) nil, # RATIONAL ‘c’, # SBYTE (8-bit signed integer) nil, # UNDEFINED endian, # SSHORT (16-bit unsigned integer) endian.upcase, # SLONG (32-bit unsigned integer) ] offset = img_io.read_o(4).unpack(endian.upcase)[0] # Get offset to IFD ifd = img_io.read_o(2, offset) num_dirent = ifd.unpack(endian)[0] # Make it useful offset += 2 num_dirent = offset + (num_dirent * 12); # Calc. maximum offset of IFD ifd = width = height = nil while(width.nil? || height.nil?) ifd = img_io.read_o(12, offset) # Get first directory entry break if (ifd.nil? || (offset > num_dirent)) offset += 12 tag = ifd.unpack(endian)[0] # …and decode its tag type = ifd[2, 2].unpack(endian)[0] # …and the data type # Check the type for sanity. next if (type > packspec.size + 0) || (packspec[type].nil?) if tag == 0x0100 # Decode the value @width = ifd[8, 4].unpack(packspec[type])[0] elsif tag == 0x0101 # Decode the value @height = ifd[8, 4].unpack(packspec[type])[0] end end raise ‘malformed TIFF’ if @width.nil? || @height.nil? end # examine end # class