exchange 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +1 -1
- data/Gemfile.lock +3 -3
- data/README.rdoc +115 -47
- data/benchmark/benchmark.rb +49 -0
- data/changelog.rdoc +8 -1
- data/lib/exchange.rb +4 -4
- data/lib/exchange/base.rb +1 -1
- data/lib/exchange/cache.rb +2 -0
- data/lib/exchange/cache/base.rb +20 -6
- data/lib/exchange/cache/configuration.rb +24 -0
- data/lib/exchange/cache/file.rb +24 -9
- data/lib/exchange/cache/memcached.rb +3 -3
- data/lib/exchange/cache/memory.rb +89 -0
- data/lib/exchange/cache/rails.rb +1 -1
- data/lib/exchange/cache/redis.rb +4 -4
- data/lib/exchange/configurable.rb +53 -0
- data/lib/exchange/configuration.rb +32 -26
- data/lib/exchange/core_extensions.rb +3 -0
- data/lib/exchange/core_extensions/cachify.rb +25 -0
- data/lib/exchange/core_extensions/float/error_safe.rb +25 -0
- data/lib/{core_extensions → exchange/core_extensions/numeric}/conversability.rb +12 -12
- data/lib/exchange/external_api.rb +2 -1
- data/lib/exchange/external_api/base.rb +34 -9
- data/lib/exchange/external_api/call.rb +6 -8
- data/lib/exchange/external_api/configuration.rb +25 -0
- data/lib/exchange/external_api/ecb.rb +16 -25
- data/lib/exchange/external_api/json.rb +11 -1
- data/lib/exchange/external_api/open_exchange_rates.rb +65 -0
- data/lib/exchange/external_api/xavier_media.rb +7 -7
- data/lib/exchange/iso_4217.rb +32 -5
- data/lib/exchange/{currency.rb → money.rb} +112 -110
- data/lib/exchange/typecasting.rb +94 -0
- data/spec/exchange/cache/base_spec.rb +2 -2
- data/spec/exchange/cache/configuration_spec.rb +56 -0
- data/spec/exchange/cache/file_spec.rb +10 -8
- data/spec/exchange/cache/memcached_spec.rb +9 -18
- data/spec/exchange/cache/memory_spec.rb +122 -0
- data/spec/exchange/cache/no_cache_spec.rb +5 -15
- data/spec/exchange/cache/rails_spec.rb +2 -6
- data/spec/exchange/cache/redis_spec.rb +8 -18
- data/spec/exchange/configuration_spec.rb +31 -7
- data/spec/exchange/core_extensions/array/cachify_spec.rb +12 -0
- data/spec/exchange/core_extensions/float/error_safe_spec.rb +49 -0
- data/spec/exchange/core_extensions/hash/cachify_spec.rb +12 -0
- data/spec/exchange/core_extensions/numeric/cachify_spec.rb +26 -0
- data/spec/{core_extensions → exchange/core_extensions/numeric}/conversability_spec.rb +22 -22
- data/spec/exchange/core_extensions/string/cachify_spec.rb +59 -0
- data/spec/exchange/core_extensions/symbol/cachify_spec.rb +12 -0
- data/spec/exchange/external_api/base_spec.rb +10 -7
- data/spec/exchange/external_api/call_spec.rb +3 -0
- data/spec/exchange/external_api/configuration_spec.rb +52 -0
- data/spec/exchange/external_api/ecb_spec.rb +8 -5
- data/spec/exchange/external_api/open_exchange_rates_spec.rb +70 -0
- data/spec/exchange/external_api/xavier_media_spec.rb +8 -5
- data/spec/exchange/iso_4217_spec.rb +208 -20
- data/spec/exchange/{currency_spec.rb → money_spec.rb} +102 -82
- data/spec/exchange/typecasting_spec.rb +86 -0
- metadata +117 -71
- data/exchange-0.7.5.gem +0 -0
- data/exchange-0.7.6.gem +0 -0
- data/lib/exchange/external_api/currency_bot.rb +0 -61
- data/spec/exchange/external_api/currency_bot_spec.rb +0 -63
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Exchange::ExternalAPI::Configuration" do
|
4
|
+
|
5
|
+
subject { Exchange::ExternalAPI::Configuration.instance }
|
6
|
+
|
7
|
+
describe "attr_readers" do
|
8
|
+
[:subclass, :retries, :app_id].each do |reader|
|
9
|
+
it "should respond to #{reader}" do
|
10
|
+
subject.should be_respond_to(reader)
|
11
|
+
end
|
12
|
+
it "should respond to #{reader}=" do
|
13
|
+
subject.should be_respond_to(:"#{reader}=")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "subclass constantize" do
|
19
|
+
it "should automatically constantize the subclass" do
|
20
|
+
subject.subclass = :xavier_media
|
21
|
+
|
22
|
+
subject.subclass.should == Exchange::ExternalAPI::XavierMedia
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "set" do
|
27
|
+
before(:each) do
|
28
|
+
@return = subject.set :subclass => :xavier_media, :retries => 55, :app_id => "KEY"
|
29
|
+
end
|
30
|
+
it "should set the options given" do
|
31
|
+
subject.subclass.should == Exchange::ExternalAPI::XavierMedia
|
32
|
+
subject.retries.should == 55
|
33
|
+
subject.app_id.should == 'KEY'
|
34
|
+
end
|
35
|
+
it "should return self" do
|
36
|
+
@return.should == subject
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "reset" do
|
41
|
+
before(:each) do
|
42
|
+
subject.set :subclass => :xavier_media, :retries => 55, :app_id => "KEY"
|
43
|
+
end
|
44
|
+
it "should restore the defaults" do
|
45
|
+
subject.reset
|
46
|
+
subject.subclass.should == Exchange::ExternalAPI::XavierMedia
|
47
|
+
subject.retries.should == 5
|
48
|
+
subject.app_id.should be_nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -8,6 +8,9 @@ describe "Exchange::ExternalAPI::Ecb" do
|
|
8
8
|
}
|
9
9
|
}
|
10
10
|
end
|
11
|
+
after(:all) do
|
12
|
+
Exchange.configuration.reset
|
13
|
+
end
|
11
14
|
before(:each) do
|
12
15
|
time = Time.gm(2012,2,3)
|
13
16
|
Time.stub! :now => time
|
@@ -19,7 +22,7 @@ describe "Exchange::ExternalAPI::Ecb" do
|
|
19
22
|
end
|
20
23
|
it "should call the api and yield a block with the result" do
|
21
24
|
subject.update
|
22
|
-
subject.base.should ==
|
25
|
+
subject.base.should == :eur
|
23
26
|
end
|
24
27
|
it "should set a unix timestamp from the api file" do
|
25
28
|
subject.update
|
@@ -32,10 +35,10 @@ describe "Exchange::ExternalAPI::Ecb" do
|
|
32
35
|
mock_api("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml", fixture('api_responses/example_ecb_xml_90d.xml'))
|
33
36
|
end
|
34
37
|
it "should convert right" do
|
35
|
-
subject.convert(80,
|
38
|
+
subject.convert(80, :eur, :usd).round(2).should == 105.28
|
36
39
|
end
|
37
40
|
it "should convert negative numbers right" do
|
38
|
-
subject.convert(-70,
|
41
|
+
subject.convert(-70, :chf, :usd).round(2).should == BigDecimal.new("-76.45")
|
39
42
|
end
|
40
43
|
it "should convert when given symbols" do
|
41
44
|
subject.convert(70, :sek, :usd).round(2).should == 10.41
|
@@ -47,10 +50,10 @@ describe "Exchange::ExternalAPI::Ecb" do
|
|
47
50
|
mock_api("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml", fixture('api_responses/example_ecb_xml_history.xml'))
|
48
51
|
end
|
49
52
|
it "should convert and be able to use history" do
|
50
|
-
subject.convert(70,
|
53
|
+
subject.convert(70, :eur, :usd, :at => Time.gm(2011,9,9)).round(2).should == 91.66
|
51
54
|
end
|
52
55
|
it "should convert negative numbers right" do
|
53
|
-
subject.convert(-70,
|
56
|
+
subject.convert(-70, :chf, :usd, :at => Time.gm(2011,9,9)).round(2).should == BigDecimal.new("-76.08")
|
54
57
|
end
|
55
58
|
it "should convert when given symbols" do
|
56
59
|
subject.convert(70, :sek, :usd, :at => Time.gm(2011,9,9)).round(2).should == 10.35
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Exchange::ExternalAPI::OpenExchangeRates" do
|
4
|
+
before(:all) do
|
5
|
+
Exchange.configuration = Exchange::Configuration.new{|c|
|
6
|
+
c.cache = {
|
7
|
+
:subclass => :no_cache
|
8
|
+
}
|
9
|
+
c.api = {
|
10
|
+
:subclass => :open_exchange_rates,
|
11
|
+
:protocol => :https
|
12
|
+
}
|
13
|
+
}
|
14
|
+
end
|
15
|
+
after(:all) do
|
16
|
+
Exchange.configuration.reset
|
17
|
+
end
|
18
|
+
describe "updating rates" do
|
19
|
+
subject { Exchange::ExternalAPI::OpenExchangeRates.new }
|
20
|
+
before(:each) do
|
21
|
+
mock_api("https://openexchangerates.org/api/latest.json?app_id=", fixture('api_responses/example_json_api.json'))
|
22
|
+
end
|
23
|
+
it "should call the api and yield a block with the result" do
|
24
|
+
subject.update
|
25
|
+
subject.base.should == :usd
|
26
|
+
end
|
27
|
+
it "should set a unix timestamp from the api file" do
|
28
|
+
subject.update
|
29
|
+
subject.timestamp.should == 1327748496
|
30
|
+
end
|
31
|
+
end
|
32
|
+
describe "conversion" do
|
33
|
+
subject { Exchange::ExternalAPI::OpenExchangeRates.new }
|
34
|
+
before(:each) do
|
35
|
+
mock_api("https://openexchangerates.org/api/latest.json?app_id=", fixture('api_responses/example_json_api.json'))
|
36
|
+
end
|
37
|
+
it "should convert right" do
|
38
|
+
subject.convert(78, :eur, :usd).round(2).should == BigDecimal.new("103.12")
|
39
|
+
end
|
40
|
+
it "should convert negative numbers right" do
|
41
|
+
subject.convert(-70, :chf, :usd).round(2).should == BigDecimal.new("-76.71")
|
42
|
+
end
|
43
|
+
it "should convert when given symbols" do
|
44
|
+
subject.convert(70, :sek, :usd).round(2).should == 10.38
|
45
|
+
end
|
46
|
+
end
|
47
|
+
describe "historic conversion" do
|
48
|
+
subject { Exchange::ExternalAPI::OpenExchangeRates.new }
|
49
|
+
it "should convert and be able to use history" do
|
50
|
+
mock_api("https://openexchangerates.org/api/historical/2011-09-09.json?app_id=", fixture('api_responses/example_json_api.json'))
|
51
|
+
subject.convert(72, :eur, :usd, :at => Time.gm(2011,9,9)).round(2).should == BigDecimal.new("95.19")
|
52
|
+
end
|
53
|
+
it "should convert negative numbers right" do
|
54
|
+
mock_api("https://openexchangerates.org/api/historical/2011-09-09.json?app_id=", fixture('api_responses/example_json_api.json'))
|
55
|
+
subject.convert(-70, :chf, :usd, :at => Time.gm(2011,9,9)).round(2).should == BigDecimal.new("-76.71")
|
56
|
+
end
|
57
|
+
it "should convert when given symbols" do
|
58
|
+
mock_api("https://openexchangerates.org/api/historical/2011-09-09.json?app_id=", fixture('api_responses/example_json_api.json'))
|
59
|
+
subject.convert(70, :sek, :usd, :at => Time.gm(2011,9,9)).round(2).should == 10.38
|
60
|
+
end
|
61
|
+
it "should convert right when the year is the same, but the yearday is not" do
|
62
|
+
mock_api("https://openexchangerates.org/api/historical/#{Time.now.year}-0#{Time.now.month > 9 ? Time.now.month - 1 : Time.now.month + 1}-01.json?app_id=", fixture('api_responses/example_json_api.json'))
|
63
|
+
subject.convert(70, :sek, :usd, :at => Time.gm(Time.now.year,Time.now.month > 9 ? Time.now.month - 1 : Time.now.month + 1,1)).round(2).should == 10.38
|
64
|
+
end
|
65
|
+
it "should convert right when the yearday is the same, but the year is not" do
|
66
|
+
mock_api("https://openexchangerates.org/api/historical/#{Time.now.year-1}-03-01.json?app_id=", fixture('api_responses/example_json_api.json'))
|
67
|
+
subject.convert(70, :sek, :usd, :at => Time.gm(Time.now.year - 1,3,1)).round(2).should == 10.38
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -8,6 +8,9 @@ describe "Exchange::ExternalAPI::XavierMedia" do
|
|
8
8
|
}
|
9
9
|
}
|
10
10
|
end
|
11
|
+
after(:all) do
|
12
|
+
Exchange.configuration.reset
|
13
|
+
end
|
11
14
|
describe "updating rates" do
|
12
15
|
subject { Exchange::ExternalAPI::XavierMedia.new }
|
13
16
|
before(:each) do
|
@@ -15,7 +18,7 @@ describe "Exchange::ExternalAPI::XavierMedia" do
|
|
15
18
|
end
|
16
19
|
it "should call the api and yield a block with the result" do
|
17
20
|
subject.update
|
18
|
-
subject.base.should ==
|
21
|
+
subject.base.should == :eur
|
19
22
|
end
|
20
23
|
it "should set a unix timestamp from the api file" do
|
21
24
|
subject.update
|
@@ -28,10 +31,10 @@ describe "Exchange::ExternalAPI::XavierMedia" do
|
|
28
31
|
mock_api("http://api.finance.xaviermedia.com/api/#{Time.now.strftime("%Y/%m/%d")}.xml", fixture('api_responses/example_xml_api.xml'))
|
29
32
|
end
|
30
33
|
it "should convert right" do
|
31
|
-
subject.convert(80,
|
34
|
+
subject.convert(80, :eur, :usd).round(2).should == 107.94
|
32
35
|
end
|
33
36
|
it "should convert negative numbers right" do
|
34
|
-
subject.convert(-70,
|
37
|
+
subject.convert(-70, :chf, :usd).round(2).should == BigDecimal.new("-77.01")
|
35
38
|
end
|
36
39
|
it "should convert when given symbols" do
|
37
40
|
subject.convert(70, :sek, :usd).round(2).should == 10.35
|
@@ -41,11 +44,11 @@ describe "Exchange::ExternalAPI::XavierMedia" do
|
|
41
44
|
subject { Exchange::ExternalAPI::XavierMedia.new }
|
42
45
|
it "should convert and be able to use history" do
|
43
46
|
mock_api("http://api.finance.xaviermedia.com/api/2011/09/09.xml", fixture('api_responses/example_xml_api.xml'))
|
44
|
-
subject.convert(70,
|
47
|
+
subject.convert(70, :eur, :usd, :at => Time.gm(2011,9,9)).round(2).should == 94.44
|
45
48
|
end
|
46
49
|
it "should convert negative numbers right" do
|
47
50
|
mock_api("http://api.finance.xaviermedia.com/api/2011/09/09.xml", fixture('api_responses/example_xml_api.xml'))
|
48
|
-
subject.convert(-70,
|
51
|
+
subject.convert(-70, :chf, :usd, :at => Time.gm(2011,9,9)).round(2).should == BigDecimal.new("-77.01")
|
49
52
|
end
|
50
53
|
it "should convert when given symbols" do
|
51
54
|
mock_api("http://api.finance.xaviermedia.com/api/2011/09/09.xml", fixture('api_responses/example_xml_api.xml'))
|
@@ -3,43 +3,231 @@ require 'spec_helper'
|
|
3
3
|
describe "Exchange::ISO4217" do
|
4
4
|
let(:subject) { Exchange::ISO4217 }
|
5
5
|
describe "self.definitions" do
|
6
|
-
it "should be a hash of definitions loaded from the yaml file" do
|
7
|
-
subject.definitions.should
|
6
|
+
it "should be a hash of exactly the ISO standard definitions loaded from the yaml file" do
|
7
|
+
subject.definitions.should === {
|
8
|
+
:nio => { :minor_unit => 2, :currency => "Cordoba Oro" },
|
9
|
+
:lak => { :minor_unit => 2, :currency => "Kip" },
|
10
|
+
:sar => { :format => "#,###.##", :minor_unit => 2, :currency => "Saudi Riyal" },
|
11
|
+
:nok => { :format => "#.###,##", :minor_unit => 2, :currency => "Norwegian Krone" },
|
12
|
+
:usd => { :format=>"#,###.##", :minor_unit => 2, :currency => "US Dollar" },
|
13
|
+
:rub => { :format=>"#.###,##", :minor_unit => 2, :currency => "Russian Ruble" },
|
14
|
+
:xcd => { :format=>"#,###.##", :minor_unit => 2, :currency => "East Caribbean Dollar" },
|
15
|
+
:omr => { :format=>"#,###.###", :minor_unit => 3, :currency => "Rial Omani" },
|
16
|
+
:amd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Armenian Dram" },
|
17
|
+
:cdf => { :minor_unit => 2, :currency => "Congolese Franc" },
|
18
|
+
:kpw => { :minor_unit => 2, :currency => "North Korean Won" },
|
19
|
+
:cny => { :format=>"#,###.##", :minor_unit => 2, :currency => "Yuan Renminbi" },
|
20
|
+
:kes => { :format=>"#,###.##", :minor_unit => 2, :currency => "Kenyan Shilling" },
|
21
|
+
:khr => { :minor_unit => 2, :currency => "Riel" },
|
22
|
+
:pln => { :format=>"# ###,##", :minor_unit => 2, :currency => "Zloty" },
|
23
|
+
:mvr => { :minor_unit => 2, :currency => "Rufiyaa" },
|
24
|
+
:gtq => { :format=>"#,###.##", :minor_unit => 2, :currency => "Quetzal" },
|
25
|
+
:clp => { :format=>"#.###", :minor_unit => 0, :currency => "Chilean Peso" },
|
26
|
+
:inr => { :format=>"#,##,###.##", :minor_unit => 2, :currency => "Indian Rupee" },
|
27
|
+
:bzd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Belize Dollar" },
|
28
|
+
:myr => { :format=>"#,###.##", :minor_unit => 2, :currency => "Malaysian Ringgit" },
|
29
|
+
:hkd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Hong Kong Dollar" },
|
30
|
+
:cop => { :format=>"#.###,##", :minor_unit => 2, :currency => "Colombian Peso" },
|
31
|
+
:dkk => { :format=>"#.###,##", :minor_unit => 2, :currency => "Danish Krone" },
|
32
|
+
:sek => { :format=>"#.###,##", :minor_unit => 2, :currency => "Swedish Krona" },
|
33
|
+
:byr => { :minor_unit => 0, :currency => "Belarussian Ruble" },
|
34
|
+
:lyd => { :minor_unit => 3, :currency => "Libyan Dinar" },
|
35
|
+
:ron => { :format=>"#.###,##", :minor_unit => 2, :currency => "New Romanian Leu" },
|
36
|
+
:dzd => { :minor_unit => 2, :currency => "Algerian Dinar" },
|
37
|
+
:bif => { :minor_unit => 0, :currency => "Burundi Franc" },
|
38
|
+
:ars => { :format=>"#.###,##", :minor_unit => 2, :currency => "Argentine Peso" },
|
39
|
+
:gip => { :format=>"#,###.##", :minor_unit => 2, :currency => "Gibraltar Pound" },
|
40
|
+
:uyi => { :minor_unit => 0, :currency => "Uruguay Peso en Unidades Indexadas (URUIURUI)" },
|
41
|
+
:bob => { :format=>"#,###.##", :minor_unit => 2, :currency => "Boliviano" },
|
42
|
+
:ssp => { :minor_unit => 2, :currency => "South Sudanese Pound" },
|
43
|
+
:ngn => { :minor_unit => 2, :currency => "Naira" },
|
44
|
+
:pgk => { :minor_unit => 2, :currency => "Kina" },
|
45
|
+
:std => { :minor_unit => 2, :currency => "Dobra" },
|
46
|
+
:xof => { :minor_unit => 0, :currency => "CFA Franc BCEAO" },
|
47
|
+
:aed => { :format=>"#,###.##", :minor_unit => 2, :currency => "UAE Dirham" },
|
48
|
+
:ern => { :minor_unit => 2, :currency => "Nakfa" },
|
49
|
+
:mwk => { :minor_unit => 2, :currency => "Kwacha" },
|
50
|
+
:cup => { :format=>"#,###.##", :minor_unit => 2, :currency => "Cuban Peso" },
|
51
|
+
:usn => { :minor_unit => 2, :currency => "US Dollar (Next day)" },
|
52
|
+
:gmd => { :minor_unit => 2, :currency => "Dalasi" },
|
53
|
+
:cve => { :minor_unit => 2, :currency => "Cape Verde Escudo" },
|
54
|
+
:tzs => { :format=>"#,###.##", :minor_unit => 2, :currency => "Tanzanian Shilling" },
|
55
|
+
:cou => { :minor_unit => 2, :currency => "Unidad de Valor Real" },
|
56
|
+
:btn => { :minor_unit => 2, :currency => "Ngultrum" },
|
57
|
+
:zwl => { :minor_unit => 2, :currency => "Zimbabwe Dollar" },
|
58
|
+
:ugx => { :minor_unit => 2, :currency => "Uganda Shilling" },
|
59
|
+
:syp => { :minor_unit => 2, :currency => "Syrian Pound" },
|
60
|
+
:mad => { :minor_unit => 2, :currency => "Moroccan Dirham" },
|
61
|
+
:mnt => { :minor_unit => 2, :currency => "Tugrik" },
|
62
|
+
:lsl => { :minor_unit => 2, :currency => "Loti" },
|
63
|
+
:xaf => { :minor_unit => 0, :currency => "CFA Franc BEAC" },
|
64
|
+
:shp => { :minor_unit => 2, :currency => "Saint Helena Pound" },
|
65
|
+
:htg => { :minor_unit => 2, :currency => "Gourde" },
|
66
|
+
:rsd => { :minor_unit => 2, :currency => "Serbian Dinar" },
|
67
|
+
:mga => { :minor_unit => 2, :currency => "Malagasy Ariary" },
|
68
|
+
:top => { :format=>"#,###.##", :minor_unit => 2, :currency => "Pa\xE2\x80\x99anga" },
|
69
|
+
:mzn => { :minor_unit => 2, :currency => "Mozambique Metical" },
|
70
|
+
:lvl => { :format=>"#,###.##", :minor_unit => 2, :currency => "Latvian Lats" },
|
71
|
+
:fkp => { :minor_unit => 2, :currency => "Falkland Islands Pound" },
|
72
|
+
:uss => { :minor_unit => 2, :currency => "US Dollar (Same day)" },
|
73
|
+
:bwp => { :format=>"#,###.##", :minor_unit => 2, :currency => "Pula" },
|
74
|
+
:hnl => { :format=>"#,###.##", :minor_unit => 2, :currency => "Lempira" },
|
75
|
+
:che => { :minor_unit => 2, :currency => "WIR Euro" },
|
76
|
+
:eur => { :format=>"#,###.##", :minor_unit => 2, :currency => "Euro" },
|
77
|
+
:egp => { :format=>"#,###.##", :minor_unit => 2, :currency => "Egyptian Pound" },
|
78
|
+
:chf => { :format=>"#'###.##", :minor_unit => 2, :currency => "Swiss Franc" },
|
79
|
+
:ils => { :format=>"#,###.##", :minor_unit => 2, :currency => "New Israeli Sheqel" },
|
80
|
+
:pyg => { :minor_unit => 0, :currency => "Guarani" },
|
81
|
+
:lbp => { :format=>"# ###", :minor_unit => 2, :currency => "Lebanese Pound" },
|
82
|
+
:ang => { :format=>"#.###,##", :minor_unit => 2, :currency => "Netherlands Antillean Guilder" },
|
83
|
+
:kzt => { :minor_unit => 2, :currency => "Tenge" },
|
84
|
+
:gyd => { :minor_unit => 2, :currency => "Guyana Dollar" },
|
85
|
+
:wst => { :minor_unit => 2, :currency => "Tala" },
|
86
|
+
:npr => { :format=>"#,###.##", :minor_unit => 2, :currency => "Nepalese Rupee" },
|
87
|
+
:kmf => { :minor_unit => 0, :currency => "Comoro Franc" },
|
88
|
+
:thb => { :format=>"#,###.##", :minor_unit => 2, :currency => "Baht" },
|
89
|
+
:irr => { :format=>"#,###.##", :minor_unit => 2, :currency => "Iranian Rial" },
|
90
|
+
:srd => { :minor_unit => 2, :currency => "Surinam Dollar" },
|
91
|
+
:jpy => { :format=>"#,###", :minor_unit => 0, :currency => "Yen" },
|
92
|
+
:brl => { :format=>"#.###,##", :minor_unit => 2, :currency => "Brazilian Real" },
|
93
|
+
:uyu => { :format=>"#.###,##", :minor_unit => 2, :currency => "Peso Uruguayo" },
|
94
|
+
:mop => { :minor_unit => 2, :currency => "Pataca" },
|
95
|
+
:bmd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Bermudian Dollar" },
|
96
|
+
:szl => { :format=>"#, ###.##", :minor_unit => 2, :currency => "Lilangeni" },
|
97
|
+
:etb => { :minor_unit => 2, :currency => "Ethiopian Birr" },
|
98
|
+
:jod => { :format=>"#,###.###", :minor_unit => 3, :currency => "Jordanian Dinar" },
|
99
|
+
:idr => { :format=>"#.###,##", :minor_unit => 2, :currency => "Rupiah" },
|
100
|
+
:mdl => { :minor_unit => 2, :currency => "Moldovan Leu" },
|
101
|
+
:xpf => { :minor_unit => 0, :currency => "CFP Franc" },
|
102
|
+
:mro => { :minor_unit => 2, :currency => "Ouguiya" },
|
103
|
+
:yer => { :minor_unit => 2, :currency => "Yemeni Rial" },
|
104
|
+
:bam => { :format=>"#,###.##", :minor_unit => 2, :currency => "Convertible Mark" },
|
105
|
+
:awg => { :format=>"#,###.##", :minor_unit => 2, :currency => "Aruban Florin" },
|
106
|
+
:nzd => { :format=>"#,###.##", :minor_unit => 2, :currency => "New Zealand Dollar" },
|
107
|
+
:pen => { :format=>"#,###.##", :minor_unit => 2, :currency => "Nuevo Sol" },
|
108
|
+
:aoa => { :minor_unit => 2, :currency => "Kwanza" },
|
109
|
+
:kyd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Cayman Islands Dollar" },
|
110
|
+
:sll => { :minor_unit => 2, :currency => "Leone" },
|
111
|
+
:try => { :format=>"#,###.##", :minor_unit => 2, :currency => "Turkish Lira" },
|
112
|
+
:vef => { :format=>"#.###,##", :minor_unit => 2, :currency => "Bolivar Fuerte" },
|
113
|
+
:isk => { :format=>"#.###", :minor_unit => 0, :currency => "Iceland Krona" },
|
114
|
+
:gnf => { :minor_unit => 0, :currency => "Guinea Franc" },
|
115
|
+
:bsd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Bahamian Dollar" },
|
116
|
+
:djf => { :minor_unit => 0, :currency => "Djibouti Franc" },
|
117
|
+
:huf => { :format=>"#.###", :minor_unit => 2, :currency => "Forint" },
|
118
|
+
:ltl => { :format=>"# ###,##", :minor_unit => 2, :currency => "Lithuanian Litas" },
|
119
|
+
:mxn => { :format=>"#,###.##", :minor_unit => 2, :currency => "Mexican Peso" },
|
120
|
+
:scr => { :minor_unit => 2, :currency => "Seychelles Rupee" },
|
121
|
+
:sgd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Singapore Dollar" },
|
122
|
+
:lkr => { :minor_unit => 2, :currency => "Sri Lanka Rupee" },
|
123
|
+
:tjs => { :minor_unit => 2, :currency => "Somoni" },
|
124
|
+
:tnd => { :minor_unit => 3, :currency => "Tunisian Dinar" },
|
125
|
+
:dop => { :format=>"#,###.##", :minor_unit => 2, :currency => "Dominican Peso" },
|
126
|
+
:fjd => { :minor_unit => 2, :currency => "Fiji Dollar" },
|
127
|
+
:gel => { :minor_unit => 2, :currency => "Lari" },
|
128
|
+
:sdg => { :minor_unit => 2, :currency => "Sudanese Pound" },
|
129
|
+
:vuv => { :format=>"#,###", :minor_unit => 0, :currency => "Vatu" },
|
130
|
+
:bbd => { :minor_unit => 2, :currency => "Barbados Dollar" },
|
131
|
+
:lrd => { :minor_unit => 2, :currency => "Liberian Dollar" },
|
132
|
+
:krw => { :format=>"#,###", :minor_unit => 0, :currency => "Won" },
|
133
|
+
:mmk => { :minor_unit => 2, :currency => "Kyat" },
|
134
|
+
:mur => { :format=>"#,###", :minor_unit => 2, :currency => "Mauritius Rupee" },
|
135
|
+
:php => { :format=>"#,###.##", :minor_unit => 2, :currency => "Philippine Peso" },
|
136
|
+
:zar => { :format=>"# ###.##", :minor_unit => 2, :currency => "Rand" },
|
137
|
+
:kgs => { :minor_unit => 2, :currency => "Som" },
|
138
|
+
:gbp => { :format=>"#,###.##", :minor_unit => 2, :currency => "Pound Sterling" },
|
139
|
+
:bgn => { :minor_unit => 2, :currency => "Bulgarian Lev" },
|
140
|
+
:iqd => { :minor_unit => 3, :currency => "Iraqi Dinar" },
|
141
|
+
:tmt => { :minor_unit => 2, :currency => "Turkmenistan New Manat" },
|
142
|
+
:uah => { :format=>"# ###,##", :minor_unit => 2, :currency => "Hryvnia" },
|
143
|
+
:vnd => { :format=>"#.###", :minor_unit => 0, :currency => "Dong" },
|
144
|
+
:zmk => { :minor_unit => 2, :currency => "Zambian Kwacha" },
|
145
|
+
:bov => { :minor_unit => 2, :currency => "Mvdol" },
|
146
|
+
:hrk => { :format=>"#.###,##", :minor_unit => 2, :currency => "Croatian Kuna" },
|
147
|
+
:ttd => { :minor_unit => 2, :currency => "Trinidad and Tobago Dollar" },
|
148
|
+
:bhd => { :format=>"#,###.###", :minor_unit => 3, :currency => "Bahraini Dinar" },
|
149
|
+
:clf => { :minor_unit => 0, :currency => "Unidades de fomento" },
|
150
|
+
:rwf => { :minor_unit => 0, :currency => "Rwanda Franc" },
|
151
|
+
:mkd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Denar" },
|
152
|
+
:aud => { :format=>"# ###.##", :minor_unit => 2, :currency => "Australian Dollar" },
|
153
|
+
:crc => { :format=>"#.###,##", :minor_unit => 2, :currency => "Costa Rican Colon" },
|
154
|
+
:pkr => { :format=>"#,###.##", :minor_unit => 2, :currency => "Pakistan Rupee" },
|
155
|
+
:twd => { :minor_unit => 2, :currency => "New Taiwan Dollar" },
|
156
|
+
:uzs => { :minor_unit => 2, :currency => "Uzbekistan Sum" },
|
157
|
+
:czk => { :format=>"#.###,##", :minor_unit => 2, :currency => "Czech Koruna" },
|
158
|
+
:azn => { :minor_unit => 2, :currency => "Azerbaijanian Manat" },
|
159
|
+
:bdt => { :format=>"#,###.##", :minor_unit => 2, :currency => "Taka" },
|
160
|
+
:nad => { :minor_unit => 2, :currency => "Namibia Dollar" },
|
161
|
+
:afn => { :minor_unit => 2, :currency => "Afghani" },
|
162
|
+
:mxv => { :minor_unit => 2, :currency => "Mexican Unidad de Inversion (UDI)" },
|
163
|
+
:cuc => { :format=>"#,###.##", :minor_unit => 2, :currency => "Peso Convertible" },
|
164
|
+
:pab => { :minor_unit => 2, :currency => "Balboa" },
|
165
|
+
:qar => { :minor_unit => 2, :currency => "Qatari Rial" },
|
166
|
+
:sos => { :minor_unit => 2, :currency => "Somali Shilling" },
|
167
|
+
:chw => { :minor_unit => 2, :currency => "WIR Franc" },
|
168
|
+
:cad => { :format=>"#,###.##", :minor_unit => 2, :currency => "Canadian Dollar" },
|
169
|
+
:jmd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Jamaican Dollar" },
|
170
|
+
:bnd => { :format=>"#,###.##", :minor_unit => 2, :currency => "Brunei Dollar" },
|
171
|
+
:all => { :minor_unit => 2, :currency => "Lek" },
|
172
|
+
:svc => { :format=>"#,###.##", :minor_unit => 2, :currency => "El Salvador Colon" },
|
173
|
+
:sbd => { :minor_unit => 2, :currency => "Solomon Islands Dollar" },
|
174
|
+
:ghs => { :minor_unit => 2, :currency => "Ghana Cedi" },
|
175
|
+
:kwd => { :format=>"#,###.###", :minor_unit => 3, :currency => "Kuwaiti Dinar" }
|
176
|
+
}
|
177
|
+
end
|
178
|
+
end
|
179
|
+
describe "self.currencies" do
|
180
|
+
it "should be an array of currency symbols" do
|
181
|
+
subject.currencies.should == %W(nio lak sar nok usd rub xcd omr amd cdf kpw cny kes khr pln mvr gtq clp inr bzd myr hkd cop dkk sek byr lyd ron dzd bif ars gip uyi bob ssp ngn pgk std xof aed ern mwk cup usn gmd cve tzs cou btn zwl ugx syp mad mnt lsl xaf shp htg rsd mga top mzn lvl fkp uss bwp hnl che eur egp chf ils pyg lbp ang kzt gyd wst npr kmf thb irr srd jpy brl uyu mop bmd szl etb jod idr mdl xpf mro yer bam awg nzd pen aoa kyd sll try vef isk gnf bsd djf huf ltl mxn scr sgd lkr tjs tnd dop fjd gel sdg vuv bbd lrd krw mmk mur php zar kgs gbp bgn iqd tmt uah vnd zmk bov hrk ttd bhd clf rwf mkd aud crc pkr twd uzs czk azn bdt nad afn mxv cuc pab qar sos chw cad jmd bnd all svc sbd ghs kwd).sort.map(&:to_sym)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
describe "self.defines?" do
|
185
|
+
context "with a defined currency" do
|
186
|
+
it "should return true" do
|
187
|
+
subject.currencies.each do |curr|
|
188
|
+
subject.should be_defines(curr)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
context "with an undefined currency" do
|
193
|
+
it "should return false" do
|
194
|
+
subject.should_not be_defines(:xxx)
|
195
|
+
end
|
8
196
|
end
|
9
197
|
end
|
10
198
|
describe "self.instantiate" do
|
11
199
|
it "should instantiate a big decimal according to the iso standards" do
|
12
200
|
BigDecimal.should_receive(:new).with('23.23', 3).and_return('INSTANCE')
|
13
|
-
subject.instantiate(23.23,
|
201
|
+
subject.instantiate(23.23, :tnd).should == 'INSTANCE'
|
14
202
|
BigDecimal.should_receive(:new).with('23.23', 2).and_return('INSTANCE')
|
15
|
-
subject.instantiate(23.23,
|
203
|
+
subject.instantiate(23.23, :sar).should == 'INSTANCE'
|
16
204
|
BigDecimal.should_receive(:new).with('23.23', 0).and_return('INSTANCE')
|
17
|
-
subject.instantiate(23.23,
|
205
|
+
subject.instantiate(23.23, :clp).should == 'INSTANCE'
|
18
206
|
end
|
19
207
|
end
|
20
208
|
describe "self.round" do
|
21
209
|
it "should round a currency according to ISO 4217 Definitions" do
|
22
|
-
subject.round(BigDecimal.new("23.232524"),
|
23
|
-
subject.round(BigDecimal.new("23.232524"),
|
24
|
-
subject.round(BigDecimal.new("23.232524"),
|
210
|
+
subject.round(BigDecimal.new("23.232524"), :tnd).should == BigDecimal.new("23.233")
|
211
|
+
subject.round(BigDecimal.new("23.232524"), :sar).should == BigDecimal.new("23.23")
|
212
|
+
subject.round(BigDecimal.new("23.232524"), :clp).should == BigDecimal.new("23")
|
25
213
|
end
|
26
214
|
end
|
27
215
|
describe "self.stringify" do
|
28
216
|
it "should stringify a currency according to ISO 4217 Definitions" do
|
29
|
-
subject.stringify(BigDecimal.new("23.232524"),
|
30
|
-
subject.stringify(BigDecimal.new("23.232524"),
|
31
|
-
subject.stringify(BigDecimal.new("23.232524"),
|
32
|
-
subject.stringify(BigDecimal.new("23.2"),
|
33
|
-
subject.stringify(BigDecimal.new("23.4"),
|
34
|
-
subject.stringify(BigDecimal.new("23.0"),
|
217
|
+
subject.stringify(BigDecimal.new("23.232524"), :tnd).should == "TND 23.233"
|
218
|
+
subject.stringify(BigDecimal.new("23.232524"), :sar).should == "SAR 23.23"
|
219
|
+
subject.stringify(BigDecimal.new("23.232524"), :clp).should == "CLP 23"
|
220
|
+
subject.stringify(BigDecimal.new("23.2"), :tnd).should == "TND 23.200"
|
221
|
+
subject.stringify(BigDecimal.new("23.4"), :sar).should == "SAR 23.40"
|
222
|
+
subject.stringify(BigDecimal.new("23.0"), :clp).should == "CLP 23"
|
35
223
|
end
|
36
224
|
it "should not render the currency if only the amount is wished for" do
|
37
|
-
subject.stringify(BigDecimal.new("23.232524"),
|
38
|
-
subject.stringify(BigDecimal.new("23.232524"),
|
39
|
-
subject.stringify(BigDecimal.new("23.232524"),
|
40
|
-
subject.stringify(BigDecimal.new("23.2"),
|
41
|
-
subject.stringify(BigDecimal.new("23.4"),
|
42
|
-
subject.stringify(BigDecimal.new("23.0"),
|
225
|
+
subject.stringify(BigDecimal.new("23.232524"), :tnd, :amount_only => true).should == "23.233"
|
226
|
+
subject.stringify(BigDecimal.new("23.232524"), :sar, :amount_only => true).should == "23.23"
|
227
|
+
subject.stringify(BigDecimal.new("23.232524"), :clp, :amount_only => true).should == "23"
|
228
|
+
subject.stringify(BigDecimal.new("23.2"), :tnd, :amount_only => true).should == "23.200"
|
229
|
+
subject.stringify(BigDecimal.new("23.4"), :sar, :amount_only => true).should == "23.40"
|
230
|
+
subject.stringify(BigDecimal.new("23.0"), :clp, :amount_only => true).should == "23"
|
43
231
|
end
|
44
232
|
end
|
45
233
|
end
|