class Money::Bank::XeCurrency
Constants
- SERVICE_HOST
- SERVICE_PATH
Attributes
@return [Time] Returns the time when the rates expire.
@return [Integer] Returns the Time To Live (TTL) in seconds.
@return [Hash] Stores the currently known rates.
Public Class Methods
# File lib/money/bank/xe_currency.rb, line 46 def initialize(*) super @store.extend Money::RatesStore::RateRemovalSupport end
Set the rates expiration TTL seconds from the current time.
@return [Time] The next expiration.
# File lib/money/bank/xe_currency.rb, line 41 def refresh_rates_expiration! @rates_expiration = Time.now + ttl_in_seconds end
Set the Time To Live (TTL) in seconds.
@param [Integer] the seconds between an expiration and another.
# File lib/money/bank/xe_currency.rb, line 32 def ttl_in_seconds=(value) @ttl_in_seconds = value refresh_rates_expiration! if ttl_in_seconds end
Public Instance Methods
Flushes all the rates if they are expired.
@return [Boolean]
# File lib/money/bank/xe_currency.rb, line 104 def expire_rates if self.class.ttl_in_seconds && self.class.rates_expiration <= Time.now flush_rates self.class.refresh_rates_expiration! true else false end end
Clears the specified rate stored in @rates.
@param [String, Symbol, Currency] from Currency to convert from (used
for key into @rates).
@param [String, Symbol, Currency] to Currency to convert to (used for
key into @rates).
@return [Float] The flushed rate.
@example
@bank = XeCurrency.new #=> <Money::Bank::XeCurrency...> @bank.get_rate(:USD, :EUR) #=> 0.776337241 @bank.flush_rate(:USD, :EUR) #=> 0.776337241
# File lib/money/bank/xe_currency.rb, line 78 def flush_rate(from, to) store.remove_rate(from, to) end
Clears all rates stored in @rates
@return [Hash] The empty @rates Hash.
@example
@bank = XeCurrency.new #=> <Money::Bank::XeCurrency...> @bank.get_rate(:USD, :EUR) #=> 0.776337241 @bank.flush_rates #=> {}
# File lib/money/bank/xe_currency.rb, line 60 def flush_rates store.clear_rates end
Returns the requested rate.
It also flushes all the rates when and if they are expired.
@param [String, Symbol, Currency] from Currency to convert from @param [String, Symbol, Currency] to Currency to convert to
@return [Float] The requested rate.
@example
@bank = XeCurrency.new #=> <Money::Bank::XeCurrency...> @bank.get_rate(:USD, :EUR) #=> 0.776337241
# File lib/money/bank/xe_currency.rb, line 95 def get_rate(from, to) expire_rates store.get_rate(from, to) || store.add_rate(from, to, fetch_rate(from, to)) end
Private Instance Methods
Build a URI for the given arguments.
@param [Currency] from The currency to convert from. @param [Currency] to The currency to convert to.
@return [URI::HTTP]
# File lib/money/bank/xe_currency.rb, line 143 def build_uri(from, to) uri = URI::HTTP.build( :host => SERVICE_HOST, :path => SERVICE_PATH, :query => "Amount=1&From=#{from.iso_code}&To=#{to.iso_code}" ) end
Takes the response from Xe and extract the rate.
@param [String] data The xe rate string to decode.
@return [BigDecimal]
# File lib/money/bank/xe_currency.rb, line 157 def extract_rate(data) rate = ::Nokogiri::HTML(data).css('.uccResultAmount')[0].inner_text BigDecimal(rate) end
Queries for the requested rate and returns it.
@param [String, Symbol, Currency] from Currency to convert from @param [String, Symbol, Currency] to Currency to convert to
@return [BigDecimal] The requested rate.
# File lib/money/bank/xe_currency.rb, line 123 def fetch_rate(from, to) from, to = Currency.wrap(from), Currency.wrap(to) data = build_uri(from, to).read rate = extract_rate(data); if (rate < 0.1) rate = 1/extract_rate(build_uri(to, from).read) end rate end