Capybara on the Command Line - Live Browser Testing From IRB

Everyone knows retro fitting tests is not the best way to go, but you can’t escape the fact that sometimes it’s unavoidable. Writing integration tests after the fact can sometimes be tricky, especially when dealing with complex xpaths or trying to assert against deeply buried elements. So if you’re as tired of digging around in web inspector as I am, try this instead:

gem install capybara

And then fire up console:

require 'capybara/dsl'
=> true 
include Capybara::DSL
=> Object 
Capybara.default_driver = :selenium
=> :selenium 
visit "http://google.co.uk"
=> "" 

Watch and smile as Firefox opens, and then access anything on it with page

page.fill_in('q', :with => 'capybara')
=> ""
page.click_link "jnicklas/capybara - GitHub"
=> "ok"

And by getting instant feedback in this way, those steps will write themselves.

Get Scamp for Campfire Running on Heroku

Scamp is a great bot framework for Campfire, and here’s how to get a bot up and running in no time on Heroku.

First up, create a new directory for the project and make sure bundler and heroku gems are installed.

mkdir scampbot;cd scampbot
gem install bundler heroku

Then create a Gemfile

Gemfile
1
2
3
source :rubygems
gem 'rack'
gem 'scamp'

bundle, set up a git repo, and then create a new Heroku app

bundle
git init
git commit -a -m "boom"
heroku create

As Heroku is happy running Rack based apps, all we need to do here is include a rackup file named config.ru in the root directory.

config.ru
1
2
3
4
5
6
7
8
#!/usr/bin/env ruby

require 'bundler/setup'
require 'rack'
require 'scamp'

# respond to something
run lambda { |env| [200, {'Content-Type'=>'text/plain'}, StringIO.new("It's Alive!\n")] }

This tiny Rack app will just respond to any request with “It’s Alive!” - handy for stopping Heroku apps sleeping on the job. Push it up to Heroku and try it out.

    git add .
    git commit -m "Adding config.ru"
    git push heroku master
    heroku open

Next grab your Campfire API key - rather than checking this into the repo create some config vars instead

heroku config:add CAMPFIRE_KEY=your_api_key SUBDOMAIN=your_subdomain

With those variables in place start on Scamp

config.ru
1
2
3
4
5
6
# do the robot
scamp = Scamp.new(:api_key => ENV['CAMPFIRE_KEY'],
                  :subdomain => ENV['SUBDOMAIN'])

# add some rooms, can be ID numbers or room names
rooms = ['My awesome room', 1234 ]

Now the basics are in place all that’s left to do is define some simple behaviour and connect to the rooms

config.ru
1
2
3
4
5
6
7
8
scamp.behaviour do

  match /^blame (?<scapegoat>.+)$/ do
    say "Damnit #{scapegoat}!"
  end
end

scamp.connect!(rooms)

And that’s it, push it up to Heroku!

git commit -m "adding scamp"
git push heroku master

Or for the super quick version, clone my example app.

When you’ve got the basic setup in place I highly recommend checking out the Scamp Readme to learn about some of Scamp’s other moves.

Contributing to Open Source Projects With Give

Working as part of a large team on a sizeable number of applications, it’s fair to say that I spend quite a lot of time thinking about git. After spotting a recent Railscast on open source contributions, it occurred to me that the process involved in maintaining an open source fork isn’t neccessarily obvious. As a result, I’ve released give, a gem which wraps up some simple git workflow alongside the GitHub API, with the aim of making it just a little bit easier to get started contributing to open source projects.

The setup

gem install give

give only requires that a GitHub user and token are set, you can check this by looking in ~/.gitconfig or from the command line

git config --get github.user
git config --get github.token

If either are missing, this GitHub article will walk you throug the process.

With give installed you can now fork a GitHub repository from the command line using the give setup USERNAME REPO command. This example will fork the give project from the user seenmyfate

give setup seenmyfate give

This command uses the GitHub API to fork the project you have specified to your GitHub account, and then clones your fork to your current path. An extra remote ‘upstream’ is also added to the project, pointing back to original project.

Staying current

Once you’re ready to start coding, the give start BRANCH command will create and checkout a new branch to work in

give start feature/my_awesome_feature

To be sure you’re working on the latest code, the give update BRANCH command will rebase your fork’s master branch from the original repository, and then rebase your feature branch.

give update feature/my_awesome_feature

By doing this regularly you can be sure that you are not duplicating work, and also ensure that any code you depend on has not changed since you started work. This should also help your pull request to apply cleanly, allowing the project maintainer to easily pull in your contribution.

Pull

When your contribution is complete, push your changes to GitHub and send a pull request with give finish. Here you must pass in the user and repository to send the pull request, and your local branch.

  give finish seenmyfate give feature/my_awesome_feature

You will be prompted to enter a title and body for the pull request.

And that’s it. I hope this gem will encourage more people to make their first open source contribution, and maybe make things a little easier for those who don’t spend all day merging and rebasing with git. I’d love to hear from anyone having success with give, and of course if you have any ideas for improvements, please fork away!

