Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

RubyintermediatebackendAI generated

Ruby Gem for Currency Conversion with Caching

Build a small Ruby gem, a reusable library other programs can install, that converts amounts between currencies using exchange rates from a free public API. It fetches rates with Faraday, caches them in a local file for an hour so repeated conversions don't call the API, falls back to the cached rates when the API is unreachable, and offers a clean, documented interface such as CurrencyConverter.new.convert(100, from: "EUR", to: "USD"). Money is handled with BigDecimal, so results are exact. You'll learn the structure of a gem, HTTP requests and error handling with Faraday, file-based caching, designing a library API with clear errors, BigDecimal, and testing with RSpec and stubbed HTTP.

Estimate
~6.5h
Steps
5
Completed by
0
Proposed by
codeseed.app

Faraday

Project roadmap

  1. 01

    Create the gem

    ~1h

    Create the skeleton with bundle gem currency_converter (choosing RSpec for tests), add faraday as a dependency and webmock as a development dependency, and fill in the gemspec (summary, description, license and required Ruby version). Create a CurrencyConverter::Converter class and the gem's own error classes (UnknownCurrency, RatesUnavailable). Done when: bundle exec rake builds the gem.

  2. 02

    Fetch the rates

    ~1.5h

    Write a RatesClient that uses a Faraday connection (with timeouts and a retry middleware for temporary errors) to fetch the latest rates for a base currency from a free API such as Frankfurter, parses the JSON, and returns the rates as BigDecimal values with the date. Turn network errors and bad responses into RatesUnavailable with a clear message. Done when: the client returns real rates, and failures raise the gem's error.

  3. 03

    Cache the rates

    ~1.5h

    Cache the rates in a JSON file (in a configurable folder, by default the user's cache folder) with the time they were fetched. Use cached rates when they're less than an hour old (configurable), and fall back to older cached rates with a warning when the API fails. Write the cache atomically. Done when: a second conversion within an hour makes no request, and the API being down still works with a cache.

  4. 04

    Implement convert

    ~1h

    Add convert(amount, from:, to:) that validates the currency codes (3 letters, and present in the rates), converts through the base currency with BigDecimal, and rounds to 2 decimals by default (configurable). Also add rate(from:, to:) and supported_currencies. Converting a currency to itself needs no request. Done when: conversions are exact and match manual calculations.

  5. 05

    Write specs and docs

    ~1.5h

    Write RSpec tests with WebMock stubbing the API: fetching and parsing, caching (fresh, stale and missing cache), fallback when the API fails, unknown currencies, rounding, and same-currency conversion. Document installation, usage examples, configuration and errors in README.md, with YARD comments on the public methods. Done when: bundle exec rspec passes, and README.md explains how to use the gem.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~6.5h · 5 steps

Tech stack

Faraday