So tonight's five minute challenge involved dealing with mixed case.
You see like most projects, as I work through improvements and additions to the fubnub idea, I also discover new issues and problems here and there. Tonight I finally got around to thinking about the fact that the templates are going to be input by...wait for it...users. Yuck.
As a developer, I continue to have this love/hate relationship with users. I love them because, well, they take what I build and actually use it (hence the name user)...but I also hate them because they have the gall to have no idea how to use what I built in just the specific way I envision it. The gall I say, the gall!
Anyway, long story short, the burden to make things 'just work' falls to me as the developer.
So getting back to the fact that templates are going to be entered by users, I realized that users might (and probably will) enter template details in all sorts of strange ways...though it would be great, I can't just assume that they'll magically enter the fields to their feeds in the same way that their feeds actually provide them.
For example, the RSS feed for this very blog (which by the way you can subscribe to right here) has a 'link' tag...now in the RSS the tag is in lower case...but in a user input template, it might be something like [' Link '] or [' LINK '] or [' LiNk '] or whatever (you get the point).
And I need to properly tie these things together regardless of how they are input (ie. I need to do what the user means, even if it's not what they actually say).
Luckily this is not a huge problem. First of all, I'm already building out a list of the fields in a template (which I use to do the actual data injection with). So all that I really had to do was loop through the list of fields, and do a replacement that up-cases all the fields. Here's the simple Perl that I'm using right now:
foreach (@$fields) {
$field = $_;
$template_data =~ s/(['s+)$field(s+'])/$1$field$2/gi;
}
Now if you're a salty and savvy developer, you may be thinking that technically that's not all there was to it - and you'd be right. The above code correctly upper cases fields in my user input templates, but that still doesn't mean we'll always match our feeds.
Remember in my example, my RSS feed has a link tag (a lower case 'link') and since we've just upper cased our fields we've actually made sure that we'll NEVER have a match. Yikes.
But never fear, we can fix that problem right quick as well.
We just need to make sure that we convert all tags from feeds to upper case...and since I'm also already grabbing the list of tags from our feeds (which is also used to deal with the data injection bit), it's another simple case of up-casing the values in my array (this time with the Perl uc function).
So in the end, it was a simple solution to a sticky situation. Those crazy users can feel free to get wild with case variations in templates, and things will 'just work' (well at least that part of things).



