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
Wow, that’s all there is to it? Mechanize FTW!
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.
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/).
Mechanize Rules! Turn off the Xbox though? With GTA IV and Halo 3 I’m not sure that’s possible.
At a glance Mechanize looks similar to Curb
Any major advantages?
Ok, on second glance the syntax looks nicer, plus forms are submitted via the form URL instead of curl.
I’ve gotta say I’ve just spent about half an hour playing around with Mechanize and it’s awesome.
@defunkt – is this your way of saying you’re not adding anything extra to github’s data API? :)
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.
Mechanize is pretty cool. Thanks for introducing us!
Chime in.