XSDs are inflexible and redundant

I recently had to do some work supplying an XML feed for a client. I used Ruby’s built-in .to_xml on the array of ActiveRecord objects and it generated this XML which I was happy with:

      <date type="date">2012-04-17</date>
      <certificate-number nil="true"></certificate-number>
      <certified-at type="datetime">2012-04-18T07:23:00Z</certified-at>

Nice and simple.

Unfortunately, the organisation that was consuming this XML wanted an XSD. And provided one. But it required the namespace xsi for nil values (replacing the nil="true" above with xsi:nil="true". I thought this would be an easy change…

It is not possible to provide settings to Rails’ to_xml to include the namespace attribute xmlns:xsi="http://..." into the <vehicles> tag so that it could be used wherever there were nil values.

It seem it is impossible to get an XSD to support the XML as supplied.

As a result I had to manually build the xml using builder. Now sure, there’s nothing particularly complex about this XML, but now I needed to write xml.builder views and write my own xml tag helper to identify nil/blank values and put in the appropriate attribute:

  def nil_tag(tag, value)
    if value.nil? || value.blank?
      @xml.tag!(tag, value, 'xsi:nil' => true)
      @xml.tag!(tag, value)

I also then had to format my DateTime output as .xmlschema as the default to_s is not valid in XML. Added an extra day of work all because the XSD couldn’t describe the XML I had.

I should have insisted on JSON.

Code smells: a = a

In a project I’m working on at the moment, I saw some rather unusual code at the top of a number of views – it looked like it was trying to set a default value in the event the variable wasn’t defined, for example:

name = undefined_to_false(name)

and the definition of undefined_to_false:

def undefined_to_false(variable)
  if !defined?(variable)
    return false
    return variable

My first reaction to that method was that it couldn’t work – if I have an undefined variable and attempt to pass it into the undefined_to_false method, I will get a “NameError: undefined local variable or method” before ruby attempts to pass it into that method. The only was around this was to pass in a Proc.

I pulled up irb and confirmed that this method would not work, yet it was being used in a production system. Rcov confirmed that the return false never got called in any tests. Given this information I removed the method and every reference to it – if any of the callers of this method actually needed this method, I figured we would have already had NameErrors.

Then someone complained that their view wasn’t working anymore, and I started to wonder if there was some other magic that made this work. I had confirmed that passing an undefined variable into a method cannot work which meant that the variable must have been defined before it called that method.

A colleague confirmed that

name = undefined_to_false(name)

did in fact work when name was previously undefined (and it was undefined in a number of views). Then it occurred to me:

a = a

In an assignment, the variable on the left hand side will become defined prior to evaluation of the right hand side. Thus, even if a was not defined prior to that line, it will be by the time of evaluation and therefore will not generate a NameError.

So I replaced all the old occurrences of a = undefined_to_false(a) with just a ||= false, although to be more correct I could have replaced them all with a = a but that just looks silly – and smells bad.

Godaddy deleted my emails

Seems Godaddy implemented a new feature on their email hosting: “Auto-purge”.  As the name suggests it will automatically delete emails.  Now that’s fine for folder like Spam and Deleted Items but they put it on my Sent Items!  Goodbye 7 years of email history…

As a loyal *paying* customer for 7 years, who has recommended them many times, I’m pissed off.  After a few emails, they restored 30 days worth of stuff that I had already deleted (so I just had to delete them again) but not my Sent Items.  Still waiting to hear if they’re going to do anything…

Looked at migrating to Google Apps.  According to Godaddy though, I have to pay them MORE money to upgrade to an IMAP plan so that I can get my emails off them…

Facebook and Rails

Ok, so I thought of developing a Facebook application using Rails and kind of assumed that there’d be some fantastic library that just did all the hard work leaving me to write the app.

It seems there are a number of attempts to develop a ruby library for Facebook but finding one that is still active and useful wasn’t easy.

A quick roundup of what I found (mostly via google).

Facebooker – This seems to be the one that is recommended and there’s a book on Peepcode although it’s been said that it expects your entire app to be a facebook app.

RFacebook – The author says “I may cease development on RFacebook in favor of Facebooker”

Giant Robots useful article (one of the most recent I found!)

Facebook on Rails – The author (Hoan) has discontinued support for this plugin.

Facetime – for adding to an existing system with a User model

SocialonRails – Nothing at all at their repository: http://code.google.com/p/socialonrails/

After some attempts at using these, I’m going to proceed down the path of using Facebooker.  The docs indicate that there is an “excellent” tutorial at http://apps.facebook.com/facebooker_tutorial but it is out of date to say the least.  So perhaps I’ll write one while I’m developing an app.