Module: CurrencySwitcher
- Defined in:
- lib/currency_switcher.rb,
lib/currency_switcher/currencies.rb
Constant Summary collapse
- URL =
URL for working out the exchange rate
"http://exchange-rates.org/converter"
- CURRENCIES =
Available currencies with descriptions
{ :dzd => "Algerian Dinar", :ars => "Argentine Peso", :amd => "Armenian Dram", :aud => "Australian Dollar", :bsd => "Bahamian Dollar", :bhd => "Bahraini Dinar", :bdt => "Bangladeshi Taka", :bbd => "Barbados Dollar", :byr => "Belarusian Ruble", :bzd => "Belize Dollar", :bmd => "Bermudian Dollar", :bob => "Bolivian Boliviano", :bwp => "Botswana Pula", :brl => "Brazilian Real", :gbp => "British Pound", :bnd => "Brunei Dollar", :bgn => "Bulgarian Lev", :bif => "Burundi Franc", :khr => "Cambodian Riel", :cad => "Canadian Dollar", :cve => "Cape Verde Escudo", :kyd => "Cayman Islands Dollar", :xof => "CFA BCEAO Franc", :xaf => "CFA BEAC Franc", :xpf => "CFP Franc", :clp => "Chilean Peso", :cny => "Chinese Yuan Renminbi", :cop => "Colombian Peso", :crc => "Costa Rican Colon", :hrk => "Croatian Kuna", :cup => "Cuban Peso", :czk => "Czech Koruna", :dkk => "Danish Krone", :djf => "Djibouti Franc", :dop => "Dominican Peso", :xcd => "East Caribbean Dollar", :egp => "Egyptian Pound", :eek => "Estonian Kroon", :etb => "Ethiopian Birr", :eur => "Euro", :fjd => "Fiji Dollar", :gmd => "Gambian Dalasi", :ghs => "Ghanaian Cedi", :gtq => "Guatemalan Quetzal", :htg => "Haitian Gourde", :hnl => "Honduran Lempira", :hkd => "Hong Kong Dollar", :huf => "Hungarian Forint", :isk => "Iceland Krona", :inr => "Indian Rupee", :idr => "Indonesian Rupiah", :irr => "Iranian Rial", :iqd => "Iraqi Dinar", :ils => "Israeli New Shekel", :jmd => "Jamaican Dollar", :jpy => "Japanese Yen", :jod => "Jordanian Dinar", :kzt => "Kazakhstan Tenge", :kes => "Kenyan Shilling", :krw => "Korean Won", :kwd => "Kuwaiti Dinar", :lak => "Lao Kip", :lvl => "Latvian Lats", :lbp => "Lebanese Pound", :lsl => "Lesotho Loti", :lyd => "Libyan Dinar", :ltl => "Lithuanian Litas", :mop => "Macau Pataca", :mwk => "Malawi Kwacha", :myr => "Malaysian Ringgit", :mur => "Mauritius Rupee", :mxn => "Mexican Peso", :mdl => "Moldovan Leu", :mad => "Moroccan Dirham", :mmk => "Myanmar Kyat", :npr => "Nepalese Rupee", :ang => "Netherlands Antillian Guilder", :nzd => "New Zealand Dollar", :nio => "Nicaraguan Cordoba Oro", :ngn => "Nigerian Naira", :nok => "Norwegian Krone", :omr => "Omani Rial", :pkr => "Pakistan Rupee", :pab => "Panamanian Balboa", :pyg => "Paraguay Guarani", :pen => "Peruvian Nuevo Sol", :php => "Philippine Peso", :pln => "Polish Zloty", :qar => "Qatari Rial", :ron => "Romanian Leu", :rub => "Russian Ruble", :rwf => "Rwanda Franc", :sar => "Saudi Riyal", :rsd => "Serbian Dinar", :scr => "Seychelles Rupee", :sgd => "Singapore Dollar", :sos => "Somali Shilling", :zar => "South African Rand", :lkr => "Sri Lanka Rupee", :sdd => "Sudanese Dinar", :szl => "Swaziland Lilangeni", :sek => "Swedish Krona", :chf => "Swiss Franc", :syp => "Syrian Pound", :twd => "Taiwan Dollar", :tzs => "Tanzanian Shilling", :thb => "Thai Baht", :ttd => "Trinidad and Tobago Dollar", :tnd => "Tunisian Dinar", :try => "Turkish Lira", :ugx => "Uganda Shilling", :uah => "Ukraine Hryvnia", :aed => "United Arab Emirates Dirham", :uyu => "Uruguay Peso", :usd => "US Dollar", :vef => "Venezuelan Bolivar", :vnd => "Vietnamese Dong", :zmk => "Zambian Kwacha", :zwd => "Zimbabwe Dollar" }
Class Attribute Summary collapse
-
.from_currency ⇒ Object
readonly
Get ‘from’ currency.
-
.to_currency ⇒ Object
readonly
Get ‘to’ currency.
Class Method Summary collapse
-
.both_currencies_valid? ⇒ Boolean
Check if ‘from’ and ‘to’ currencies are valid.
-
.calculate_value(fixnum) ⇒ Object
Calculate the value from fixnum.
-
.currencies ⇒ Object
Print the available list of currencies.
-
.exchange(fixnum, from, to) ⇒ Object
Calculate the value from fixnum, from and to currency.
-
.exchange_rate ⇒ Object
Work out the exchange rate for a given URL.
-
.link ⇒ Object
Create a customized URL for ‘from’ and ‘to’ currencies.
-
.valid_exchange_method?(method) ⇒ Boolean
Validates if a method called on Fixnum is a currency exchange method.
Class Attribute Details
.from_currency ⇒ Object (readonly)
Get ‘from’ currency
Returns the Symbol of ‘from’ currency
18 19 20 |
# File 'lib/currency_switcher.rb', line 18 def from_currency @from_currency end |
.to_currency ⇒ Object (readonly)
Get ‘to’ currency
Returns the Symbol of ‘to’ currency
23 24 25 |
# File 'lib/currency_switcher.rb', line 23 def to_currency @to_currency end |
Class Method Details
.both_currencies_valid? ⇒ Boolean
Check if ‘from’ and ‘to’ currencies are valid. They have to be defined in CURRENCIES hash
Returns true if botch currencies are valid or false if one of them is invalid
54 55 56 |
# File 'lib/currency_switcher.rb', line 54 def self.both_currencies_valid? self.from_currency_valid? && self.to_currency_valid? end |
.calculate_value(fixnum) ⇒ Object
Calculate the value from fixnum
fixnum - The Fixnum representing how much should be converted
Examples
CurrencySwitcher.calculate_value(3)
# => 5.23
Returns float value of fixnum multiplied by exchange rate Raises StandardError if exchange rate is nil
91 92 93 94 95 96 97 |
# File 'lib/currency_switcher.rb', line 91 def self.calculate_value(fixnum) ex_rate = exchange_rate raise StandardError, "Could not work out the result" if ex_rate.nil? value = "%.2f" % (ex_rate * fixnum) value.to_f end |
.currencies ⇒ Object
Print the available list of currencies
Returns the String with all the currencies
119 120 121 |
# File 'lib/currency_switcher.rb', line 119 def self.currencies CURRENCIES.keys.sort.each { |symbol| puts "#{symbol} => #{CURRENCIES[symbol]}"} end |
.exchange(fixnum, from, to) ⇒ Object
Calculate the value from fixnum, from and to currency
fixnum - The Fixnum representing how much should be converted from - From currency to - To currency
Examples
CurrencySwitcher.exchange(3,"usd", "gbp")
# => 5.23
Returns float value of fixnum multiplied by exchange rate Raises StandardError if currencies are not valid
70 71 72 73 74 75 76 77 78 |
# File 'lib/currency_switcher.rb', line 70 def self.exchange(fixnum, from, to) @from_currency = from.to_sym @to_currency = to.to_sym raise StandardError, "From currency #{from_currency} is invalid" unless self.from_currency_valid? raise StandardError, "To currency #{to_currency} is invalid" unless self.to_currency_valid? self.calculate_value(fixnum) end |
.exchange_rate ⇒ Object
Work out the exchange rate for a given URL. Parse the response and extract the value
Returns the Float value of exchange rate or nil
103 104 105 106 107 |
# File 'lib/currency_switcher.rb', line 103 def self.exchange_rate doc = Nokogiri::HTML(open(self.link), nil, 'UTF-8') doc.css('#ctl00_M_lblToAmount').text.to_f rescue end |
.link ⇒ Object
Create a customized URL for ‘from’ and ‘to’ currencies
Returns the String representing a valid URL
112 113 114 |
# File 'lib/currency_switcher.rb', line 112 def self.link "#{URL}/#{from_currency.to_s.upcase}/#{to_currency.to_s.upcase}/1" end |
.valid_exchange_method?(method) ⇒ Boolean
Validates if a method called on Fixnum is a currency exchange method
method - The String representing the method’s name
Examples
CurrencySwitcher.valid_exchange_method?(usd_to_eur)
# => true
CurrencySwitcher.valid_exchange_method?(usd_to_fakecurrency)
# => false
Returns true if a method is valid or false if the method is invalid
40 41 42 43 44 45 46 47 48 |
# File 'lib/currency_switcher.rb', line 40 def self.valid_exchange_method?(method) if method =~ /(.+)_to_(.+)/ @from_currency = $1.to_sym @to_currency = $2.to_sym @method = method both_currencies_valid? ? true : false end end |