rails_currency 1.0
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/History.txt +0 -0
- data/Manifest.txt +9 -0
- data/README.txt +99 -0
- data/Rakefile +10 -0
- data/lib/rcurrency/constants.rb +91 -0
- data/lib/rcurrency/convertor.rb +45 -0
- data/lib/rcurrency/updater.rb +0 -0
- data/lib/rcurrency.rb +7 -0
- data/test/ts_convertor.rb +20 -0
- metadata +72 -0
data/History.txt
ADDED
File without changes
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
= rcurrency
|
2
|
+
A ruby lib to real time convert among different currencies.
|
3
|
+
|
4
|
+
== Install
|
5
|
+
|
6
|
+
gem install -l rcurrency-1.0.gem
|
7
|
+
|
8
|
+
== Usage
|
9
|
+
|
10
|
+
{{{
|
11
|
+
require "rcurrency"
|
12
|
+
result = RCurrency::Convertor.convert(100, "CNY", "USD")
|
13
|
+
}}}
|
14
|
+
|
15
|
+
== Available Currency List
|
16
|
+
|
17
|
+
{{{
|
18
|
+
AFN 'Afghanistan Afghanis'
|
19
|
+
ALL 'Albania Leke'
|
20
|
+
DZD 'Algeria Dinars'
|
21
|
+
ARS 'Argentina Pesos'
|
22
|
+
AUS 'Australia Dollars'
|
23
|
+
BSD 'Bahamas Dollars'
|
24
|
+
BHD 'Bahrain Dinars'
|
25
|
+
BDT 'Bangladesh Taka'
|
26
|
+
BBD 'Barbados Dollars'
|
27
|
+
BMD 'Bermuda Dollars'
|
28
|
+
BRL 'Brazil Reais'
|
29
|
+
BGN 'Bulgaria Leva'
|
30
|
+
XOF 'CFA BCEAO Francs'
|
31
|
+
XAF 'CFA BEAC Francs'
|
32
|
+
CAD 'Canada Dollars'
|
33
|
+
CLP 'Chile Pesos'
|
34
|
+
CNY 'China Yuan Renminbi'
|
35
|
+
COP 'Colombia Pesos'
|
36
|
+
XPF 'Comptoirs Français du Pacifique Francs'
|
37
|
+
CRC 'Costa Rica Colones'
|
38
|
+
HRK 'Croatia Kuna'
|
39
|
+
CZK 'Czech Republic Koruny'
|
40
|
+
DKK 'Denmark Kroner'
|
41
|
+
DOP 'Dominican Republic Pesos'
|
42
|
+
XCD 'East Caribbean Dollars'
|
43
|
+
EGP 'Egypt Pounds'
|
44
|
+
EEK 'Estonia Krooni'
|
45
|
+
EUR 'Euro'
|
46
|
+
FJD 'Fiji Dollars'
|
47
|
+
XAU 'Gold Ounces'
|
48
|
+
HKD 'Hong Kong Dollars'
|
49
|
+
HUF 'Hungary Forint'
|
50
|
+
XDR 'IMF Special Drawing Rights'
|
51
|
+
ISK 'Iceland Kronur'
|
52
|
+
INR 'India Rupees'
|
53
|
+
IDR 'Indonesia Rupiahs'
|
54
|
+
IRR 'Iran Rials'
|
55
|
+
IQD 'Iraq Dinars'
|
56
|
+
ILS 'Israel New Shekels'
|
57
|
+
JMD 'Jamaica Dollars'
|
58
|
+
JPY 'Japan Yen'
|
59
|
+
JOD 'Jordan Dinars'
|
60
|
+
KES 'Kenya Shillings'
|
61
|
+
KWD 'Kuwait Dinars'
|
62
|
+
LBP 'Lebanon Pounds'
|
63
|
+
MYR 'Malaysia Ringgits'
|
64
|
+
MUR 'Mauritius Rupees'
|
65
|
+
MXN 'Mexico Pesos'
|
66
|
+
MAD 'Morocco Dirhams'
|
67
|
+
NZD 'New Zealand Dollars'
|
68
|
+
NOK 'Norway Kroner'
|
69
|
+
OMR 'Oman Rials'
|
70
|
+
PKR 'Pakistan Rupees'
|
71
|
+
XPD 'Palladium Ounces'
|
72
|
+
PEN 'Peru Nuevos Soles'
|
73
|
+
PHP 'Philippines Pesos'
|
74
|
+
XPT 'Platinum Ounces'
|
75
|
+
PLN 'Poland Zlotych'
|
76
|
+
QAR 'Qatar Riyals'
|
77
|
+
RON 'Romania New Lei'
|
78
|
+
RUB 'Russia Rubles'
|
79
|
+
SAR 'Saudi Arabia Riyals'
|
80
|
+
XAG 'Silver Ounces'
|
81
|
+
SGD 'Singapore Dollars'
|
82
|
+
SKK 'Slovakia Koruny'
|
83
|
+
ZAR 'South Africa Rand'
|
84
|
+
KRW 'South Korea Won'
|
85
|
+
LKR 'Sri Lanka Rupees'
|
86
|
+
SDG 'Sudan Pounds'
|
87
|
+
SEK 'Sweden Kronor'
|
88
|
+
CHF 'Switzerland Francs'
|
89
|
+
TWD 'Taiwan New Dollars'
|
90
|
+
THB 'Thailand Baht'
|
91
|
+
TTD 'Trinidad and Tobago Dollars'
|
92
|
+
TND 'Tunisia Dinars'
|
93
|
+
TRY 'Turkey New Lira'
|
94
|
+
AED 'United Arab Emirates Dirhams'
|
95
|
+
GBP 'United Kingdom Pounds'
|
96
|
+
USD 'United States Dollars'
|
97
|
+
VEF 'Venezuela Bolivares Fuertes'
|
98
|
+
VND 'Vietnam Dong'
|
99
|
+
}}}
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hoe'
|
3
|
+
|
4
|
+
Hoe.new('rails_currency', '1.0') do |p|
|
5
|
+
p.author = 'Naveen Joshi'
|
6
|
+
p.email = 'joshinav@gmail.com'
|
7
|
+
p.url = ''
|
8
|
+
p.summary = 'Real time currency converter for ruby'
|
9
|
+
p.description = 'A ruby lib to real time convert among different currencies'
|
10
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module RCurrency
|
2
|
+
module Constants
|
3
|
+
CurrencyUrl = "http://www.xe.com/ucc/convert.cgi"
|
4
|
+
Params = {"User-Agent"=>"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9", "Connection"=>"Keep-Alive", "Keep-Alive"=>"30"}
|
5
|
+
|
6
|
+
CurrencyList = {
|
7
|
+
:AFN => 'Afghanistan Afghanis',
|
8
|
+
:ALL => 'Albania Leke',
|
9
|
+
:DZD => 'Algeria Dinars',
|
10
|
+
:ARS => 'Argentina Pesos',
|
11
|
+
:AUS => 'Australia Dollars',
|
12
|
+
:BSD => 'Bahamas Dollars',
|
13
|
+
:BHD => 'Bahrain Dinars',
|
14
|
+
:BDT => 'Bangladesh Taka',
|
15
|
+
:BBD => 'Barbados Dollars',
|
16
|
+
:BMD => 'Bermuda Dollars',
|
17
|
+
:BRL => 'Brazil Reais',
|
18
|
+
:BGN => 'Bulgaria Leva',
|
19
|
+
:XOF => 'CFA BCEAO Francs',
|
20
|
+
:XAF => 'CFA BEAC Francs',
|
21
|
+
:CAD => 'Canada Dollars',
|
22
|
+
:CLP => 'Chile Pesos',
|
23
|
+
:CNY => 'China Yuan Renminbi',
|
24
|
+
:COP => 'Colombia Pesos',
|
25
|
+
:XPF => 'Comptoirs Français du Pacifique Francs',
|
26
|
+
:CRC => 'Costa Rica Colones',
|
27
|
+
:HRK => 'Croatia Kuna',
|
28
|
+
:CZK => 'Czech Republic Koruny',
|
29
|
+
:DKK => 'Denmark Kroner',
|
30
|
+
:DOP => 'Dominican Republic Pesos',
|
31
|
+
:XCD => 'East Caribbean Dollars',
|
32
|
+
:EGP => 'Egypt Pounds',
|
33
|
+
:EEK => 'Estonia Krooni',
|
34
|
+
:EUR => 'Euro',
|
35
|
+
:FJD => 'Fiji Dollars',
|
36
|
+
:XAU => 'Gold Ounces',
|
37
|
+
:HKD => 'Hong Kong Dollars',
|
38
|
+
:HUF => 'Hungary Forint',
|
39
|
+
:XDR => 'IMF Special Drawing Rights',
|
40
|
+
:ISK => 'Iceland Kronur',
|
41
|
+
:INR => 'India Rupees',
|
42
|
+
:IDR => 'Indonesia Rupiahs',
|
43
|
+
:IRR => 'Iran Rials',
|
44
|
+
:IQD => 'Iraq Dinars',
|
45
|
+
:ILS => 'Israel New Shekels',
|
46
|
+
:JMD => 'Jamaica Dollars',
|
47
|
+
:JPY => 'Japan Yen',
|
48
|
+
:JOD => 'Jordan Dinars',
|
49
|
+
:KES => 'Kenya Shillings',
|
50
|
+
:KWD => 'Kuwait Dinars',
|
51
|
+
:LBP => 'Lebanon Pounds',
|
52
|
+
:MYR => 'Malaysia Ringgits',
|
53
|
+
:MUR => 'Mauritius Rupees',
|
54
|
+
:MXN => 'Mexico Pesos',
|
55
|
+
:MAD => 'Morocco Dirhams',
|
56
|
+
:NZD => 'New Zealand Dollars',
|
57
|
+
:NOK => 'Norway Kroner',
|
58
|
+
:OMR => 'Oman Rials',
|
59
|
+
:PKR => 'Pakistan Rupees',
|
60
|
+
:XPD => 'Palladium Ounces',
|
61
|
+
:PEN => 'Peru Nuevos Soles',
|
62
|
+
:PHP => 'Philippines Pesos',
|
63
|
+
:XPT => 'Platinum Ounces',
|
64
|
+
:PLN => 'Poland Zlotych',
|
65
|
+
:QAR => 'Qatar Riyals',
|
66
|
+
:RON => 'Romania New Lei',
|
67
|
+
:RUB => 'Russia Rubles',
|
68
|
+
:SAR => 'Saudi Arabia Riyals',
|
69
|
+
:XAG => 'Silver Ounces',
|
70
|
+
:SGD => 'Singapore Dollars',
|
71
|
+
:SKK => 'Slovakia Koruny',
|
72
|
+
:ZAR => 'South Africa Rand',
|
73
|
+
:KRW => 'South Korea Won',
|
74
|
+
:LKR => 'Sri Lanka Rupees',
|
75
|
+
:SDG => 'Sudan Pounds',
|
76
|
+
:SEK => 'Sweden Kronor',
|
77
|
+
:CHF => 'Switzerland Francs',
|
78
|
+
:TWD => 'Taiwan New Dollars',
|
79
|
+
:THB => 'Thailand Baht',
|
80
|
+
:TTD => 'Trinidad and Tobago Dollars',
|
81
|
+
:TND => 'Tunisia Dinars',
|
82
|
+
:TRY => 'Turkey New Lira',
|
83
|
+
:AED => 'United Arab Emirates Dirhams',
|
84
|
+
:GBP => 'United Kingdom Pounds',
|
85
|
+
:USD => 'United States Dollars',
|
86
|
+
:VEF => 'Venezuela Bolivares Fuertes',
|
87
|
+
:VND => 'Vietnam Dong',
|
88
|
+
:ZMK => 'Zambia Kwacha'
|
89
|
+
}
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'timeout'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'hpricot'
|
7
|
+
rescue LoadError
|
8
|
+
require 'rubygems'
|
9
|
+
require 'hpricot'
|
10
|
+
end
|
11
|
+
|
12
|
+
module RCurrency
|
13
|
+
class RCurrencyError < StandardError
|
14
|
+
end
|
15
|
+
|
16
|
+
class InvalidAmountError < RCurrencyError
|
17
|
+
end
|
18
|
+
|
19
|
+
class UnknownCurrencyError < RCurrencyError
|
20
|
+
end
|
21
|
+
|
22
|
+
class ServerTimeoutError < RCurrencyError
|
23
|
+
end
|
24
|
+
|
25
|
+
class Convertor
|
26
|
+
class << self
|
27
|
+
def convert(amount, from, to)
|
28
|
+
raise InvalidAmountError, "Only numeric value allowed" unless amount.is_a?(Numeric)
|
29
|
+
raise UnknownCurrencyError, "Unknown Currency" unless CurrencyList.key?(from.to_sym) && CurrencyList.key?(to.to_sym)
|
30
|
+
|
31
|
+
begin
|
32
|
+
url = "#{CurrencyUrl}?Amount=#{amount}&From=#{from}&To=#{to}"
|
33
|
+
|
34
|
+
timeout(30) do
|
35
|
+
doc = open(url, Params) { |f| Hpricot(f) }
|
36
|
+
result = doc.search("//table[@class='XEtbl_sub']/tr/td[@id='XEenlarge']/h2[@class='XE']")[2].inner_text
|
37
|
+
result.scan(/\d+\.?\d+/).first.to_f
|
38
|
+
end
|
39
|
+
rescue TimeoutError
|
40
|
+
raise ServerTimeoutError, "Can't connect to server #{url}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
File without changes
|
data/lib/rcurrency.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rcurrency'
|
5
|
+
|
6
|
+
class RCurrencyTest < Test::Unit::TestCase
|
7
|
+
def test_convert
|
8
|
+
assert_kind_of Numeric, RCurrency::Convertor.convert(100, "CNY", "USD")
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_invalid_convert
|
12
|
+
assert_raise RCurrency::InvalidAmountError do
|
13
|
+
RCurrency::Convertor.convert("100", "CNY", "USD")
|
14
|
+
end
|
15
|
+
|
16
|
+
assert_raise RCurrency::UnknownCurrencyError do
|
17
|
+
RCurrency::Convertor.convert(100, "CNY", "ABC")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_currency
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.0"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Naveen Joshi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-02-12 00:00:00 +08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.5.0
|
23
|
+
version:
|
24
|
+
description: A ruby lib to real time convert among different currencies
|
25
|
+
email: joshinav@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- History.txt
|
32
|
+
- Manifest.txt
|
33
|
+
- README.txt
|
34
|
+
files:
|
35
|
+
- History.txt
|
36
|
+
- Manifest.txt
|
37
|
+
- README.txt
|
38
|
+
- Rakefile
|
39
|
+
- lib/rcurrency.rb
|
40
|
+
- lib/rcurrency/constants.rb
|
41
|
+
- lib/rcurrency/convertor.rb
|
42
|
+
- lib/rcurrency/updater.rb
|
43
|
+
- test/ts_convertor.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage:
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --main
|
49
|
+
- README.txt
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: rails_currency
|
67
|
+
rubygems_version: 1.0.1
|
68
|
+
signing_key:
|
69
|
+
specification_version: 2
|
70
|
+
summary: Real time currency converter for ruby
|
71
|
+
test_files: []
|
72
|
+
|