class CurrencyConverter::XE
Attributes
from_currency[R]
Returns the Symbol of 'from' currency
to_currency[R]
Returns the Symbol of 'to' currency
Public Instance Methods
exchange(from, to, fixnum)
click to toggle source
Receive the amount of you desire currency.
@param [String, String, Numeric] other currency to exchange to.
@return [amount]
@example currency_converter = CurrencyConverter::XE.new currency_converter.exchange('USD', 'EUR', 100) currency_converter.exchange('USD', 'INR', 100)
# File lib/currency_converter/xe.rb, line 24 def exchange(from, to, fixnum) @from_currency = from.upcase.to_sym @to_currency = to.upcase.to_sym validate_currency ex_rate = exchange_rate validate_rate(ex_rate) ex_rate.to_f * fixnum end
Private Instance Methods
exchange_rate()
click to toggle source
Returns the Float value of rate or nil
# File lib/currency_converter/xe.rb, line 40 def exchange_rate url = "/currencyconverter/convert/?Amount=1&From=#{from_currency.to_s.upcase}&To=#{to_currency.to_s.upcase}" uri = URI.parse('https://www.xe.com') request = Net::HTTP.new(uri.host, uri.port) request.use_ssl = true response = request.get(url) doc = Nokogiri::HTML(response.body) result = doc.css('span.uccResultAmount').text regexp = Regexp.new('(\\d+(?:\\.\\d+)?)') regexp.match result return $1 rescue Timeout::Error raise StandardError, 'Please check your internet connection' end
validate_currency()
click to toggle source
# File lib/currency_converter/xe.rb, line 59 def validate_currency raise CurrencyConverter::UnknownCurrency.new(from_currency) unless CurrencyConverter::CURRENCIES.has_key?(from_currency) raise CurrencyConverter::UnknownCurrency.new(to_currency) unless CurrencyConverter::CURRENCIES.has_key?(to_currency) end
validate_rate(rate)
click to toggle source
# File lib/currency_converter/xe.rb, line 64 def validate_rate(rate) raise CurrencyConverter::MissingExchangeRate.new(from_currency, to_currency) if rate.to_f.zero? end