Vim - My .vimrc Highlights

Moving from TextMate to Vim is probably the most significant change I’ve made to my daily development over the last year - and I’m amazed by how much difference it’s made. A few weeks back though this blog post convinced me to go one step further - I took a deep breath and deleted my entire vim install.

Starting over

Following Mislav’s advice, I started out with 13 lines of simple configuration, initially adding only pathogen, the vim-rails plugin and the truly awesome solarized theme. With those staples in place I left my .vimrc file open, made friends with :source $MYVIMRC and resolved to only add a mapping or plugin if my workflow depended on it. Two weeks later I can safely say this is the best thing I’ve done since switching to vim, so here are the highlights from my configuration.

Fuzz Face

Command-T is often suggested as the drop-in vim replacement for TextMate’s popular fuzzy finder functionality, but I heartily recommend the far superior ctrlp. The speed alone is worth the switch, but once you add in regexp mode, the ability to jump directly to a line number, and multiple file opening, Command-T is firmly outclassed.

Force yourself

Between gvim and arrow keys I’ve been cheating for a while, nothing has forced me into dropping some bad habits than locking the arrow keys.

1
2
3
4
  map <Left> :echo 'damnit!'<cr>
  map <Right> :echo 'you suck!'<cr>
  map <Up> :echo 'this is why you fail'<cr>
  map <Down> :echo 'nooooo!'<cr>

Split panes and tabs

I love split panes, this feature was the main reason I left TextMate in the first place. Opening a split pane and jumping into it is an essential shortcut, quite often followed up with vim-rails’ :A

1
  map :vs :vsplit<cr><c-w>l

And some shortcuts to resize the split.

1
2
3
4
5
6
  " Resize windows quickly
  " reset with <c-w>=
  nmap <c-w>l :vertical res +20<cr>
  nmap <c-w>h :vertical res -20<cr>
  nmap <c-w>j :res +20<cr>
  nmap <c-w>k :res -20<cr>

Whilst I tend not to use tabs as often as I use buffers, I still enjoying being able to flip between tabs quickly when I do.

1
2
3
  nmap <leader>[ :tabprevious<cr>
  nmap <leader>] :tabNext<cr>
  nmap T :tabnew<cr>

Scratch buffer

With the help of the scratch plugin this gives me a quick split for random notes, something I use all the time.

1
  nmap <leader>; :Sscratch<cr>

And more

In addition to these mappings I’ve borrowed Gary Bernhardt’s testing functions and really cool shortcuts to scroll the other split window.

Feel free to take my vim for a spin, the vimrc is pretty well documented.

  git clone git://github.com/seenmyfate/vim.git ~/.vim
  cd ~/.vim && rake

‘Fuck Spotify’ - a Rant in D Minor

Over the last few months I’ve noticed a growing murmuring from musicians about Spotify and royalty payments, culminating in this Music Week article that inspred this rant.

“Got paid £8 for 90,000 plays. Fuck spotify.”

Fuck Spotify. I mean really? Do you cry when thousands of people downloaded a free track on your website? Are you devastated because your SoundCloud profile has topped 100,000 plays? Are you enraged with the injustice of Radio 1 playing your music to millions of listeners for free and only giving you a measly £60 in return? Such outrage. 90,000 potential new fans? Fuck Spotify.

And it’s not just Mercury Prize nominated artists rushing to alienate their fans, some labels I used to respect are getting in on it as well:

“Spotify will kill smaller bands that are already struggling to make ends meet”

So said Century Media as they pulled their entire catalog.

Well I call bullshit Century Media, even if you filled the late 90’s with glorious metal for me - or at least filled in the gaps between Roadrunner, Peaceville and Earache. This isn’t killing smaller bands - can you honestly stand there and say that a service like Spotify is going to be the final straw for a struggling band? Seriously? It won’t be the unscrupulous promoter that pulls the show when they’ve rented a van and drove 400 miles? It’s not the cost of replacing the gear stolen from an uninsured rehearsal room? It’s not the management paying thousands to move the best support tours out of reach? It’s not the labels unprepared to invest in developing talented bands? There’s an untold number of things that are killing smaller bands and Spotify is way down that list. Surely by the end of 2011 it has to be time record labels grow up and stop blaming the internet for their imminent demise - perhaps they’d be able to capture some higher moral ground when arguing the value of music if they weren’t flooding the market with inane disposable crap.

If you’re a musician, your product is you - it’s your live shows, it’s your publishing, it’s what you can physically sell to real people. You have to treat Spotify exactly as it is, a glorified Myspace player that may (or may not) help you to connect with more fans who - if you’re on top of your game - might just put in the time to care about your music. There’s never been a better time to be an independent musician, so stop being so upset that people are actually listening to your music and be happy that you live in an age where it’s possible to produce a professional sounding album on your goddamn mobile phone.

Update 15/12/11 - for the record, in my last PRS statement Spotify paid me at a rate that would have made 90,000 plays worth £72