/ crystal

EightTrack

I'm really loving how it feels to work with Crystal. I've been developing with Ruby for quite some time and it has been great. But with Ruby I feel like you end up dealing with a lot of stuff that a compilier would catch right out the gate. That is something I will get into later.

For now, I needed something similar to VCR for running specs that connect to an api. My first simple attempt was just manually save the api responses as JSON files and read those during the specs. That was fine until the next person picked up the project and wanted to make changes. It was time for a better solution.

EightTrack is a very simple shard that gives you the basics of what VCR for Ruby does.

Add it to your shard.yml file with:

dependencies:
  eighttrack:
    github: russ/eighttrack

Then in your spec/spec_helper.cr file, require it and configure it if needed.

require "eighttrack"

EightTrack.configure do
  settings.tape_library_dir = "/some/path/eightracks"
end

Now in your specs, you just need to wrap your HTTP calls in a use_tape block.

EightTrack.use_tape("in-a-gadda-da-vita") do
  response = HTTP::Client.get("https://ipapi.co/8.8.8.8/json")
end

The first time it runs, it will connect to the api and then store response. The times after that however will use the stored response, making your specs MUCH faster and easier to maintain since you won't have to do any strange mocking or stubbing.