May 5, 2009

rss/atom feed parsing/generating

I'm working on a little personal webapp that deals with RSS and ATOM feeds and thought I'd share my decision for the library I will be using.

First, let's talk about a couple main requirements.

I need to be able to parse both feed types and store them in an agnostic state (this is so I can allow for the possibility of merging two different feed types).

I'll be dealing with a lot of feeds, so naturally, performance needs to be there. This includes support for some type of caching or intelligent updater.

So, really I wanted a library that:

- abstracted RSS 2.0/ATOM 1.0 details (a bonus if it did other versions)
- was reasonably fast
- had some type of caching or intelligent updater
- parsing was robust and liberal; generation was conservative
- well documented

Initially, I looked around for ruby libraries and found some decent stuff.

feedtools was the first gem I looked at and it did faily well. It abstracted RSS & ATOM and had caching. Unfortuntely, it uses REXML which I hear is up to 60 times slower than libxml these days. And unfortunately, the project isn't being maintained anymore. It was passed onto someone else, but he seems to have left it also and started rAtom.
I also read good things about feedzirra. It boasts blazing performance which is always cool but ended up finding another solution before trying it.

It was after feedtools failure that I decided to expand my search beyond just ruby. I came across some candidates in python and php, but the one that has caught my eye and seems to pass preliminary proofs of concept is Java's Rome.

I was pretty sure that school would be the last time I used java, but turns out I was wrong. And happily wrong.

Rome is mature. Very mature. It supports the earliest versions of RSS and ATOM, in a beautiful abstract way; performance-wise it's faster than REXML (not saying much); has an intelligent updater module; seems robust from preliminary tests but investigating further; and lastly, those java folks like their documentation. It is kinda bland to read, but whatever. Extra bonus - it has recently hit 1.0 (March 2009), which means it is still under active development and has a community around it.

I actually thought this would be a good opportunity to start trying out Jruby on Rails but I'm have a few kinks w/ rails 2.3.2 and my jruby 1.1.6. I think I need to upgrade but after looking at the potential headaches in deployment, I think I will leave this battle for another day. Right now, I just want to get moving on my project.

So, hopefully Rome turns out to be as good as I think it is. Interested to hear thoughts/comments!

April 26, 2009

super cheap VPS hosting

I just read this post on hacker news and there are a couple links on some suspiciously low hosting services.

Check out:
http://www.lowendbox.com/

I think this is the recommended one:
http://www.prgmr.com/xen/

If anyone tries it, let me know how it goes please.

code highlighting with javascript

I didn't think I'd write two posts in one day, but after trying for an hour to get this to work, I thought I better put this up.

If you want to write code in your blog post and have it nicely colored there are javascript libraries that will do this for you. I've chosen Alex Gorbatchev's Syntax Highlighter.

Here is how to get it to work.

In the <head> element of your HTML, you need to add this:
<link href="http://alexgorbatchev.com/pub/sh/1.5.1/styles/SyntaxHighlighter.css" rel="stylesheet" type="text/css">


For Blogger ppl, this means going to Layout, then Edit HTML, then inserting the above right before the tag.

Then before the </body> tag, insert:
<script language="javascript" src="http://alexgorbatchev.com/pub/sh/1.5.1/scripts/shCore.js">
<script language="'javascript'" src="'http://alexgorbatchev.com/pub/sh/1.5.1/scripts/shBrushRuby.js'/">
<script language="'javascript'">
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

This works for Ruby code but can be adjusted to add other languages. The different language scripts can be found here.

Also, there are different CSS styles that you can hook up and they are found here.

I basically followed this blog and used Alex's hosting.

Clearing the file upload field

I recently needed to add file upload functionality to a rails app and discovered that HTML doesn't let you clear the file upload field after you have selected a file (in the case where you suddenly changed your mind and decided not to upload anything).

So after searching for a while I found an answer and it isn't pretty. You have to pretty much replace the file upload field with a new one using javascript.

Here is a basic example using jQuery:

In the view:
<%= f.file_field :attachment %>
<%= link_to_function 'clear', clear_js(id, name) %>
where id and name should be the same as your file field. Right now, I'm trying to figure out a way of pulling this info out cleanly from the form builder but not having much progress.

In a helper:
# creates an equivalent file field and replaces the current one
def clear_js(id, name)
%Q[
var newButton = document.createElement("input");
$(newButton).attr("type","file");
$(newButton).attr("id", "#{id}");
$(newButton).attr("name", "#{name}");
$(newButton).attr("size", "30");
$("##{id}").replaceWith(newButton);
]
end
FYI, I read that you can't just alter the value of the field with something like $('input').value = ' ', and for a good reason - then someone could theoretically write a script to set the value to any file on your computer.