Alex Pooley's Blog

Hello there, my name is Alex Pooley and I'm a freelance web developer residing in Perth, Western Australia. My passion is in the development of web sites that solve everyday problems. Here's a gallery of some of my notable work. If you need a web site designer or developer, contact me with further details. Lastly, you can read more about me.

Ruby Browser Hooks & Facebook Integration Testing

May 18th, 2008

Why don't you subscribe to my blog while you're here? I'm a freelance web developer and I blog about Ruby, Rails, and business online.

Go ahead and subscribe to my RSS feed. Thanks for visiting!

Recently I had to write some integration tests to ensure that my Facebook application was communicating with Facebook correctly. I started out trying to use plain old Net::HTTP which was painful because Facebook appears to actively block programmatic access through their login page. Just as I was about to give up I discovered a suite of libraries that allow you to access your browser from Ruby! The library is fantastic as I can very simply control the forms and content in my browser, straight from my Ruby code. Nice!

The library is called Watir. Watir is a tool to hook in Ruby with IE, but there are ports for Safari and Firefox. I ended up going with the Safari port as the Firefox port looked a bit tricky to install.

Now, for the pièce de résistance …

require File.dirname(__FILE__) + '/../spec_helper'
require 'rubygems'
require 'safariwatir'

describe UserController do
  FB_EMAIL = 'xxx@xxxxx.com'
  FB_PASS = 'xxxxx'

  FB_URL = 'http://www.facebook.com'
  CALLBACK_URL = 'http://dev.xxxxxxxxx.com'
  FB_APP_URL = 'http://apps.facebook.com/xxxxx/'
  FB_LOGOUT_URL = 'http://www.facebook.com/logout.php'

  LOGOUT_REGEX = /http:\/\/www.facebook.com\/logout.php\?.+/

  it "should use UserController" do
    controller.should be_an_instance_of(UserController)
  end

  describe "POST 'index'" do
    before(:each) do
      @browser = Watir::Safari.new
      @browser.set_fast_speed
      @browser.goto(FB_APP_URL)
      @browser.link(:url, LOGOUT_REGEX).click rescue nil
    end

    it "should find authentication prompt" do
      @browser.goto(FB_APP_URL)
      @browser.form(:id, 'loginform').exist?.should_not == nil
      expected = 'Login to Facebook to enjoy the full functionality of'
      @browser.contains_text(expected).should > 0
    end

    it "should authenticate" do
      authenticate
      expected = /Login to the .* application?/
      @browser.contains_text(expected).should != nil
    end

    it "should log in to the application" do
      authenticate
      @browser.form(:index, 1).submit
    end

  end

  private

  def authenticate
    @browser.goto(FB_APP_URL)
    @browser.text_field(:id, 'email').set(FB_EMAIL)
    @browser.password(:name, 'pass').set(FB_PASS)
    @browser.form(:index, 1).submit
  end

end

The code above is a work in progress, but it is still fully functional rspec code. Feel free to use it.

Just a note if you do end up using the Safari Watir port. I had to make a change to the core libraries as there seemed to be a race condition. If you are affected, you will notice an exception thrown part way through entering the data in a form. My quick fix/hack is to extend the sleep time from 1 to something larger. I had no problems after increasing the sleep time to 4.

# safariwatir/scripter.rb
def page_load
	yield
	#sleep 1
	sleep 4
	....
end

Developing For Social Networks

May 5th, 2008

I’ve been looking in to developing an application for social networks recently. Here are a few points of interest.

Pre-built Authentication System

w00t! Tired of writing yet another authentication system? Disappointed by the lack lustre take up of OpenID, +associated technologies? Well screw them. Whether you like it or not, the plebs among us are more likely to log in to a social network to authenticate than your “yet another damn password to remember” authentication system, or “I can’t believe this is supposed to be easier” OpenID authentication system.

Write For Popularity

You too can play the social networks, in the same way that Gordon “fuck me” Ramsay plays the camera. Give people what they want by crawling through the masses of information about applications already available on the social network sites. Want to know the most popular dating, gaming, news related application on Facebook? Check out the application directories.

OpenSocial

OpenSocial is a lot of hand waving and not very much substance… yet. OpenSocial is the promise of writing code once, to a standard interface, and having it run on all social networks. The problem right now is that you need to get in to the copious private beta’s to test your applications within OpenSocial compatible containers, and then who knows if you are then allowed to unleash your application to the rest of the social network. I’ve got an eye on you OpenSocial, get back to me when you’re ready please.

Lightweight

One of the cool things about developing applications on social networks is that you can make them very lightweight by fobbing off the processing to the client’s browser, and the storage to the social network persistence layer. Seriously. Social networks have a storage layer, per application, per user. Want to record the win/loss for a user playing hangman? Stuff it in the persistence layer of the social network. Want to grab a collection of a user profile pictures? No problem, grab the URL from the social network and tell the client’s browser to grab the image from the URL. Facebook have a javascript library so you perform a bunch of stuff without ever having to touch your server.

The only problem you will find is processing data. If you want to track your users then you are probably best to use a database on your server side, rather than the persistence layer. Also, if you want to bridge data access across social networks then you also probably want to store data on your own server. Your storage decisions are going to depend on the type of application you are building.

It’s worth noting that Facebook have the Facebook Query Language, or FQL for you acronym obsessed tech heads. It’s like of like a cut down SQL that you can use to query the Facebook data. I haven’t looked in to it much as my application has no need for this, but it looks very interesting.

Conclusion

So far it seems not too hard to develop for social networks. I found it a little confusing to understand how my application plugged in to the rest of the social network but once you jump in it’s fairly straight forward. I also, and am still, finding it a bit of an effort to understand what information is available, and what my application is allowed to do. For example, I don’t want to spam my users friends but I do want to market effectively.

That brings me to my final paragraph. There’s a bit to be said about the maturing economics of social networks. IMHO the economics of social networks are still in their infancy, but the power found in a network of friends combined with the established user base leaves a lot of room for further development. In my next post I will discuss marketing to social network users. Social networks are a marketers dream. You may be surprised with some of what I have to give away, so subscribe to my blog you lazy bastard.


Gordon Ramsay

buy mp3 music uk vpn