ephekt Programming seems to be my existence

6Feb/100

Facebook Application: Hello Birthday

About a month ago it was my birthday. On that day I was receiving countless e-mails from Facebook notifying me of Happy Birthday messages when I realized many of the messages followed a particular pattern (i.e. "Happy Birthday(!| <name>,| <endearing term>). Don't get me wrong, I enjoy and appreciate all of my birthday wishes. But being an Engineer, my brain finds patterns and attempts to "code up solutions." However, I had not played with the Facebook API yet and had been waiting for a good reason to come around so I took the idea of a Happy Birthday messenger to work and put together a version one of my Hello Birthday application. The application follows a few goals: to be easy to understand, easy to use, and it should just work. With that, here are the criteria I came up with to take a crack at achieving these goals:

  1. Automatically message friends on their birthday
  2. Allow enabling and disabling of such said feature
  3. Use a generic message that more than 50% of users observed currently use when wishing a friend "Happy Birthday"
  4. Manage an exclusion list, containing the friends Hello Birthday should not automatically message
  5. Post onto the friends' wall as if it were the user

With plans to continue development, I have a more robust friend list module going in soon and the ability to set up custom messages for particular friends. Eventually, some form of gift giving may be in order. To bootstrap development I followed a few principles:

  1. Prototype over masterpiece
  2. Design impacts implementation, but implementation makes design possible
  3. Low budget: use open source and free resources

To prototype the project I chose to use Ruby on Rails and SVN. To bridge my rails application and Facebook, I added the Facebooker rails plugin to my application. Facebooker is a pretty neat extension to Rails that provides all kinds of helper methods and an extremely easy to use interface wrapping the Facebook API. It also provides nice view helpers for generating FBML (the Facebook Markup Language) and FBJS (Facebook JavaScript). Developing version one locally saved quite a bit of time by realizing change immediately as opposed to any form of deployment. I then set-up a Facebook application (in Sandbox mode) for development (i.e. hello_birthday_dev) that only I had access to. Facebook offers test developer accounts, so after registering 6 or 7 of these to use with tests I was able to enumerate all of the states that Hello Birthday would need to recognize and transition.

When it came time to deploy to production I needed a production server and Facebook application. I registered the Facebook application (again, in Sandbox mode) for production (i.e. hell0_birthday) and then signed up for hosting with Heroku.com. Heroku is a Ruby (mostly Rails) cloud computing platform as a service. Basically, you get one free "cpu worker" and as you need more they sell scalable features. This, however, is perfect for what I need: a Rails host that allows cron jobs and makes deployment super fast & easy. Sure enough, Heroku makes deploying an application as easy as:

  1. git add .
  2. git commit -a
  3. git push heroku master

A slug of my app is created and deployed to my birthday.heroku.com production server. And to make sure I can monitor errors, I installed the hoptoad error monitoring service. Hoptoad app provides me with web-based access to error reporting and resolution management (organizes errors based on environment; test, production, and or development).

The training wheels were taken off: Sandbox mode disabled on Hello Birthday. I submitted my application to the Facebook directory a week or so ago and it's been smooth sailing since. I currently am servicing a little over 40 people. No complaints have been submitted thus far.

In summary, this is my first Facebook application and I have tried to utilize a lot of the latest technology in the process to illustrate how quickly a scalable solution to an easy problem can be solved. With Heroku & fairly efficient code, I do not foresee a problem handling a potentially high growth rate. I will try to bring back updates to this article if anything goes terribly wrong.

You can check out the Facebook application at http://apps.facebook.com/hello_birthday

25Jul/090

Validating Emails in Ruby on Rails

It's time to validate e-mail addresses and you're sitting in a Ruby on Rails application. Fortunately, there are a few methods to tackle such a task, and a combination of them can yield a pretty nice solution.

The first idea is to use some lengthy regular expressions. But why enumerate/describe in regular expressions what we are looking for when TMail has it built in... Using TMail, it is possible to let our Ruby Net SMTP wrapper class parse the email address and decide if it is correct or not.

The second task is to make up for some of TMail's odd shortcomings: the fact that the text "bob" passes as valid for TMail is alarming, but throwing in some simple regular expression to get past this provides a pretty solid solution. (For the curious, "bob" is a valid e-mail to TMail because you could be sending messages to the local domain.)

First, here is our regular expression for a basic e-mail address...

/^([^@\s'"]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

Next, we create a TMail object with our e-mail address...

tmail = TMail::Address.parse(address.to_s) rescue nil

I am rescuing nil here. You can rescue whatever error message you want, but for example sake I am not so concerned if the TMail fails to parse the e-mail address. You can pass in a multitude of e-mail formats and TMail will do its best to match the RFC standard for e-mail addresses.

You now have access to a TMail object with a flurry of options (TMail & documentation). Let's proceed.

My simply method calls TMail and then follows it with the regular expression match to ensure this e-mail address in question is ready to be used on the web. Here is my final result to a pretty safe-proof (so far, tested on a rather large web site) e-mail handler. This method will return the TMail object.

def validate_email e-mail
  tmail = TMail::Address.parse(address.to_s) rescue nil
  tmail if tmail.address =~ /^([^@\s'"]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
end

And for those of you who want the extra step, I have attached a helper method that takes a TMail object and gives you back the e-mail address in string format fully qualified.

def formatted_email tmail
  if !email.blank?
    friendly_name = (tmail.name.blank? ? "" : tmail.name.blank?).tr('"',"'")
    quote = '"' if friendly_name =~ /[&lt;,@;]/
    "#{quote}#{friendly_name}#{quote} &lt;#{address}&gt;"
  end
end

As always, test this code and do not trust it blindly. I could have fat fingered something...

23Feb/090

Transitioning from Windows XP to Mac OS X

Recently I moved over (from Windows XP) to Mac OS X. Two reasons motivated my move; the first was for better Ruby on Rails environment support (work) and the second was for a new laptop with a little more kick than the previous one (fun).

Do I think this was an “upgrade,” and am I happy with the new operating system compared to Windows XP Professional? The short answer is No. The longer answer is Yes. To be honest, I am so comfortable in a Microsoft Windows that moving to Mac OS X has been painful. Yes, I said it, painful! Simple task shortcuts that I have grown so accustomed to, like Right-click->Create New File… Please do inform me if this exists and I have overlooked it, but I can not seem to find a “Create New File” option in my right click.  Oh yay! I can create a new folder or a burn folder, but no-sir you can not create a new file. Maybe you could tell, or maybe you could not, but this is my biggest pet pieve to my new operating system. While I have not jumped on the band wagon and become one of those bumper-sticker boasting Apple owners, I will say that Apple has done a great job taking a *nix based operating system and skinning it to both look beautiful and function superbly. I am still learning how to use the Mac OS X but from what I have experienced so far I have to say it is a phenomenal operating system and I definitely prefer it over Windows at this point.

To play the role of devils advocate, here are some of the things I have come to love about my new mac:

  1. Two finger scrolling on my touch pad (what was life like before this?)
  2. Awesome hibernation support… I hardly ever shut my computer down
  3. Seamless application install and uninstall (However, I am not sure how good of a job the OS does in the background at actually removing all traces of the program on uninstall)
  4. Dock bar
  5. Some of the Mac OS mini features (Expose, Spaces, Spotlight)

and some of the speed bumps I have felt along the way:

  1. That damned Right-click->Create New File
  2. Cycling through tabs in most programs requires more keyboard presses (In FireFox I have to do option+command+<arrow key>
  3. Auto adjusting back light to the LCD (Some may disagree and tell me I can turn this off… sure, but with it turned on and sitting in an awkwardly lit room you can find yourself staring at a screen that won’t make up its mind about how bright it wants to be)
  4. Changing the size of a window can only be achieved by clicking the bottom right corner

By no means are the lists above qualified to illustrate my top ~5 likes and dislikes, rather they just serve as starting points for what I have come to most prominently notice. Being a heavy computer user these are the things I notice because the speed at which I move to complete these tasks are dependent on the ease of using shortcuts provided by my operating system.

I started this post in November and am finally getting around to hitting the publish button. Eventually I would like to expand this post or create a parallel post that highlights the top short cuts… But Google has so many result sets that include such aforementioned lists that it may never happen. If you have anything to comment in regards to the post or a familiar transition please feel free to comment. I would very much enjoy hearing other peoples opinions on what they found alarming or relieving either to or from Mac OS X (whether it be hardware, software, or socially related).