Category archive: ruby

December 13, 2007

Note to Self 0

This is how to install the Ruby mysql gem with your dumb xampp setup:

sudo gem install mysql -- --with-mysql-dir=/Applications/xampp/xamppfiles/ --with-mysql-include=/usr/local/mysql/include
sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /Applications/xampp/xamppfiles/lib/mysql/libmysqlclient.15.0.0.dylib  /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle
September 13, 2007

So I’ve been using Subversion on a Linux box for a while now to manage my code for various freelance and personal projects. I’ve recently started working with some other people who need access to the server, and I wanted to receive emails when they make a commit. I also didn’t want to deal with svnnotify (um, or I possibly didn’t know about it until it was too late). Luckily Subversion makes it very easy to write a post-commit script to do this. Here’s the ruby script I ended up with:

#!/usr/bin/ruby
require 'rubygems'
require 'action_mailer'
 
class SimpleMailer < ActionMailer::Base
  def simple_message(to, sub, message)			                
    from 'walrus svn <svn@wlrs.net>'					                
    recipients to.join(", ")
    subject sub
    body message
  end
end
 
path = ARGV[0]
revision = ARGV[1]
 
user = `/usr/bin/svnlook author #{path} -r #{revision}`.chomp
log = `/usr/bin/svnlook log #{path} -r #{revision}`.chomp
changed = `/usr/bin/svnlook changed #{path} -r #{revision}`.chomp
diff = `/usr/bin/svnlook diff #{path} -r #{revision}`.chomp
dirs = `/usr/bin/svnlook dirs-changed #{path} -r #{revision}`.chomp
date = `/usr/bin/svnlook date #{path} -r #{revision}`.chomp
 
dir_list = dirs.split("\n").collect do |dir| 
  dir =~ /([^\/]+)/
  $1
end
dir_list.uniq!
 
recipients = ["joey@wlrs.net"]
 
#other people might want these emails
recipients << "team@mycompany.com" if dir_list.include?("mycompany.com")
recipients << "someone.else@oursite.com" if dir_list.include?("oursite.com")
 
subject_log = log.split("\n")[0].slice(0..60)
dir_string = dir_list.join(", ")
subject = "r#{revision} #{user} in #{dir_string}: \"#{subject_log}\""
message = "#{user} committed these changes on #{date}:\n\n#{log}\n\n#{changed}\n\n#{diff}"
ActionMailer::Base.smtp_settings = { :address => 'localhost', :port => 25, :domain => 'localdomain'}
SimpleMailer.deliver_simple_message(recipients, subject, message)

Projects in my repository are setup like this:

svn/oursite.com/trunk
svn/mycompany.com/trunk

That means we just look at the svn directory to see which project is being modified, then we alert the appropriate people. The emails are plain text and include the commit comments, modified files, and a full diff of the commit. What else do you need?

September 24, 2006

So I’ve been doing some work on the long-neglected Fontleech and today found myself removing over .25 million spam comments. That sounds herculean, but it was actually pretty easy.

The WordPress frontend pulls the comment count for each post from the ‘comment_count’ field in the wp_posts table. obviously that doesn’t get updated when you manually delete spam directly from the database, so here’s a simple ruby script that will update that for you (just define your db variables):

#!/usr/bin/ruby
require 'mysql'
 
db = Mysql.real_connect(host, user, pass, dbname)
ids = db.query("SELECT `ID` FROM `wp_posts` WHERE 1")
 
ids.each_hash do |post|
  id = post['ID']
  num = 0
  comments = db.query("SELECT COUNT(1) FROM `wp_comments` WHERE `comment_post_ID`='#{id}' AND `comment_approved`='1';")
  comments.each {|x| num = x[0]}
  up = db.query("UPDATE `wp_posts` SET `comment_count`='#{num}' WHERE `ID`='#{id}';")
end
June 21, 2006

For the past couple months at work I’ve been using Watir, a Ruby library for controlling Internet Explorer, to automate some of the testing on our site. But the Snap site is a beast, with an enormous amount of Javascript and hundreds of dynamically-modified divs.

Since Watir can bog down while iterating through 100+ div objects, I’m working directly with WIN32OLE objects for certain things.

I’m very new to ruby. And WIN32OLE is weird. I had just been copying code from the watir library and modifying it to suit my needs, but today I had to do some more complicated stuff. In ruby, you can view the available methods of any object by doing this:

puts foo.methods

But if you do that with a WIN32OLE object it doesn’t give you jack. I finally managed to find a ruby-talk post from 3 years ago that shed some light on this. So here’s a method that can pull it off:

class WIN32OLE
  def list_ole_methods
    method_names = ole_methods.collect {|m| m.name}
    puts method_names.sort.uniq
  end
end

So doing:

foo.list_ole_methods

Will give you a long list of method names you can call with your object.

I realize this isn’t, uh, interesting, but hopefully the next tard trying to figure this stuff out will be able to find this post on Google.

And just for the record, Watir fucking rules. It easily outperformed every other automation tool we evaluated (many of which cost tens of thousands of dollars) and, not coincidentally, harnesses the power of an awesome language like Ruby. A lot of the other packages we looked at use strange proprietary languages that aren’t really documented (because then they can talk you into taking a weeklong $8k training course) and can’t pull off half the stuff we’ve already done using Watir.