Module: CurrencySwitcher

Defined in:
lib/currency_switcher.rb,
lib/currency_switcher/currencies.rb

Constant Summary collapse

URL =

URL for working out the exchange rate

"https://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

Class Method Summary collapse

Class Attribute Details

.from_currencyObject (readonly)

Get ‘from’ currency

Returns the Symbol of ‘from’ currency



19
20
21
# File 'lib/currency_switcher.rb', line 19

def from_currency
  @from_currency
end

.to_currencyObject (readonly)

Get ‘to’ currency

Returns the Symbol of ‘to’ currency



24
25
26
# File 'lib/currency_switcher.rb', line 24

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

Returns:

  • (Boolean)


55
56
57
# File 'lib/currency_switcher.rb', line 55

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

Raises:

  • (StandardError)


92
93
94
95
96
97
98
99
100
# File 'lib/currency_switcher.rb', line 92

def self.calculate_value(fixnum)
  ex_rate = exchange_rate
  raise StandardError, "Could not work out the result" if ex_rate.nil?

  if ex_rate
    value = "%.2f" % (ex_rate * fixnum)
    value.to_f
  end
end

.currenciesObject

Print the available list of currencies

Returns the String with all the currencies



129
130
131
# File 'lib/currency_switcher.rb', line 129

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

Raises:

  • (StandardError)


71
72
73
74
75
76
77
78
79
# File 'lib/currency_switcher.rb', line 71

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_rateObject

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



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/currency_switcher.rb', line 106

def self.exchange_rate
  stream = open(self.link, 'Accept-Encoding' => 'gzip')

  if (stream.content_encoding.empty?)
    body = stream.read
  else
    body = Zlib::GzipReader.new(stream).read
  end

  doc = Nokogiri::HTML(body)
  doc.css('#ctl00_M_lblToAmount').text.to_f
end

Create a customized URL for ‘from’ and ‘to’ currencies

Returns the String representing a valid URL



122
123
124
# File 'lib/currency_switcher.rb', line 122

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

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/currency_switcher.rb', line 41

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