currency_switcher 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data.tar.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ {7�O�ǬӕI��)�"�8���w�<@j5��Tdq�������j�Q
2
+ Eq��׼eE�^q`�P��O�"�n���f�c򐥻��c��_i��G�6�$�U=I� !��)��� �XNHWb��L�P�(��\�4�h�� KB��r�?ԙ}��MX�w8k�-�ǣ�+a�DY}f�r��в��;�HxH[�7��s��an}H�uv��P�:6g�{4B������hW�7���E�U;�?E
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ README.md
2
+ Rakefile
3
+ lib/currency_switcher.rb
4
+ lib/currency_switcher/currencies.rb
5
+ lib/currency_switcher/ext/fixnum.rb
6
+ test/helper.rb
7
+ test/html/eur_to_usd.html
8
+ test/html/usd_to_gbp.html
9
+ test/test_currency_switcher.rb
10
+ Manifest
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # Currency Switcher
2
+
3
+ ## Description
4
+
5
+ This is a small library that converts currencies using exchange rates from http://exchange-rates.org web site.
6
+
7
+ ## Installation
8
+
9
+ $ gem install currency_switcher
10
+
11
+ ## Usage
12
+
13
+ From the available list of currencies you can create methods in a format specified below and call them on Fixnums:
14
+
15
+ 'from_currency'_to_'to_currency'
16
+
17
+ For example:
18
+
19
+ require 'currency_switcher'
20
+ 3.usd_to_gbp # => converts 3 USD to GBP and returns 1.94
21
+ 15.eur_to_aud # => converts 15 EURO to Australian Dollar and returns 19.61
22
+
23
+ ## Available Currencies
24
+
25
+ aed => United Arab Emirates Dirham
26
+ amd => Armenian Dram
27
+ ang => Netherlands Antillian Guilder
28
+ ars => Argentine Peso
29
+ aud => Australian Dollar
30
+ bbd => Barbados Dollar
31
+ bdt => Bangladeshi Taka
32
+ bgn => Bulgarian Lev
33
+ bhd => Bahraini Dinar
34
+ bif => Burundi Franc
35
+ bmd => Bermudian Dollar
36
+ bnd => Brunei Dollar
37
+ bob => Bolivian Boliviano
38
+ brl => Brazilian Real
39
+ bsd => Bahamian Dollar
40
+ bwp => Botswana Pula
41
+ byr => Belarusian Ruble
42
+ bzd => Belize Dollar
43
+ cad => Canadian Dollar
44
+ chf => Swiss Franc
45
+ clp => Chilean Peso
46
+ cny => Chinese Yuan Renminbi
47
+ cop => Colombian Peso
48
+ crc => Costa Rican Colon
49
+ cup => Cuban Peso
50
+ cve => Cape Verde Escudo
51
+ czk => Czech Koruna
52
+ djf => Djibouti Franc
53
+ dkk => Danish Krone
54
+ dop => Dominican Peso
55
+ dzd => Algerian Dinar
56
+ eek => Estonian Kroon
57
+ egp => Egyptian Pound
58
+ etb => Ethiopian Birr
59
+ eur => Euro
60
+ fjd => Fiji Dollar
61
+ gbp => British Pound
62
+ ghs => Ghanaian Cedi
63
+ gmd => Gambian Dalasi
64
+ gtq => Guatemalan Quetzal
65
+ hkd => Hong Kong Dollar
66
+ hnl => Honduran Lempira
67
+ hrk => Croatian Kuna
68
+ htg => Haitian Gourde
69
+ huf => Hungarian Forint
70
+ idr => Indonesian Rupiah
71
+ ils => Israeli New Shekel
72
+ inr => Indian Rupee
73
+ iqd => Iraqi Dinar
74
+ irr => Iranian Rial
75
+ isk => Iceland Krona
76
+ jmd => Jamaican Dollar
77
+ jod => Jordanian Dinar
78
+ jpy => Japanese Yen
79
+ kes => Kenyan Shilling
80
+ khr => Cambodian Riel
81
+ krw => Korean Won
82
+ kwd => Kuwaiti Dinar
83
+ kyd => Cayman Islands Dollar
84
+ kzt => Kazakhstan Tenge
85
+ lak => Lao Kip
86
+ lbp => Lebanese Pound
87
+ lkr => Sri Lanka Rupee
88
+ lsl => Lesotho Loti
89
+ ltl => Lithuanian Litas
90
+ lvl => Latvian Lats
91
+ lyd => Libyan Dinar
92
+ mad => Moroccan Dirham
93
+ mdl => Moldovan Leu
94
+ mmk => Myanmar Kyat
95
+ mop => Macau Pataca
96
+ mur => Mauritius Rupee
97
+ mwk => Malawi Kwacha
98
+ mxn => Mexican Peso
99
+ myr => Malaysian Ringgit
100
+ ngn => Nigerian Naira
101
+ nio => Nicaraguan Cordoba Oro
102
+ nok => Norwegian Krone
103
+ npr => Nepalese Rupee
104
+ nzd => New Zealand Dollar
105
+ omr => Omani Rial
106
+ pab => Panamanian Balboa
107
+ pen => Peruvian Nuevo Sol
108
+ php => Philippine Peso
109
+ pkr => Pakistan Rupee
110
+ pln => Polish Zloty
111
+ pyg => Paraguay Guarani
112
+ qar => Qatari Rial
113
+ ron => Romanian Leu
114
+ rsd => Serbian Dinar
115
+ rub => Russian Ruble
116
+ rwf => Rwanda Franc
117
+ sar => Saudi Riyal
118
+ scr => Seychelles Rupee
119
+ sdd => Sudanese Dinar
120
+ sek => Swedish Krona
121
+ sgd => Singapore Dollar
122
+ sos => Somali Shilling
123
+ syp => Syrian Pound
124
+ szl => Swaziland Lilangeni
125
+ thb => Thai Baht
126
+ tnd => Tunisian Dinar
127
+ try => Turkish Lira
128
+ ttd => Trinidad and Tobago Dollar
129
+ twd => Taiwan Dollar
130
+ tzs => Tanzanian Shilling
131
+ uah => Ukraine Hryvnia
132
+ ugx => Uganda Shilling
133
+ usd => US Dollar
134
+ uyu => Uruguay Peso
135
+ vef => Venezuelan Bolivar
136
+ vnd => Vietnamese Dong
137
+ xaf => CFA BEAC Franc
138
+ xcd => East Caribbean Dollar
139
+ xof => CFA BCEAO Franc
140
+ xpf => CFP Franc
141
+ zar => South African Rand
142
+ zmk => Zambian Kwacha
143
+ zwd => Zimbabwe Dollar
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('currency_switcher', '0.0.1') do |p|
6
+ p.description = "Convert currencies using exchange rates from http://exchange-rates.org"
7
+ p.url = "http://github.com/staskie/currency_switcher"
8
+ p.author = "Dominik Staskiewicz"
9
+ p.email = "stadominik@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{currency_switcher}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Dominik Staskiewicz"]
9
+ s.cert_chain = ["/Users/dominik/Documents/keys/gem-public_cert.pem"]
10
+ s.date = %q{2011-01-07}
11
+ s.description = %q{Convert currencies using exchange rates from http://exchange-rates.org}
12
+ s.email = %q{stadominik@gmail.com}
13
+ s.extra_rdoc_files = ["README.md", "lib/currency_switcher.rb", "lib/currency_switcher/currencies.rb", "lib/currency_switcher/ext/fixnum.rb"]
14
+ s.files = ["README.md", "Rakefile", "lib/currency_switcher.rb", "lib/currency_switcher/currencies.rb", "lib/currency_switcher/ext/fixnum.rb", "test/helper.rb", "test/html/eur_to_usd.html", "test/html/usd_to_gbp.html", "test/test_currency_switcher.rb", "Manifest", "currency_switcher.gemspec"]
15
+ s.homepage = %q{http://github.com/staskie/currency_switcher}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Currency_switcher", "--main", "README.md"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{currency_switcher}
19
+ s.rubygems_version = %q{1.3.7}
20
+ s.signing_key = %q{/Users/dominik/Documents/keys/gem-private_key.pem}
21
+ s.summary = %q{Convert currencies using exchange rates from http://exchange-rates.org}
22
+ s.test_files = ["test/test_currency_switcher.rb"]
23
+
24
+ if s.respond_to? :specification_version then
25
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
+ s.specification_version = 3
27
+
28
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
29
+ else
30
+ end
31
+ else
32
+ end
33
+ end
@@ -0,0 +1,101 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ $:.unshift(File.join(File.dirname(__FILE__), 'currency_switcher'))
5
+
6
+ require 'currencies'
7
+ require 'ext/fixnum'
8
+
9
+ module CurrencySwitcher
10
+
11
+ # URL for working out the exchange rate
12
+ URL = "http://exchange-rates.org/converter"
13
+
14
+ class << self
15
+ # Get 'from' currency
16
+ #
17
+ # Returns the Symbol of 'from' currency
18
+ attr_reader :from_currency
19
+
20
+ # Get 'to' currency
21
+ #
22
+ # Returns the Symbol of 'to' currency
23
+ attr_reader :to_currency
24
+ end
25
+
26
+ # Validates if a method called on Fixnum is a currency
27
+ # exchange method
28
+ #
29
+ # method - The String representing the method's name
30
+ #
31
+ # Examples
32
+ #
33
+ # CurrencySwitcher.valid_exchange_method?(usd_to_eur)
34
+ # # => true
35
+ #
36
+ # CurrencySwitcher.valid_exchange_method?(usd_to_fakecurrency)
37
+ # # => false
38
+ #
39
+ # Returns true if a method is valid or false if the method is invalid
40
+ def self.valid_exchange_method?(method)
41
+ if method =~ /(.+)_to_(.+)/
42
+ @from_currency = $1.to_sym
43
+ @to_currency = $2.to_sym
44
+ @method = method
45
+
46
+ both_currencies_valid? ? true : false
47
+ end
48
+ end
49
+
50
+ # Check if 'from' and 'to' currencies are valid. They have to be defined
51
+ # in CURRENCIES hash
52
+ #
53
+ # Returns true if botch currencies are valid or false if one of them is invalid
54
+ def self.both_currencies_valid?
55
+ CURRENCIES.has_key?(from_currency) && CURRENCIES.has_key?(to_currency)
56
+ end
57
+
58
+
59
+ # Calculate the value from fixnum and exchange rate
60
+ #
61
+ # fixnum - The Fixnum representing how much should be converted
62
+ #
63
+ # Examples
64
+ #
65
+ # CurrencySwitcher.calculate_value(3)
66
+ # # => 5.23
67
+ #
68
+ # Returns float value of fixnum multiplied by exchange rate
69
+ # Raises StandardError if exchange rate is nil
70
+ def self.calculate_value(fixnum)
71
+ ex_rate = exchange_rate
72
+ raise StandardError, "Could not work out the result" if ex_rate.nil?
73
+
74
+ value = "%.2f" % (ex_rate * fixnum)
75
+ value.to_f
76
+ end
77
+
78
+ # Work out the exchange rate for a given URL.
79
+ # Parse the response and extract the value
80
+ #
81
+ # Returns the Float value of exchange rate or nil
82
+ def self.exchange_rate
83
+ doc = Nokogiri::HTML(open(self.link), nil, 'UTF-8')
84
+ doc.css('#ctl00_M_lblToAmount').text.to_f
85
+ rescue
86
+ end
87
+
88
+ # Create a customized URL for 'from' and 'to' currencies
89
+ #
90
+ # Returns the String representing a valid URL
91
+ def self.link
92
+ "#{URL}/#{from_currency.to_s.upcase}/#{to_currency.to_s.upcase}/1"
93
+ end
94
+
95
+ # Print the available list of currencies
96
+ #
97
+ # Returns the String with all the currencies
98
+ def self.currencies
99
+ CURRENCIES.keys.sort.each { |symbol| puts "#{symbol} => #{CURRENCIES[symbol]}"}
100
+ end
101
+ end
@@ -0,0 +1,124 @@
1
+ module CurrencySwitcher
2
+ # Available currencies with descriptions
3
+ CURRENCIES = {
4
+ :dzd => "Algerian Dinar",
5
+ :ars => "Argentine Peso",
6
+ :amd => "Armenian Dram",
7
+ :aud => "Australian Dollar",
8
+ :bsd => "Bahamian Dollar",
9
+ :bhd => "Bahraini Dinar",
10
+ :bdt => "Bangladeshi Taka",
11
+ :bbd => "Barbados Dollar",
12
+ :byr => "Belarusian Ruble",
13
+ :bzd => "Belize Dollar",
14
+ :bmd => "Bermudian Dollar",
15
+ :bob => "Bolivian Boliviano",
16
+ :bwp => "Botswana Pula",
17
+ :brl => "Brazilian Real",
18
+ :gbp => "British Pound",
19
+ :bnd => "Brunei Dollar",
20
+ :bgn => "Bulgarian Lev",
21
+ :bif => "Burundi Franc",
22
+ :khr => "Cambodian Riel",
23
+ :cad => "Canadian Dollar",
24
+ :cve => "Cape Verde Escudo",
25
+ :kyd => "Cayman Islands Dollar",
26
+ :xof => "CFA BCEAO Franc",
27
+ :xaf => "CFA BEAC Franc",
28
+ :xpf => "CFP Franc",
29
+ :clp => "Chilean Peso",
30
+ :cny => "Chinese Yuan Renminbi",
31
+ :cop => "Colombian Peso",
32
+ :crc => "Costa Rican Colon",
33
+ :hrk => "Croatian Kuna",
34
+ :cup => "Cuban Peso",
35
+ :czk => "Czech Koruna",
36
+ :dkk => "Danish Krone",
37
+ :djf => "Djibouti Franc",
38
+ :dop => "Dominican Peso",
39
+ :xcd => "East Caribbean Dollar",
40
+ :egp => "Egyptian Pound",
41
+ :eek => "Estonian Kroon",
42
+ :etb => "Ethiopian Birr",
43
+ :eur => "Euro",
44
+ :fjd => "Fiji Dollar",
45
+ :gmd => "Gambian Dalasi",
46
+ :ghs => "Ghanaian Cedi",
47
+ :gtq => "Guatemalan Quetzal",
48
+ :htg => "Haitian Gourde",
49
+ :hnl => "Honduran Lempira",
50
+ :hkd => "Hong Kong Dollar",
51
+ :huf => "Hungarian Forint",
52
+ :isk => "Iceland Krona",
53
+ :inr => "Indian Rupee",
54
+ :idr => "Indonesian Rupiah",
55
+ :irr => "Iranian Rial",
56
+ :iqd => "Iraqi Dinar",
57
+ :ils => "Israeli New Shekel",
58
+ :jmd => "Jamaican Dollar",
59
+ :jpy => "Japanese Yen",
60
+ :jod => "Jordanian Dinar",
61
+ :kzt => "Kazakhstan Tenge",
62
+ :kes => "Kenyan Shilling",
63
+ :krw => "Korean Won",
64
+ :kwd => "Kuwaiti Dinar",
65
+ :lak => "Lao Kip",
66
+ :lvl => "Latvian Lats",
67
+ :lbp => "Lebanese Pound",
68
+ :lsl => "Lesotho Loti",
69
+ :lyd => "Libyan Dinar",
70
+ :ltl => "Lithuanian Litas",
71
+ :mop => "Macau Pataca",
72
+ :mwk => "Malawi Kwacha",
73
+ :myr => "Malaysian Ringgit",
74
+ :mur => "Mauritius Rupee",
75
+ :mxn => "Mexican Peso",
76
+ :mdl => "Moldovan Leu",
77
+ :mad => "Moroccan Dirham",
78
+ :mmk => "Myanmar Kyat",
79
+ :npr => "Nepalese Rupee",
80
+ :ang => "Netherlands Antillian Guilder",
81
+ :nzd => "New Zealand Dollar",
82
+ :nio => "Nicaraguan Cordoba Oro",
83
+ :ngn => "Nigerian Naira",
84
+ :nok => "Norwegian Krone",
85
+ :omr => "Omani Rial",
86
+ :pkr => "Pakistan Rupee",
87
+ :pab => "Panamanian Balboa",
88
+ :pyg => "Paraguay Guarani",
89
+ :pen => "Peruvian Nuevo Sol",
90
+ :php => "Philippine Peso",
91
+ :pln => "Polish Zloty",
92
+ :qar => "Qatari Rial",
93
+ :ron => "Romanian Leu",
94
+ :rub => "Russian Ruble",
95
+ :rwf => "Rwanda Franc",
96
+ :sar => "Saudi Riyal",
97
+ :rsd => "Serbian Dinar",
98
+ :scr => "Seychelles Rupee",
99
+ :sgd => "Singapore Dollar",
100
+ :sos => "Somali Shilling",
101
+ :zar => "South African Rand",
102
+ :lkr => "Sri Lanka Rupee",
103
+ :sdd => "Sudanese Dinar",
104
+ :szl => "Swaziland Lilangeni",
105
+ :sek => "Swedish Krona",
106
+ :chf => "Swiss Franc",
107
+ :syp => "Syrian Pound",
108
+ :twd => "Taiwan Dollar",
109
+ :tzs => "Tanzanian Shilling",
110
+ :thb => "Thai Baht",
111
+ :ttd => "Trinidad and Tobago Dollar",
112
+ :tnd => "Tunisian Dinar",
113
+ :try => "Turkish Lira",
114
+ :ugx => "Uganda Shilling",
115
+ :uah => "Ukraine Hryvnia",
116
+ :aed => "United Arab Emirates Dirham",
117
+ :uyu => "Uruguay Peso",
118
+ :usd => "US Dollar",
119
+ :vef => "Venezuelan Bolivar",
120
+ :vnd => "Vietnamese Dong",
121
+ :zmk => "Zambian Kwacha",
122
+ :zwd => "Zimbabwe Dollar"
123
+ }
124
+ end
@@ -0,0 +1,23 @@
1
+ class Numeric
2
+ # Overriding method_missing to add CurrencySwitcher methods
3
+ #
4
+ # sym - standard method_missing argument
5
+ # *args - standard method_missing argument
6
+ # &block - standard method_missing argument
7
+ #
8
+ # Examples
9
+ #
10
+ # 3.usd_to_eur
11
+ # 1200.gbp_to_pln
12
+ #
13
+ # Returns Float value of currency exchange result
14
+ # Raises NoMethodError if couldn't find a method
15
+ def method_missing(sym, *args, &block)
16
+ # Check if the missing method applies to CurrencySwitcher module
17
+ if CurrencySwitcher.valid_exchange_method?(sym)
18
+ return CurrencySwitcher.calculate_value(self)
19
+ else
20
+ super.method_missing(sym, *args, &block)
21
+ end
22
+ end
23
+ end