Tuesday, February 03, 2009

Backup Blogger Blog with Ruby Mechanize Script

After writing up a cURL script to download a backup of my Blogger blog in my previous post I started wondering what the best way to do this in Ruby would be. I looked at some Ruby HTTP clients and narrowed my choices down to two: Mechanize and HTTPClient. Both of these tools look pretty good but Mechanize seemed to offer a very simple interface to scripting a web interaction like this while HTTPClient would offer something that was more like my original cURL script. So I decided to write the same code with a Mechanize approach:



I need to beef this up with some error handling obviously - but I think you'll find Mechanize to be pretty slick if you try it.

Sunday, February 01, 2009

Backup Blogger Blog with cURL Script

I read Dave Winer's Where's Your Data? post today which was in response to a blog post by Craig Burton title The State of Blogging Sucks. These articles refer to the problems that may arise when someone else is hosting your data. The problem could be a massive data loss (see Ma.gnolia), a company going out of business (apparently a Userland product?), or just data lock in.

With that in mind I thought about my blog here. This is primarily a dumping ground for small but handy techniques I learn so that if I figure out a way to do something - I can quickly google for it and use it again later. At this point there are enough helpful hints in here that I would hate to lose it. Blogger provides a way to export your blog as XML but it involves logging in and clicking a button - the last thing a programmer wants to have to remember to do on a recurring basis.

So...I cooked up this bash cURL script to backup a Blogger blog. This script demonstrates the basics of using cURL. The first curl command fetches the login page and pulls out a security token using grep and sed. The second curl command logs in, providing the security token, login, and password among other arguments. The next curl request requests the home page so that your blogID can be determined, which will be used in the final request. The final curl request actually requests the backup dumping the result to standard out - you can redirect this to the file of your choice or modify the script to also accept an output file. Here is the script:



In other news the Cardinals almost pulled it off today. This was the first time in a long time where I really wanted one team to win over the other in the Super Bowl. Alas it wasn't meant to be. I've been waiting for the Chiefs to make it for almost 25 years and it doesn't look I'll be rooting for them in the Super Bowl anytime soon.

Saturday, January 31, 2009

Mouse Event Don't fire in IE6 for Absolutely Positioned DIVs with no "Content"

While experimenting with some Google Maps stuff I learned that mouse events won't fire under IE6 for a div that is absolutely positioned and has no "content." Googling turned up this. As the page says for events to fire in IE6 the div needs to have either some content or a background color. This becomes a problem in Google Maps when you want to make your own overlay and you want to catch events by absolutely positioning a div in the mouse events pane. The solution - easy enough, just put a transparent gif inside the div with the same dimensions as the div. The following two divs should demonstrate the problem, however, I'll have to get in front of IE6 before I can verify it...that is the red outlined div will not pop an alert box in response to a mouseover while the green outlined div will.




So the solution with my Google Maps problem was to use this transparent gif technique, which is the same thing you will find Google doing with their GMarkers. They use a transparent gif the same size as the GMarker, positioned above the GMarker in the mouse events pane in order to catch the events for the marker.

Friday, January 09, 2009

Rails Select Filters that Map to In Queries

Example of a Rails select box that performs filtering and is backed by an IN query. First a constant is defined in the controller. It is an array of arrays where the "first" is the text to display in the select and the "last" is an array of the values the first represents.



Then we render that in our page with typical code:



Back in the controller, when the form is submitted, the following code sets up the conditions for a find call:



The only somewhat interesting bit is how the conditions for "Other" are set up with an inject from the valid options defined in the SERVICE_GRADE_CODE_OPTIONS constant.

Wednesday, January 07, 2009

Apache Tip: Encourage Browsers to Open Documents in the Native Application

Jakob Nielsen says that you shouldn't open "non-web documents" within the browser window. Some of our users had the same complaint and were asking for us to prevent pdf, doc, and other file formats from opening inline within the browser window.

The way to get this to happen is to add a "Content-disposition" header with the value of "Attachment". Of course if you are using Rails send_file or send_data methods you can easily add this header. If, however, the files are being served up directly by your web server then you need to find another way to add this header. If you are using Apache 2 the following directives in your conf file should do the trick:

Monday, December 29, 2008

Javascript Event Bubbling with Prototype

Update: After I wrote this post I remembered that change events don't bubble in IE6 for select elements and thus likely don't bubble for check boxes either. After an investigation I found out that no - they don't bubble. So this code won't work correctly for IE 6 which still commands a 20% + share of the market. Please STOP IE6 NOW...and on with the post.

Bubbling is a good thing...and no I'm not talking about the fact that it might help you to locate a turtle. Javascript events bubble from the source element outward. This gives you a couple of important benefits when attaching event handlers to elements:

  • reduced resource usage - one global handler instead of a handler on each element
  • simpler wiring with dynamic content - instead of adding handlers to new elements inserted into the page, the parent element already has the lone handler


The following example shows some code where a div element with an id of "overlay-entries" contains a number of, well, overlay entries in which each overlay entry has an checkbox element that allows that overlay to be shown or hidden. Instead of wiring up a handler for the "change" event for each checkbox we can merely catch the events as they bubble to the containing div with the id "overlay-entries." With prototype Event objects are normalized to make sure that the .element() method returns the element that the event actually originated on. In my situation it is enough to check that the event fired on a checkbox to allow the corresponding event to fire:

Monday, December 22, 2008

Send HTML to Javascript Functions from Rails Partials

Many Javascript libraries, such as ExtJS, allow you to pass HTML to functions. A good example of this is the Ext.Msg.alert call which accepts a a title and a message where the message string can have HTML markup in it to form the content of the alert popup. When this message is a large block of HTML, for example a large disclaimer that you have to stick up in the face of your customer, it becomes very awkward to include this HTML text in the same page that shows the popup. It bloats the page and mixes together what would be better edited by a "subject matter expert." What you really want to do is to put this disclaimer into its own partial but then you have to figure out how to render the string so that newlines and other content don't break the Javascript. How do you do that? Here are two ways that work: