Class: UniRateRails::Client

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

Overview

Stdlib-only HTTP client for the UniRate API (https://unirateapi.com).

Zero runtime dependencies beyond Ruby's standard library (net/http, json, uri) — no third-party HTTP gem, so nothing extra to audit in a Rails app's dependency tree.

Method names and parameter order mirror the canonical UniRate client spec. Currency and country codes are upcased before being sent.

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil, timeout: nil) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
# File 'lib/unirate_rails/client.rb', line 17

def initialize(api_key: nil, base_url: nil, timeout: nil)
  config = UniRateRails.configuration
  @api_key = api_key || config.api_key
  @base_url = base_url || config.base_url
  @timeout = timeout || config.timeout

  raise AuthenticationError.new("Missing or invalid API key", status: 401) if @api_key.nil? || @api_key.to_s.empty?
end

Instance Method Details

#convert(to:, amount: 1, from: "USD", format: "json", callback: nil) ⇒ Object

GET /api/convert — current conversion. Returns a Float.



42
43
44
45
46
47
48
# File 'lib/unirate_rails/client.rb', line 42

def convert(to:, amount: 1, from: "USD", format: "json", callback: nil)
  params = { "from" => up(from), "to" => up(to), "amount" => amount }
  body = request("/api/convert", params, format: format, callback: callback)
  return body unless json?(format)

  to_f(body.fetch("result"))
end

#get_historical_rate(date:, amount: 1, from: "USD", to: nil, format: "json", callback: nil) ⇒ Object

GET /api/historical/rates — Pro-gated (403 on free tier). Guarded by the enable_historical config flag so a free-tier app doesn't hit it by accident. Returns a Float or a { code => Float } map depending on args.



72
73
74
75
76
77
78
79
80
# File 'lib/unirate_rails/client.rb', line 72

def get_historical_rate(date:, amount: 1, from: "USD", to: nil, format: "json", callback: nil)
  ensure_historical_enabled!
  params = { "date" => date, "from" => up(from), "amount" => amount }
  params["to"] = up(to) if to
  body = request("/api/historical/rates", params, format: format, callback: callback)
  return body unless json?(format)

  parse_historical(body, to: to)
end

#get_rate(from: "USD", to: nil, format: "json", callback: nil) ⇒ Object

GET /api/rates — current rate(s). Returns a Float when to is given, otherwise a { "EUR" => Float, ... } map.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/unirate_rails/client.rb', line 28

def get_rate(from: "USD", to: nil, format: "json", callback: nil)
  params = { "from" => up(from) }
  params["to"] = up(to) if to
  body = request("/api/rates", params, format: format, callback: callback)
  return body unless json?(format)

  if to
    to_f(body.fetch("rate"))
  else
    floatify(body.fetch("rates", {}))
  end
end

#get_supported_currencies(format: "json", callback: nil) ⇒ Object

GET /api/currencies — supported currency codes. Returns an Array of String.



51
52
53
54
55
56
# File 'lib/unirate_rails/client.rb', line 51

def get_supported_currencies(format: "json", callback: nil)
  body = request("/api/currencies", {}, format: format, callback: callback)
  return body unless json?(format)

  Array(body.fetch("currencies", []))
end

#get_vat_rates(country: nil, format: "json", callback: nil) ⇒ Object

GET /api/vat/rates — VAT rates. Returns the whole map keyed by country when country is nil, otherwise the single-country vat_data hash.



60
61
62
63
64
65
66
67
# File 'lib/unirate_rails/client.rb', line 60

def get_vat_rates(country: nil, format: "json", callback: nil)
  params = {}
  params["country"] = up(country) if country
  body = request("/api/vat/rates", params, format: format, callback: callback)
  return body unless json?(format)

  country ? body.fetch("vat_data") : body.fetch("vat_rates")
end