ruby

jQuery with Rails 3

One of the most talked about features in Rails 3 is its plug & play architecture with various frameworks like Datamapper in place of ActiveRecord for the ORM or jQuery for javascript. However, I've yet to see much info on how to actually do this with the javascript framework.

Fortunately, it looks like a lot of the hard work has already been done. Rails now emits HTML that is compatible with the unobtrusive approach to javascript. Meaning, instead of seeing a delete link like this:

you'll now see it written as

This makes it very easy for a javascript driver to come along, pick out and identify the relevant pieces, and attach the appropriate handlers.

So, enough blabbing. How do you get jQuery working with Rails 3? I'll try to make this short and sweet.

Grab the jQuery driver at http://github.com/rails/jquery-ujs and put it in your javascripts directory. The file is at src/rails.js

Include jQuery (I just use the google hosted version) and the driver in your application layout or view. In HAML it would look something like.

Capistrano Meets Drupal

There are some very trivial administration tasks that I find myself doing over and over again in Drupal which can't currently be done from the admin interface. As of right now, these include installing a theme or module from drupal.org or adding a new site to an existing Drupal install. So, I've built a tiny Capistrano file that allows me to automate these tasks.

The tasks that I've setup are:

cap drupal:install:theme
cap drupal:install:module
cap drupal:install:site

cap drupal:install:theme

All this task requires is a path to the tar file of the theme that you want to install. What I do is browse over to the list of Drupal themes, find one that I want to try out, and copy the location of the "Download" link for the correct theme. In Firefox and Safari its as simple as right clicking the link and selecting "Copy Link Location". Make sure the version of the theme corresponds to the version of Drupal you have installed.

Once you've got the location to your theme copied, run the task with cap drupal:install:theme. It will ask you for the path to the theme location which you can then paste in. That's it. You should be able to browse to admin/build/themes and see your newly installed theme.

Spiders with Ruby

IBM recently published an article on creating spiders with Linux. I've always had a fascination with how this was done and knew it couldn't be that difficult. Reading this article does, however, make you appreciate the logic that has gone into the big Search Engines' spiders.

After a uniquely timed request to scrape some data from the web, it was like God was telling me, "Now is the time to learn web scraping and spidering." So, I jumped at the opportunity.

Fortunately, the first few examples in the article used Ruby. The last one used Python because of its built in HTML Parsing library. I was able to find an HTML parsing gem for Ruby called hpricot which I found to work very well.

Unfortunately, the request that I had to do this came about from a confidential project - so I can't share my code at this point. Nonetheless, it was quite a fun little project that included using ActiveRecord from Rails to store my scraped data in a MySQL database.

Check out the article and have fun programming. I know I did. Oh yeah, I looked at MySpace's robots.txt file and it only specified limits for a user agent that I couldn't recognize. I guess I should do a little digging to see if my scraper violates any legal terms of service.