The Hype
Rack is taking the Ruby and Rails worlds by storm. It’s hard to find any recent Ruby related posts that don’t mention Rack in some way or other. It’s obvious that Rack is here. If you haven’t read up on it. Jason Siefer has put together a good post full of links to get you started.
The Project
I recently killed Rubyyot.com, the GTD task management site. It turns out that using computers to take notes just isn’t convenient. I haven’t quite figured out what is, but I suspect that it involves pencil and paper. In any case, I decided to take the site down since I wasn’t using it. The site had a pretty “this domain is parked” page on it, which I wasn’t particularly fond of.
So to make the site more interesting, I wanted to be able to display an news items that I might have available about the domain. However, I already have a blog, this one here. I didn’t want to write a full rails app to post blog entries, or have yet another wordpress site. What I wanted to be able to do was post to my blog and if the item had to do with the rubyyot domain have it show up in the page I have there as well.
What I needed was a way to take an rss feed from my blog for the tag rubyyot and post the items to a page on Rubyyot.com. That is exactly what I did in a few hours with some help from Rack.
The Host
Rubyyot.com is hosted on Dreamhost which supports Passenger. And here is one of the nice things about Rack. Rack is a standard interface and so Passenger supports Rack. All Passenger needs to run a Rack app is a rackup file (config.ru) a public directory for css, images and other files available to the public. Finally, it needs a file to trigger a Passenger restart, this is located at tmp/restart.txt.
It turns out that since Rails now runs on Rack, you can simply configure your app in the Dreamhost Webpanel as you would a Rails app. Instead on putting the Rails app at this location, you simply put the Rack app there. In my case I put a git repository there, though I may try to capify it in the future.
The Experience
All in all, it was a painless experience. The code needs a little cleanup but it works. Dreamhost had Rack and Hpricot already installed on the server, not the current versions of the gems, but this didn’t cause an issue. I wanted to put it out here to show how easy it can be. In the future, I would like to use a similar technique to be able to post status or news messages to any of my sites from my blog.
The Code
Note: The full code can be found on github tagged as first_post
#config.ru require 'rack' require 'rubyyot_status' run RubyyotStatus.new
and
#rubyyot_status.rb class RubyyotStatus def call(env) require 'rubygems' require 'hpricot' require 'open-uri' # Setup beginning of page and initial status value text = "<h2>Rubyyot.com is going through changes</h2>" text << "<p>If you are looking for my blog you can find it <a href='http://blog.rubyyot.com'>here</a><p>" text << "<h3>Latest Updates</h3><ul>" css = '<link rel="stylesheet" type="text/css" href="/rubyyot.css" />' response_body = "<html><head><title>Rubyyot</title></head>#{css}<body>#{text}" status = "200" begin # read rss feed doc = Hpricot.XML(open("http://blog.rubyyot.com/tag/rubyyot/feed/rss")) (doc/"item").each do |item| # get link, description and title from feed link = (item/"link").inner_html description = (item/"description").inner_html title = (item/"title").inner_html # add each post to page response_body << "<li><h4><a href='#{link}'>#{title}<a></h4>#{description}</li>" end rescue status = "500" end # finish off page response_body << "</ul></body></html>" # add headers headers = {} headers["Content-Length"] = response_body.length.to_s headers["Content-Type"] = "text/html" # return values [status, headers, response_body] end end
See it in action
Working version (at least for now) is at Rubyyot.com.
[...] Blog | Rubyyot » Blog Archive » Using Rack to generate content from an rss feed [...]
[...] the past week I started playing with Rack and put together a little Rack application that generates content from my blog’s rss feed about the site. As I stated in that post I really didn’t like having the “parked” page up [...]
у вас окупается блог? я знаю что выгоднее различные партнерки не поссылкам
привет ! ничё беспричинный у тебя блог )) почитаешь мой? я любитель акуры Коли сколько заходи и почитай, бывает пишу информацию , которой не покажу в книгах и чём то подобном
Cool site, love the info.