Class: Money::Bank::MNB
- Inherits:
-
VariableExchange
- Object
- VariableExchange
- Money::Bank::MNB
- Defined in:
- lib/money/bank/mnb.rb
Constant Summary collapse
- WSDL_URL =
'http://www.mnb.hu/arfolyamok.asmx?WSDL'
- VERSION =
File.read(File.('../../../VERSION', File.dirname(__FILE__)))
Instance Attribute Summary collapse
-
#rates ⇒ Object
readonly
Returns the value of attribute rates.
Instance Method Summary collapse
- #currencies ⇒ Object
- #fetch_currencies_from_wsdl ⇒ Object
- #fetch_rate(from, to) ⇒ Object
- #fetch_rates_from_wsdl ⇒ Object
- #flush_rate(from, to) ⇒ Object
- #flush_rates ⇒ Object
- #get_rate(from, to) ⇒ Object
- #mnb_client ⇒ Object
- #setup ⇒ Object
Instance Attribute Details
#rates ⇒ Object (readonly)
Returns the value of attribute rates.
12 13 14 |
# File 'lib/money/bank/mnb.rb', line 12 def rates @rates end |
Instance Method Details
#currencies ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/money/bank/mnb.rb', line 73 def currencies @currencies ||= [] return @currencies unless @currencies.empty? doc = Nokogiri::XML.parse fetch_currencies_from_wsdl @currencies = doc.css('Curr').collect(&:text) @currencies << 'HUF' @currencies end |
#fetch_currencies_from_wsdl ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/money/bank/mnb.rb', line 96 def fetch_currencies_from_wsdl rsp = mnb_client.call :get_info raise UnknownRate, "Bank returned with an error response" unless rsp.success? rsp.body[:get_info_response][:get_info_result] end |
#fetch_rate(from, to) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/money/bank/mnb.rb', line 38 def fetch_rate(from, to) from, to = Currency.wrap(from).iso_code.to_s, Currency.wrap(to).iso_code.to_s # Providing usable error messages raise UnknownRate, "Source currency '#{from}' is not known by this module" unless currencies.include?(from) raise UnknownRate, "Target currency '#{to}' is not known by this module" unless currencies.include?(to) rsp = fetch_rates_from_wsdl doc = Nokogiri::XML.parse(rsp) ## Rates are specified in HUF, so we have to lookup both side before convert ## If any currency is HUF then it will be constant 1 from_rate = from == 'HUF' ? 1 : 0 to_rate = to == 'HUF' ? 1 : 0 doc.css('Rate').each do |rate| curr = rate['curr'] next unless [from, to].include?(curr) ## XXX Bank provides exchange rates in hungarian number notation form! value = rate.text.sub(',', '.').to_f unit = rate['unit'].to_i if curr == from from_rate = value / unit else to_rate = value / unit end end from_rate / to_rate end |
#fetch_rates_from_wsdl ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/money/bank/mnb.rb', line 88 def fetch_rates_from_wsdl rsp = mnb_client.call :get_current_exchange_rates raise UnknownRate, "Bank returned with an error response" unless rsp.success? rsp.body[:get_current_exchange_rates_response][:get_current_exchange_rates_result] end |
#flush_rate(from, to) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/money/bank/mnb.rb', line 25 def flush_rate(from, to) key = rate_key_for(from, to) mutex.synchronize do @rates.delete(key) end end |
#flush_rates ⇒ Object
19 20 21 22 23 |
# File 'lib/money/bank/mnb.rb', line 19 def flush_rates mutex.synchronize do @rates = {} end end |
#get_rate(from, to) ⇒ Object
32 33 34 35 36 |
# File 'lib/money/bank/mnb.rb', line 32 def get_rate(from, to) mutex.synchronize do @rates[rate_key_for(from, to)] ||= fetch_rate(from, to) end end |
#mnb_client ⇒ Object
84 85 86 |
# File 'lib/money/bank/mnb.rb', line 84 def mnb_client @client ||= Savon.client(:wsdl => WSDL_URL, :log => false) end |
#setup ⇒ Object
14 15 16 17 |
# File 'lib/money/bank/mnb.rb', line 14 def setup HTTPI.log = false @rates = {} end |