Class: Ubea::CurrencyConverter::FreeCurrencyConverterAPI
- Inherits:
-
Object
- Object
- Ubea::CurrencyConverter::FreeCurrencyConverterAPI
- Defined in:
- lib/ubea/currency_converter.rb
Constant Summary collapse
- URL =
"http://www.freecurrencyconverterapi.com/api/v2/convert?q=%s_%s&compact=y".freeze
Class Method Summary collapse
Class Method Details
.get_json(url, from, to) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/ubea/currency_converter.rb', line 77 def self.get_json(url, from, to) Retryable.retryable(tries: 3, sleep: 1) do json = open(url).read JSON.parse(json)["#{from}_#{to}"]["val"].to_s end end |
.get_rate(from, to) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ubea/currency_converter.rb', line 61 def self.get_rate(from, to) url = URL % [from, to] string = get_json(url, from, to) rate = BigDecimal.new(string) if rate <= 1E-3 Log.warn "Cannot retrieve exchange rate for #{from}#{to}, not enough precision, using the opposite pair" url = URL % [to, from] string = get_json(url, to, from) rate = BigDecimal.new(1) / BigDecimal.new(string) end rate end |