Introducing mechanized_session and acts_as_other_website

Have you ever wanted to make a website that's just a re-skin or re-layout of another website? I always find myself wanting to do this, especially when there's no mobile version of a site and no API.

Enter mechanzied_session

All the mechanized_session gem provides is a way for the programmer to easily define remote actions that require an authenticated session. By providing these actions and an implementation for how to login, mechanized_session makes your life very easy.

View mechanized_session on GitHub

Acts_as_other_website on rails

MechanizedSession requires work to integrate with a frontend, so if you're building a rails app that just a skin of another website, acts_as_other_website is for you. It provides a default login page, handles all the normal exceptions that MechanizedSession raises, and provides the glue that stores your remote session data in your local session hash.

Example using EngineYard's cloud website

class EySession < MechanizedSession
  action :login do |session, options|
    session.get("https://login.engineyard.com/login") do |page|
      next_page = page.form_with(:action =>"/login") do |form|
        form["email"] = options[:username]
        form["password"] = options[:password]
      end.click_button
    end
    true  # tells MechanizedSession that login was successful.  EY returns a 401 exception if not successful
  end

  action :list_environments do |session|
    envs = []
    session.get("https://cloud.engineyard.com/dashboard") do |page|
      page.parser.css("div.environment").each do |env|
        envs << env.css('h3').first["title"]
      end
    end
    envs
  end
end

Then invoke acts_as_other_website in ApplicationController

class ApplicationController
  acts_as_other_website :using => EySession
end

Finally create actions that call the remote server using the @mechanized_session object like so:

class EnvironmentsController < ApplicationController
  def index
    @environments = @mechanized_session.list_environments
  end
end

The plugin will take care of ensuring that there's always an authenticated session when you do actions that require it, and will redirect users to a provided login page to establish that session when necessary.

View this example online using your iPhone or Safari: http://eycloud.heroku.com

Visit acts_as_other_website on GitHub

Published on Mon, 07 Dec 2009 01:17

RSS