Class: ExchangerateApi::Client
- Inherits:
-
Object
- Object
- ExchangerateApi::Client
- Defined in:
- lib/exchangerate_api/client.rb
Overview
# Client
Client to fetch currency exchange rate data.
Constant Summary collapse
- SERVICE_ADDRESS =
Address of the exchagerate api service.
'https://api.exchangerate-api.com'
Instance Attribute Summary collapse
-
#version ⇒ String
readonly
Exchangerate api version.
Instance Method Summary collapse
-
#currency_codes ⇒ Hash
List all supported currency codes with countries.
-
#initialize(version: 'v4') ⇒ Client
constructor
Creates a new Client instance.
-
#rates_for(currency_code) ⇒ ExchangerateApi::Result
Fetch exchage rate data by currency code.
-
#rates_for_usd ⇒ ExchangerateApi::Result
Get exchage rate for usd.
Constructor Details
#initialize(version: 'v4') ⇒ Client
Creates a new Client instance.
33 34 35 36 37 38 39 |
# File 'lib/exchangerate_api/client.rb', line 33 def initialize(version: 'v4') @version = version @connection = Faraday.new(url: SERVICE_ADDRESS) do |conn| conn.response :json conn.adapter Faraday.default_adapter end end |
Instance Attribute Details
#version ⇒ String (readonly)
Returns Exchangerate api version.
28 29 30 |
# File 'lib/exchangerate_api/client.rb', line 28 def version @version end |
Instance Method Details
#currency_codes ⇒ Hash
List all supported currency codes with countries
60 61 62 |
# File 'lib/exchangerate_api/client.rb', line 60 def currency_codes CurrencyCodes::CODES end |
#rates_for(currency_code) ⇒ ExchangerateApi::Result
Fetch exchage rate data by currency code
49 50 51 52 53 54 55 |
# File 'lib/exchangerate_api/client.rb', line 49 def rates_for(currency_code) resp = @connection.get("/#{version}/latest/#{currency_code}") return Result.new(resp.body) if resp.status == 200 raise ExchangerateApi::Error.new(status: resp.status, message: resp.body) end |
#rates_for_usd ⇒ ExchangerateApi::Result
Get exchage rate for usd
67 68 69 |
# File 'lib/exchangerate_api/client.rb', line 67 def rates_for_usd rates_for 'USD' end |