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

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

Release: Social Buttons Wordpress Plugin

May 9th, 2008

Here’s a Wordpress plugin that allows you to selectively add social network buttons such as digg, sphinn, etc to your posts or theme design.

There’s a stack of these type of plugins out there, but I couldn’t find one that allowed me to add a button for a social network on a per post basis. My main point of pain was that each of my blog posts appeal to different types of social sites so I wanted to be selective with the social buttons I added to each post. For instance, my SEO and marketing posts are better suited to Sphinn, while my technical posts are better suited to Reddit.

Add Buttons Per Post

To scratch my own itch I devised a wordpress plugin creatively called “Social Buttons”. You write a formatted piece of text in your post, and it’s replaced with the button when converted to HTML. It’s kind of like magic, except real, and without cheesy costumes. For example, to add a digg button I write this…


Adding a Digg button using Social Buttons Wordpress Widget

Add you get this result:


You can add more than a single button if you want…


Adding a Sphinn and Reddit button using Social Buttons Wordpress Widget

Add you get this result:


Add Buttons To Your Theme, Or All Posts

It’s also possible to use the plugin within your theme. You add a button to your theme (Digg in this case):

<?php echo sb_make_button('digg', 'http://www.alexpooley.com') ?>

Or, add the same button to every single post (Reddit in this case):

# Inside posts.php of your theme.
<?php echo "I'm in a post now:".sb_make_button('reddit', get_permalink()) ?>

Install

Download the Social Buttons Wordpress Plugin and install the plugin as you would install any other plugin. There are further instructions in the readme.txt if you need them.

If you want a social site added to the plugin then let me know. Or, if you need a particular Wordpress plugin built then get in touch.


Save To Delicious

Google Suggests Spamming Anchor Text

May 8th, 2008

I stumbled across this text while browsing through my website statistics in Google’s Webmaster Center.

Google Suggests Spamming

So there you go! Get spamming working with your back linkers people!