Updated 2026: The Viaggiatreno API is an unofficial, undocumented API that Trenitalia has changed over the years without notice. The gem may need updates to reflect current API responses. Check the GitHub repository for the current status.

I’ve been steadily working on viaggiatreno, a Ruby gem that parses real-time train information from Italy’s railway network: current location, delay, expected and actual arrival times. Version 1.0.5 is out, with a focus on code quality, with zero Rubocop violations.

Source code is on GitHub: mbologna/viaggiatreno, with usage examples in the README.

Testing code that depends on live web requests Link to heading

The most interesting challenge I dealt with while writing continuous integration tests (via Travis CI) was this: the gem fetches live data from the Viaggiatreno API, and train information changes from request to request. How do you write deterministic tests for something that’s inherently non-deterministic?

The answer is the VCR gem. VCR records real HTTP interactions on the first test run (“cassettes”), then replays those recorded responses on subsequent runs. This means:

  • Tests are fast: no real network requests after the first run
  • Tests are deterministic: the same recorded response is returned every time
  • You test your parsing logic against real-world API responses, not hand-crafted mocks

It’s a clean solution for any Ruby project that talks to external web services. If you’re testing code that depends on external APIs, VCR is worth adding to your test toolkit.