class Momm::Calculator
Attributes
feed[R]
storage[R]
Public Class Methods
new(storage = Memcached.new, feed = Feeds::ECB.instance)
click to toggle source
Public Instance Methods
exchange(money, from, to, options= {})
click to toggle source
Exchange Money from one currency to another
Parameters¶ ↑
- money
-
money you have
- from
-
ruby symbol, such as :USD, :GBP
- to
-
same as above
- options
-
option parameters, contain today by default
Returns¶ ↑
money exchanged
# File lib/momm/calculator.rb, line 101 def exchange(money, from, to, options= {}) options[:date] ||= Date.today (origin_exchange_rate(from, to, options) * money).round(2) end
exchange_rate(from, to, options = {})
click to toggle source
Exchange Rate
Parameters¶ ↑
- from
-
ruby symbol, such as :USD, :GBP
- to
-
same as above
- options
-
0ption parameters, contain today by default
Returns¶ ↑
the exchange rate
# File lib/momm/calculator.rb, line 52 def exchange_rate(from, to, options = {}) origin_exchange_rate(from, to, options = {}).round(2) end
get_rate(from, date = Date.today)
click to toggle source
Delegate the get_rate
method, if the target is missing Fetching all data from remote
Parameters¶ ↑
- date
-
Default is Date.today
- currency
-
Currency passed in
Returns¶ ↑
the currency rate
# File lib/momm/calculator.rb, line 118 def get_rate(from, date = Date.today) res = get_rate_origin(from, date) return res if res != 0 && res update! get_rate_origin(from, date) end
Private Instance Methods
origin_exchange_rate(from, to, options = {})
click to toggle source
Exchange Rate without precision bound
# File lib/momm/calculator.rb, line 57 def origin_exchange_rate(from, to, options = {}) date = options[:date] || Date.today date = Date.parse(date) if date.is_a? String max_count = 10 date_counter = date # @TODO Refactoring. # It seems that currency does not have feeds at weekends, so # we simply find the closest day which has currency feeds. while max_count > 0 to_rate = get_rate(to, date_counter) from_rate = get_rate(from, date_counter) date_counter -=1 max_count -= 1 next if to_rate == 0 || from_rate == 0 set_rate(to, to_rate, date) set_rate(from, from_rate, date) return (to_rate / from_rate) end 0.0 / 0 end