Module: Lbank
- Defined in:
- lib/lbank.rb,
lib/lbank/version.rb
Defined Under Namespace
Classes: MemoryCache
Constant Summary collapse
- RATE_TYPE =
'LT'- TIMEZONE =
'Europe/Vilnius'- URL =
'http://www.lb.lt/webservices/FxRates/FxRates.asmx/getFxRates'- LTL =
'LTL'- LTL_RATE =
BigDecimal('3.45280')
- VERSION =
'1.0.0'
Class Method Summary collapse
- .cache ⇒ Object
- .cache=(store) ⇒ Object
- .cache_key(date) ⇒ Object
- .connection ⇒ Object
- .convert_currency(amount, from_currency, to_currency, date = nil) ⇒ Object
- .currency_rates(time = nil) ⇒ Object
Class Method Details
.cache ⇒ Object
17 18 19 |
# File 'lib/lbank.rb', line 17 def cache @cache ||= MemoryCache.new end |
.cache=(store) ⇒ Object
21 22 23 |
# File 'lib/lbank.rb', line 21 def cache=(store) @cache = store end |
.cache_key(date) ⇒ Object
52 53 54 |
# File 'lib/lbank.rb', line 52 def cache_key(date) "lbank-#{date}" end |
.connection ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/lbank.rb', line 56 def connection @connection ||= Faraday.new do |builder| builder.request :url_encoded builder.response :raise_error builder.response :xml, content_type: /\bxml$/ builder.adapter Faraday.default_adapter end end |
.convert_currency(amount, from_currency, to_currency, date = nil) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/lbank.rb', line 44 def convert_currency(amount, from_currency, to_currency, date = nil) rates = currency_rates(date) from_rate = rates[from_currency.to_s] to_rate = rates[to_currency.to_s] amount / from_rate * to_rate end |
.currency_rates(time = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lbank.rb', line 25 def currency_rates(time = nil) time ||= Time.now bank_time = time.to_time.in_time_zone(TIMEZONE) date = bank_time.strftime('%Y-%m-%d') cache.fetch(cache_key(date)) do response = connection.post(URL, tp: RATE_TYPE, dt: date) fx_rates = response.body['FxRates']['FxRate'] rates = { fx_rates[0]['CcyAmt'][0]['Ccy'] => 1 } rates[LTL] ||= LTL_RATE fx_rates.each_with_object(rates) do |rate, result| base, foreign = rate['CcyAmt'] result[foreign['Ccy']] = BigDecimal(foreign['Amt']) / BigDecimal(base['Amt']) end end end |