Class: Jekyll::Unirate::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/unirate/client.rb

Overview

Minimal stdlib-only HTTP client for the UniRate API (unirateapi.com). Pulls a single-base rate snapshot from ‘GET /api/rates`; cross-rates are derived from it by Snapshot.

Zero runtime dependencies beyond Ruby’s standard library — ‘net/http` and `json`. No third-party HTTP gem, so nothing extra to audit.

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.unirateapi.com"
DEFAULT_TIMEOUT =
30

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.

Raises:



28
29
30
31
32
33
34
# File 'lib/jekyll/unirate/client.rb', line 28

def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: DEFAULT_TIMEOUT)
  raise Error, "UniRate API key is required" if api_key.nil? || api_key.to_s.empty?

  @api_key = api_key
  @base_url = base_url
  @timeout = timeout
end

Instance Method Details

#fetch_snapshot(base) ⇒ Object

Fetch a single-base snapshot. Returns a Snapshot whose ‘rates` map is { “EUR” => BigDecimal, … } expressed against base.

Raises:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jekyll/unirate/client.rb', line 38

def fetch_snapshot(base)
  base = base.to_s.upcase
  body = http_get("/api/rates", "from" => base)
  rates = body["rates"]
  raise Error, 'Unexpected UniRate response: missing "rates"' unless rates.is_a?(Hash)

  parsed = rates.each_with_object({}) do |(code, value), out|
    out[code.to_s.upcase] = BigDecimal(value.to_s)
  end
  Snapshot.new(base: base, rates: parsed)
end