Friday, March 21, 2008

Rails: Session Cleaning code for removing sessions older than a given number of hours

I needed to figure out how to wipe the session table clean of all sessions older than a given number of hours.

num = num_hours * 3600

sql = ActiveRecord::Base.connection();
sql.begin_db_transaction
sql.execute("DELETE FROM sessions WHERE now() - updated_at > #{num}");
sql.commit_db_transaction

Hash to Class

I ran into this looking for Ruby Charts. This is a brilliant bit of code to convert a Hash into a class object. Essentially it converts the keys into members containing the hash values. It also converts the object back into a hash.

http://pullmonkey.com/2008/1/6/convert-a-ruby-hash-into-a-class-object

Thanks to Charlie at Pull Monkey

XML Charts

So the requirements are simple I need a good free XML charting mechanism to show stock data for my site. I'm partial to choosing Flash based over Javascript just because I think Javascript is a bit clunkier.

I got the these requirements while looking for a Ruby graphing library. But all the available ones work at too basic a level or are too new. I initially started looking at gruff. But these are too low level as far as drawing is concerned. Since Rails has such a simple XML interface I figured expanding to XML would increase my ability to find good options.

So far the top contenders are:

  • XML/SWF - this one is a clear favorite because it seems to have the cleanest look and provides good support. There is a ruby interface available

  • Open Flash Charts - this one is totally free but seems to lag behind XML/SWF a bit. Would be the top contender though it it supported the ability to show both price, volume and something else simultaneously

  • FusionCharts - the free version of this is clunky and the commercial one too expensive


There is a Java option discussed in conjunction with rails here but I think the flash packages are better contained and simpler to work with for simple projects.