Err the Blog Atom Feed Icon
Err the Blog
Rubyisms and Railities
  • “Who Needs an API?”
    – PJ on May 14, 2008

    GitHub is a pretty fun site to work on, I’m not gonna lie. On more than one occasion, we thought it would be pretty cool to setup a service allowing public projects to receive donations.

    Sounds like an Itch

    Pledgie being the venerable service it is, I decided one night it couldn’t possibly be that hard to integrate it with GitHub. It’s a pretty standard Rails site with some simple forms to make the magic happen (eg. setup a donation page).

    They don’t have an API (yet), but in this day and age, you really don’t need one if you procure the proper tools.

    Enter Mechanize. You can do all I’m about to describe with just Net::HTTP, but seriously, who wants to do that?

    Start Scratching

    Step 0: Drive girlfriend to airport, buy a case of Anchor Steam, and turn off the Xbox.

    Step 1: Sign up for a Pledgie account, cause GitHub’s a regular user after all.

    Step 2: Write the interface on GitHub to accept the user’s Paypal address.

    Step 3: Figure out the form fields I should be filling out to login and create a new pledge on Pledgie.

    Step 4: Write the Mechanize code:

    def pledgify(email)
      agent = WWW::Mechanize.new
      page  = agent.get('http://pledgie.org/accounts/login')
      form  = page.forms[1]
      form['account[login]']    = GitHub::PledgieUser
      form['account[password]'] = GitHub::PledgiePass
      page  = agent.submit(form)
    
      link  = page.links.text(/Create A Campaign/)
      page  = agent.click(link)
      form  = page.forms[1]
      form['campaign[title]']        = "FooBarz"
      form['campaign[paypal]']       = email
      form['campaign[description]']  = "The best project evar!"
      form['campaign[end_date(1i)]'] = 10.years.from_now.year.to_s
      page = agent.submit(form, form.buttons.last)
      update_attribute(:pledgie, page.uri.to_s[/\d+/])
    end
    

    Step 5: Take the pledgie attribute we just grabbed and put a cool badge in their repository’s detail box.

    Step 6: Watch the millions pour in for GitHub’s hard-working open source committers.

    The obvious caveat here is that it relies on Pledgie not drastically changing the structure of its HTML, but it’s incredibly satisfying to throw something together like this in such a short amount of time.

    PS. If you’re on GitHub and wondering how you missed the original announcement, the post is here: http://github.com/blog/57-getting-paid-the-open-source-way

  • Garry, about 2 hours later:

    Wow, that’s all there is to it? Mechanize FTW!

  • Christopher Petersen, about 2 hours later:

    That’s a great feature and like the use of Mechanize.

    One thing though, I happened to see the feature before I saw the blog post though, and I must admit I was slightly confused. My initial reaction was, am I donating to the project or to GitHub? Just an observation.

    Great work, love the site and love the feature.

  • Andrew Pennebaker, about 5 hours later:

    Hey, that’s really nifty. That’s the second time WWW::Mechanize has come in handy. The first for me was a script to make some Free Rice (http://freerice.com/).

  • Scott Tamosunas, about 16 hours later:

    Mechanize Rules! Turn off the Xbox though? With GTA IV and Halo 3 I’m not sure that’s possible.

  • Zubin, 1 day later:

    At a glance Mechanize looks similar to Curb

    Any major advantages?

  • Zubin, 1 day later:

    Ok, on second glance the syntax looks nicer, plus forms are submitted via the form URL instead of curl.

  • Jim Neath, 1 day later:

    I’ve gotta say I’ve just spent about half an hour playing around with Mechanize and it’s awesome.

  • Dr Nic, 2 days later:

    @defunkt – is this your way of saying you’re not adding anything extra to github’s data API? :)

  • Jon Canady, 4 days later:

    We’ve started using Mechanize to black-box test some legacy applications—these are crufty old things where we don’t want to spend the time or energy to write internal unit tests, but we can certainly spend an afternoon and use RSpec and Mechanize to put together a set of “make sure this happens” scenarios.

  • Sam Nardoni, about 1 month later:

    Mechanize is pretty cool. Thanks for introducing us!

  • Ten people have commented.
    Chime in.
    Sorry, no more comments :(
This is Err, the weblog of PJ Hyett and Chris Wanstrath.
All original content copyright ©2006-2008 the aforementioned.