software, business, and fun
cambodia

Ghetto-riffic Hacking on Radiant

Project need a screenscraper to pull out the google maps portion of a page and embed on our own.

Problem is Radiant cms likes to take over all your urls, so how to make a custom ruby page that lives within a Radiant powered site?

Radiant by default is a systemwide gem that would prefer you didnt do things like this, and going to the trouble of adding a custom plugin to manage one page was overkill. I don’t remember if there was a static content plugin - I think I found one, but it wasn’t able to run custom Ruby code.

What we needed was really really simple, no edibility or site layout on the page so here’s what I made.

Here we go:

* start by adding the app/controllers and app/views folders.
* add in the controller you want with these specifics:

skip_before_filter :authenticate

up_path = File.join(File.dirname(__FILE__), '..')
render_file up_path + '/views/location/index.rhtml' and return true

The skip filter removes Radiant’s checking for admin, and the render_file explicitly loads the correct template. Radiant is using the system gem path for views, so it’s necessary to run relative to it’s install.

* routes.rb hacking. at the bottom:
builder = ActionController::Routing::RouteBuilder.new
route = builder.build('YOURURL', {:controller => 'NEWCONTROLLER'})
custom_routes = Array.new
custom_routes << route

# put it in first place
ActionController::Routing::Routes.routes = custom_routes + ActionController::Routing::Routes.routes

Pretty cool! The new url as just a controller will exist on the Radiant powered domain, but not barf over Radiant. I tried this with 0.6.4. YMMV

The actual screenscraping was done hpricot/open-uri, and was pretty straightforward.

http://www.igvita.com/blog/2007/02/04/ruby-screen-scraper-in-60-seconds/
http://weblog.jamisbuck.org/2006/10/2/under-the-hood-rails-routing-dsl

The end result was a big hack, but I got in running in about an hour.

0 comments

There are no comments yet...

Kick things off by filling out the form below.

Leave a Comment