TextMate snippets for test/spec

written by matt on July 18th, 2007 @ 02:27 PM

Here are a few TextMate snippets I’ve been using lately to speed up working with test/spec.

# snippet: specify
# activation: tab trigger 'spec'

specify "should $1" do
  $0
end

# snippet: xspecify (repeat)
# activation: tab trigger 'xspec'

xspecify "should $1" do
  # TODO: should $1
end

xspec$0

# snippet: context
# activation: tab trigger 'con'

context "$1" do
  ${2:use_controller :${3:controller}
  }
  ${4:setup do
    $5
  end

  }${6:xspec}$0
end

Use a scope selector of ‘source.ruby’ for all of the above. The context and xspecify snippets build on each other, so you can quickly define a context and knock through your requirement list, going back to populate them later. If you change the activation trigger for xspecify you’ll need to change the context snippet to reflect that.

The todo lines are handy because then you can always use the rake notes task to get a quick list of what you have outstanding at all times. Your output will look something like this:

rake notes

test/functional/users_controller_test.rb:
  * [130] [TODO] should show user's profile image
  * [134] [TODO] should have link to send message to user
  * [138] [TODO] should show user's friends

You may also want to check out Chris Wanstrath’s awesome rake task for generating a spec from a quick YAML text list, which can be handy if you want to divorce yourself from the test code for a minute and really focus on your specs.

UPDATE: I’ve fixed a typo in the xspec definition which made it a broken block (bad paste, sorry) and updated context definition with my new version which has better whitespace management when you delete optional elements.

PayJunction Support for Rails via ActiveMerchant

written by matt on July 16th, 2007 @ 09:30 AM

A couple of weeks ago the folks over at jadedpixel were kind enough to accept my patch adding support for the PayJunction gateway to their payment processing library, ActiveMerchant.

Of course every processor has their pluses and minuses, but one really cool thing about Pay Junction is their support for “instant” transactions, effectively the ability to run a new authorization or capture on a previously run card using an authorization from before. Say we have a new user we need to charge $10USD. This could look something like:

response = @gateway.purchase(Money.new(1000), @creditcard)
if response.success?
  @authorization = response.authorization
end

Now we can store that authorization along with information about our user and later on if we need to charge them for something else we can run a charge without having stored any information about their card.

So at some date in the future we can:

response = @gateway.purchase(Money.new(2500), @authorization)

...and that’s about all there is to it. An awesome feature I wish more gateways supported. While this doesn’t cover every case, there’s a lot of situations where this kind of feature can help you avoid the security/complexity issues of storing credit card information. If you do need to warehouse credit cards, Tobias has a few thoughts.

If you’re interested in using the gateway, you can check out the in-class documentation. You may also find want to take a look at the tests, since they demonstrate how most of the transaction types work in practice.

Ruby and iconv on FreeBSD

written by matt on July 9th, 2007 @ 08:55 PM

If you’ve installed ruby from the ports collection on FreeBSD or have an older version of iconv on your system, you may not get iconv from the standard library installed. Assuming you’re using a ruby or rails program that needs it, you may see something like this:

no such file to load -- iconv

The reason is that ruby needs to be able to find a version of the iconv library that it will play nicely with (read, a reasonably recent one) or otherwise it just doesn’t bother building the ruby version of iconv. Since ruby’s iconv is compiled, it will be in a different place depending on your OS. On the Mac my most current version lives here:

/usr/local/lib/ruby/1.8/powerpc-darwin8.10.0/iconv.bundle

While on FreeBSD you might find it here:

/usr/local/lib/ruby/1.8/i386-freebsd6.2/iconv.so

The version and OS strings will vary, but you should be able to find something in your standard lib directory that holds all the compiled extensions and looks something like the above.

If you’ve installed ruby using FreeBSD ports collection, the answer is simple: install the ruby-iconv port (in converters). If you’ve compiled from source you may need to upgrade your installed version of iconv and run configure/make again.