some sort of pun on ruby, java, and gluing goes here. i got nothing. 0

Posted by anton
on Thursday, June 21, 2007

speaking of gluing things, below is a jruby script i cobbled together to get a backup of an archaic snipsnap instance.

as you might have guessed, it was just an excuse to play with ActiveRecord-JDBC, since all it really takes is just connecting to the database and pulling one table out.

still, it was fun and just a few lines of code, although you had to install ActiveRecord gem as well as ActiveRecord-JDBC gem (not to mention adding mysql jdbc driver in the classpath). as an excuse, i did not want to deal with low-level jdbc machinery, nor did i want to install another gem to get ruby's mysql connectivity.

although it takes an ungodly amount of time to startup, it works just fine. here's the best of both worlds - java's jdbc type4 driver prowess and ruby's terse and readable way of expressing yourself (plus the quick feedback of edit-run-swear-edit cycle):

#!jruby
require 'rubygems'
gem 'ActiveRecord-JDBC'
require 'jdbc_adapter'
require 'active_record'

require 'FileUtils'

ActiveRecord::Base.establish_connection(
          :adapter  => 'jdbc',
          :driver   => 'com.mysql.jdbc.Driver',
          :url      => 'jdbc:mysql://host/snipsnap',
          :username => 'username',
          :password => 'password'
)

class Wiki < ActiveRecord::Base
  set_table_name 'Snip'
end

pages = Wiki.find(:all, :order => 'name')

index = '<html><body><ul>'

pages.each do |p|
  name = p.name.gsub(/:/, '')
  FileUtils.mkpath name if !File.exist? name
  open(File.join(name, 'index.txt'), 'w') { |f| f.puts p.content }

  index << '<li><a href="' << name << '/index.txt">' << name << '</a></li>'
end

index << '</ul></body></html>'

open('index.html', 'w') { |f| f.puts index }

snipsnap does boast xml-rpc support, but it only provides a meager pingback ability.

Comments

Leave a response

Comment