class ExchangerateApi::Client

# Client

Client to fetch currency exchange rate data.

@example

require "exchagerate_api"

client = ExchangerateApi::Client.new

client.rates_for('USD')

client.rates_for('EUR')

Constants

SERVICE_ADDRESS

Address of the exchagerate api service.

Attributes

version[R]

@return [String] Exchangerate api version.

Public Class Methods

new(version: 'v4') click to toggle source

Creates a new Client instance.

@param version [String] Version of the api. Default value is `v4`

# 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

Public Instance Methods

currency_codes() click to toggle source

List all supported currency codes with countries

@return [Hash] List of currency codes {CurrencyCodes::CODES}

# File lib/exchangerate_api/client.rb, line 60
def currency_codes
  CurrencyCodes::CODES
end
rates_for(currency_code) click to toggle source

Fetch exchage rate data by currency code

@param currency_code [String] ISO format currency code.

Supported currency codes are.

@raise [ExchangerateApi::Error] if invalid currency code or

other api errors.

@return [ExchangerateApi::Result] Exchange rate data.

# 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() click to toggle source

Get exchage rate for usd

@return [ExchangerateApi::Result] Exchange rate data.

# File lib/exchangerate_api/client.rb, line 67
def rates_for_usd
  rates_for 'USD'
end