Class: ExchangeRatesAPI::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/exchangeratesapi/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, base_url = nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/exchangeratesapi/client.rb', line 10

def initialize(api_key = nil, base_url = nil)
  @api_key = api_key || ENV.fetch("EXCHANGE_RATE_API_KEY", nil)
  @base_url = base_url || ENV.fetch("EXCHANGE_RATE_API_BASE", "https://api.exchangeratesapi.io/v1")
  
  # Set the base URI for HTTParty
  self.class.base_uri @base_url
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/exchangeratesapi/client.rb', line 8

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



8
9
10
# File 'lib/exchangeratesapi/client.rb', line 8

def base_url
  @base_url
end

Instance Method Details

#convert(amount:, from_currency:, to_currency:, date: nil) ⇒ Hash

Convert amount between currencies

Parameters:

  • Amount to convert

  • Source currency (e.g., "USD")

  • Target currency (e.g., "EUR")

  • (defaults to: nil)

    Optional date in YYYY-MM-DD format for historical rates

Returns:

  • Conversion result with amount and rate

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/exchangeratesapi/client.rb', line 72

def convert(amount:, from_currency:, to_currency:, date: nil)
  response = exchange_rate(
    from_currency: from_currency,
    to_currency: to_currency,
    date: date
  )
  
  rate = response.rates[to_currency]
  raise ExchangeRatesAPI::Error.new("Rate not found for #{to_currency}") if rate.nil?
  converted_amount = amount * rate
  
  {
    amount: amount,
    from_currency: from_currency,
    to_currency: to_currency,
    rate: rate,
    converted_amount: converted_amount,
    date: response.date
  }
end

#currenciesPayloadWrapper

Get supported currencies

Returns:

  • Response wrapped in PayloadWrapper



60
61
62
63
64
# File 'lib/exchangeratesapi/client.rb', line 60

def currencies
  response = execute_request("get", "/symbols")
  parse_and_raise_error(response) unless success_response?(response)
  PayloadWrapper.new(response.parsed_response)
end

#exchange_rate(from_currency:, to_currency:, date: nil) ⇒ PayloadWrapper

Get exchange rate for specific currencies

Parameters:

  • Base currency (e.g., "USD")

  • Target currency (e.g., "EUR")

  • (defaults to: nil)

    Optional date in YYYY-MM-DD format for historical rates

Returns:

  • Response wrapped in PayloadWrapper



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

def exchange_rate(from_currency:, to_currency:, date: nil)
  if date
    historical(date, from_currency: from_currency, to_currency: to_currency)
  else
    latest(from_currency: from_currency, to_currency: to_currency)
  end
end

#fluctuation(start_date:, end_date:, from_currency: "EUR", to_currency:) ⇒ PayloadWrapper

Get fluctuation data (requires paid plan)

Parameters:

  • Start date in YYYY-MM-DD format

  • End date in YYYY-MM-DD format

  • (defaults to: "EUR")

    Base currency (e.g., "USD")

  • Target currency (e.g., "EUR")

Returns:

  • Response wrapped in PayloadWrapper



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/exchangeratesapi/client.rb', line 118

def fluctuation(start_date:, end_date:, from_currency: "EUR", to_currency:)
  params = {
    base: from_currency,
    symbols: to_currency,
    start_date: start_date,
    end_date: end_date
  }
  
  response = execute_request("get", "/fluctuation", params)
  parse_and_raise_error(response) unless success_response?(response)
  PayloadWrapper.new(response.parsed_response)
end

#historical(date, from_currency: "EUR", to_currency: nil) ⇒ PayloadWrapper

Get historical exchange rates

Parameters:

  • Date in YYYY-MM-DD format

  • (defaults to: "EUR")

    Base currency (e.g., "USD")

  • (defaults to: nil)

    Target currency (e.g., "EUR") or comma-separated list

Returns:

  • Response wrapped in PayloadWrapper



36
37
38
39
40
41
42
43
# File 'lib/exchangeratesapi/client.rb', line 36

def historical(date, from_currency: "EUR", to_currency: nil)
  params = { base: from_currency }
  params[:symbols] = to_currency if to_currency
  
  response = execute_request("get", "/#{date}", params)
  parse_and_raise_error(response) unless success_response?(response)
  PayloadWrapper.new(response.parsed_response)
end

#latest(from_currency: "EUR", to_currency: nil) ⇒ PayloadWrapper

Get latest exchange rates

Parameters:

  • (defaults to: "EUR")

    Base currency (e.g., "USD")

  • (defaults to: nil)

    Target currency (e.g., "EUR") or comma-separated list

Returns:

  • Response wrapped in PayloadWrapper



22
23
24
25
26
27
28
29
# File 'lib/exchangeratesapi/client.rb', line 22

def latest(from_currency: "EUR", to_currency: nil)
  params = { base: from_currency }
  params[:symbols] = to_currency if to_currency
  
  response = execute_request("get", "/latest", params)
  parse_and_raise_error(response) unless success_response?(response)
  PayloadWrapper.new(response.parsed_response)
end

#time_series(start_date:, end_date:, from_currency: "EUR", to_currency:) ⇒ PayloadWrapper

Get time series data (requires paid plan)

Parameters:

  • Start date in YYYY-MM-DD format

  • End date in YYYY-MM-DD format

  • (defaults to: "EUR")

    Base currency (e.g., "USD")

  • Target currency (e.g., "EUR")

Returns:

  • Response wrapped in PayloadWrapper



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/exchangeratesapi/client.rb', line 99

def time_series(start_date:, end_date:, from_currency: "EUR", to_currency:)
  params = {
    base: from_currency,
    symbols: to_currency,
    start_date: start_date,
    end_date: end_date
  }
  
  response = execute_request("get", "/timeseries", params)
  parse_and_raise_error(response) unless success_response?(response)
  PayloadWrapper.new(response.parsed_response)
end