Class: NbuCurrency
- Inherits:
-
Money::Bank::VariableExchange
- Object
- Money::Bank::VariableExchange
- NbuCurrency
- Defined in:
- lib/nbu_currency.rb,
lib/nbu_currency/version.rb
Constant Summary collapse
- CURRENCIES =
%w(USD CAD EUR GBP UAH).map(&:freeze).freeze
- NBU_RATES_URL =
'https://privat24.privatbank.ua/p24/accountorder?oper=prp&PUREXML&apicour&country=ua&full'.freeze
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#last_updated ⇒ Object
Returns the value of attribute last_updated.
-
#rates_updated_at ⇒ Object
Returns the value of attribute rates_updated_at.
Instance Method Summary collapse
- #exchange(cents, from_currency, to_currency) ⇒ Object
- #exchange_with(from, to_currency) ⇒ Object
- #get_rate(from, to, opts = {}) ⇒ Object
- #save_rates(cache, url = NBU_RATES_URL) ⇒ Object
- #save_rates_to_s(url = NBU_RATES_URL) ⇒ Object
- #set_rate(from, to, rate, opts = {}) ⇒ Object
- #update_rates(cache = nil) ⇒ Object
- #update_rates_from_s(content) ⇒ Object
Instance Attribute Details
#last_updated ⇒ Object
Returns the value of attribute last_updated.
10 11 12 |
# File 'lib/nbu_currency.rb', line 10 def last_updated @last_updated end |
#rates_updated_at ⇒ Object
Returns the value of attribute rates_updated_at.
11 12 13 |
# File 'lib/nbu_currency.rb', line 11 def rates_updated_at @rates_updated_at end |
Instance Method Details
#exchange(cents, from_currency, to_currency) ⇒ Object
36 37 38 |
# File 'lib/nbu_currency.rb', line 36 def exchange(cents, from_currency, to_currency) exchange_with(Money.new(cents, from_currency), to_currency) end |
#exchange_with(from, to_currency) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/nbu_currency.rb', line 40 def exchange_with(from, to_currency) from_base_rate, to_base_rate = nil, nil rate = get_rate(from, to_currency) unless rate @mutex.synchronize do opts = { without_mutex: true } from_base_rate = get_rate("UAH", from.currency.to_s, opts) to_base_rate = get_rate("UAH", to_currency, opts) end rate = to_base_rate / from_base_rate end calculate_exchange(from, to_currency, rate) end |
#get_rate(from, to, opts = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/nbu_currency.rb', line 56 def get_rate(from, to, opts = {}) fn = -> { @rates[rate_key_for(from, to, opts)] } if opts[:without_mutex] fn.call else @mutex.synchronize { fn.call } end end |
#save_rates(cache, url = NBU_RATES_URL) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/nbu_currency.rb', line 20 def save_rates(cache, url=NBU_RATES_URL) raise InvalidCache unless cache File.open(cache, "w") do |file| io = open(url); io.each_line { |line| file.puts line } end end |
#save_rates_to_s(url = NBU_RATES_URL) ⇒ Object
32 33 34 |
# File 'lib/nbu_currency.rb', line 32 def save_rates_to_s(url=NBU_RATES_URL) open(url).read end |
#set_rate(from, to, rate, opts = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/nbu_currency.rb', line 66 def set_rate(from, to, rate, opts = {}) fn = -> { @rates[rate_key_for(from, to, opts)] = rate } if opts[:without_mutex] fn.call else @mutex.synchronize { fn.call } end end |
#update_rates(cache = nil) ⇒ Object
16 17 18 |
# File 'lib/nbu_currency.rb', line 16 def update_rates(cache=nil) update_parsed_rates(doc(cache)) end |
#update_rates_from_s(content) ⇒ Object
28 29 30 |
# File 'lib/nbu_currency.rb', line 28 def update_rates_from_s(content) update_parsed_rates(doc_from_s(content)) end |