Class: ExchangeRatesAPI::Client
- Inherits:
-
Object
- Object
- ExchangeRatesAPI::Client
- Includes:
- HTTParty
- Defined in:
- lib/exchangeratesapi/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
-
#convert(amount:, from_currency:, to_currency:, date: nil) ⇒ Hash
Convert amount between currencies.
-
#currencies ⇒ PayloadWrapper
Get supported currencies.
-
#exchange_rate(from_currency:, to_currency:, date: nil) ⇒ PayloadWrapper
Get exchange rate for specific currencies.
-
#fluctuation(start_date:, end_date:, from_currency: "EUR", to_currency:) ⇒ PayloadWrapper
Get fluctuation data (requires paid plan).
-
#historical(date, from_currency: "EUR", to_currency: nil) ⇒ PayloadWrapper
Get historical exchange rates.
-
#initialize(api_key = nil, base_url = nil) ⇒ Client
constructor
A new instance of Client.
-
#latest(from_currency: "EUR", to_currency: nil) ⇒ PayloadWrapper
Get latest exchange rates.
-
#time_series(start_date:, end_date:, from_currency: "EUR", to_currency:) ⇒ PayloadWrapper
Get time series data (requires paid plan).
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_key ⇒ Object (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_url ⇒ Object (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
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 |
#currencies ⇒ PayloadWrapper
Get supported currencies
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
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)
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
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
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)
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 |