currencies 0.2.0 → 0.4.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/.gitignore +2 -1
- data/.rvmrc +1 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/README.markdown +21 -10
- data/Rakefile +0 -2
- data/VERSION +1 -1
- data/currencies.gemspec +11 -6
- data/lib/currencies/currency.rb +46 -7
- data/lib/currencies/exchange_bank.rb +6 -4
- data/lib/currencies.rb +3 -5
- data/lib/data/iso4217.yaml +4 -27
- data/lib/iso4217.rb +8 -0
- data/spec/currency_spec.rb +311 -13
- data/spec/exchange_bank_spec.rb +3 -3
- data/spec/spec_helper.rb +1 -1
- metadata +37 -13
data/.gitignore
CHANGED
data/.rvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rvm ruby@currencies
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
GEM
|
|
2
|
+
remote: http://rubygems.org/
|
|
3
|
+
specs:
|
|
4
|
+
gemcutter (0.6.1)
|
|
5
|
+
git (1.2.5)
|
|
6
|
+
jeweler (1.4.0)
|
|
7
|
+
gemcutter (>= 0.1.0)
|
|
8
|
+
git (>= 1.2.5)
|
|
9
|
+
rubyforge (>= 2.0.0)
|
|
10
|
+
json_pure (1.4.6)
|
|
11
|
+
rspec (1.3.1)
|
|
12
|
+
rubyforge (2.0.4)
|
|
13
|
+
json_pure (>= 1.1.7)
|
|
14
|
+
|
|
15
|
+
PLATFORMS
|
|
16
|
+
ruby
|
|
17
|
+
|
|
18
|
+
DEPENDENCIES
|
|
19
|
+
jeweler (= 1.4.0)
|
|
20
|
+
rspec (= 1.3.1)
|
data/README.markdown
CHANGED
|
@@ -10,21 +10,32 @@ Currencies is hosted on GemCutter, so simply run the following:
|
|
|
10
10
|
|
|
11
11
|
gem sources -a http://gemcutter.org
|
|
12
12
|
sudo gem install currencies
|
|
13
|
-
|
|
13
|
+
|
|
14
|
+
Or you can install via bundler Gemfile:
|
|
15
|
+
|
|
16
|
+
gem 'currencies'
|
|
17
|
+
|
|
18
|
+
Or you can install via bundler Gemfile with using only ISO4217::Currency (no Currency class):
|
|
19
|
+
|
|
20
|
+
gem 'currencies', :require => 'iso4217'
|
|
21
|
+
|
|
14
22
|
Basic Usage
|
|
15
23
|
-----------
|
|
16
24
|
|
|
25
|
+
Note that Currency class still exist by default.
|
|
26
|
+
(is inherited from ISO4217::Currency to keep backward compatibility).
|
|
27
|
+
|
|
17
28
|
There are two ways to get a currency object. The first is to simply make it.
|
|
18
29
|
|
|
19
|
-
Currency.new('USD', :name => 'Dollars', :symbol => '$', :exchange_rate => 1)
|
|
30
|
+
ISO4217::Currency.new('USD', :name => 'Dollars', :symbol => '$', :exchange_rate => 1)
|
|
20
31
|
|
|
21
32
|
Or you can lookup a currency by its ISO 4217 code using the *from_code* method.
|
|
22
33
|
|
|
23
|
-
Currency.from_code('USD')
|
|
34
|
+
ISO4217::Currency.from_code('USD')
|
|
24
35
|
|
|
25
36
|
Once you have a Currency instance you can get basic information about it.
|
|
26
37
|
|
|
27
|
-
currency = Currency.from_code('USD')
|
|
38
|
+
currency = ISO4217::Currency.from_code('USD')
|
|
28
39
|
currency.code #=> "USD"
|
|
29
40
|
currency.name #=> "Dollars"
|
|
30
41
|
currency.symbol #=> "$"
|
|
@@ -36,12 +47,12 @@ Adding Currencies
|
|
|
36
47
|
|
|
37
48
|
Currencies keeps an internal list of currencies for use in the ExchangeBank and to be looked up with the *from_code* method. By default this list contains all the currencies in the ISO 4217 standard. A custom currency can be added using the *add* class method.
|
|
38
49
|
|
|
39
|
-
shiny_button = Currency.new('SBTTN', :name => 'Buttons', :symbol => '☼', :exchange_rate => 1000)
|
|
40
|
-
Currency.add(shiny_button)
|
|
50
|
+
shiny_button = ISO4217::Currency.new('SBTTN', :name => 'Buttons', :symbol => '☼', :exchange_rate => 1000)
|
|
51
|
+
ISO4217::Currency.add(shiny_button)
|
|
41
52
|
|
|
42
53
|
To do a massive addition of currencies you can load a yaml file using the *load_file* class method.
|
|
43
54
|
|
|
44
|
-
Currency.load_file('custom_currency.yaml')
|
|
55
|
+
ISO4217::Currency.load_file('custom_currency.yaml')
|
|
45
56
|
|
|
46
57
|
And the yaml file should look like ...
|
|
47
58
|
|
|
@@ -54,11 +65,11 @@ Defaults
|
|
|
54
65
|
|
|
55
66
|
You can set the base currency by using the *base_currency* class method. This defaults to 'USD'.
|
|
56
67
|
|
|
57
|
-
Currency.base_currency => 'GBP'
|
|
68
|
+
ISO4217::Currency.base_currency => 'GBP'
|
|
58
69
|
|
|
59
70
|
The exchange rate is either set manually or if nil looked up on Yahoo Finance and cached. If you want to disable looking up the currency set the *import_exchange_rates* class method to false.
|
|
60
71
|
|
|
61
|
-
Currency.import_exchange_rates = false
|
|
72
|
+
ISO4217::Currency.import_exchange_rates = false
|
|
62
73
|
|
|
63
74
|
|
|
64
75
|
Money Gem
|
|
@@ -66,7 +77,7 @@ Money Gem
|
|
|
66
77
|
|
|
67
78
|
To use with the money gem you just set the default bank to the currencies bank.
|
|
68
79
|
|
|
69
|
-
Money.default_bank = Currency::ExchangeBank.new
|
|
80
|
+
Money.default_bank = ISO4217::Currency::ExchangeBank.new
|
|
70
81
|
|
|
71
82
|
The Currencies ExchangeBank works the same as the one in the money gem except that if an exchange rate isn't set by default it uses what is set in the currencies gem.
|
|
72
83
|
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.4.0
|
data/currencies.gemspec
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# Generated by jeweler
|
|
2
|
-
# DO NOT EDIT THIS FILE
|
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{currencies}
|
|
8
|
-
s.version = "0.
|
|
8
|
+
s.version = "0.4.0"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["hexorx"]
|
|
12
|
-
s.date = %q{
|
|
12
|
+
s.date = %q{2011-02-07}
|
|
13
13
|
s.description = %q{If you are tracking any kind of assets the currencies gem is for you. It contains every currency in the ISO 4217 standard and allows you to add your own as well. So if you decide to take sparkly buttons as a form of payment you can use currencies to display the shiny button unicode symbol ☼ (disclaimer: ☼ may not look like a shiny button to everyone.) when used with something like the money gem. Speaking of the money gem, currencies gives you an ExchangeBank that the money gem can use to convert from one currency to another. There are plans to have ExchangeRate provider plugin system. Right now the rates are either set manually or pulled from Yahoo Finance.}
|
|
14
14
|
s.email = %q{hexorx@gmail.com}
|
|
15
15
|
s.extra_rdoc_files = [
|
|
@@ -19,6 +19,9 @@ Gem::Specification.new do |s|
|
|
|
19
19
|
s.files = [
|
|
20
20
|
".document",
|
|
21
21
|
".gitignore",
|
|
22
|
+
".rvmrc",
|
|
23
|
+
"Gemfile",
|
|
24
|
+
"Gemfile.lock",
|
|
22
25
|
"LICENSE",
|
|
23
26
|
"README.markdown",
|
|
24
27
|
"Rakefile",
|
|
@@ -29,6 +32,7 @@ Gem::Specification.new do |s|
|
|
|
29
32
|
"lib/currencies/exchange_bank.rb",
|
|
30
33
|
"lib/currencies/extentions.rb",
|
|
31
34
|
"lib/data/iso4217.yaml",
|
|
35
|
+
"lib/iso4217.rb",
|
|
32
36
|
"spec/currency_spec.rb",
|
|
33
37
|
"spec/exchange_bank_spec.rb",
|
|
34
38
|
"spec/spec_helper.rb"
|
|
@@ -36,7 +40,7 @@ Gem::Specification.new do |s|
|
|
|
36
40
|
s.homepage = %q{http://github.com/hexorx/currencies}
|
|
37
41
|
s.rdoc_options = ["--charset=UTF-8"]
|
|
38
42
|
s.require_paths = ["lib"]
|
|
39
|
-
s.rubygems_version = %q{1.3.
|
|
43
|
+
s.rubygems_version = %q{1.3.7}
|
|
40
44
|
s.summary = %q{Simple gem for working with currencies. It is extracted from the countries gem and contains all the currency information in the ISO 4217 standard.}
|
|
41
45
|
s.test_files = [
|
|
42
46
|
"spec/currency_spec.rb",
|
|
@@ -48,7 +52,7 @@ Gem::Specification.new do |s|
|
|
|
48
52
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
49
53
|
s.specification_version = 3
|
|
50
54
|
|
|
51
|
-
if Gem::Version.new(Gem::
|
|
55
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
52
56
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
|
53
57
|
s.add_development_dependency(%q<yard>, [">= 0"])
|
|
54
58
|
else
|
|
@@ -60,3 +64,4 @@ Gem::Specification.new do |s|
|
|
|
60
64
|
s.add_dependency(%q<yard>, [">= 0"])
|
|
61
65
|
end
|
|
62
66
|
end
|
|
67
|
+
|
data/lib/currencies/currency.rb
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
module ISO4217
|
|
2
|
+
end
|
|
3
|
+
|
|
4
|
+
class ISO4217::Currency
|
|
2
5
|
class << self
|
|
3
6
|
attr_accessor :currencies
|
|
4
7
|
attr_accessor :base_currency
|
|
8
|
+
attr_accessor :major_codes
|
|
5
9
|
attr_accessor :import_exchange_rates
|
|
6
10
|
end
|
|
7
11
|
|
|
@@ -11,7 +15,7 @@ class Currency
|
|
|
11
15
|
@code = iso_code.to_s.upcase
|
|
12
16
|
@name = opts['name']
|
|
13
17
|
@symbol = opts['symbol']
|
|
14
|
-
@exchange_currency = opts['exchange_currency'] ||
|
|
18
|
+
@exchange_currency = opts['exchange_currency'] || self.class.base_currency
|
|
15
19
|
@exchange_rate = opts['exchange_rate'].to_f if opts['exchange_rate']
|
|
16
20
|
end
|
|
17
21
|
|
|
@@ -20,14 +24,14 @@ class Currency
|
|
|
20
24
|
end
|
|
21
25
|
|
|
22
26
|
def exchange_rate
|
|
23
|
-
@exchange_rate = nil unless @exchange_currency ==
|
|
27
|
+
@exchange_rate = nil unless @exchange_currency == self.class.base_currency
|
|
24
28
|
@exchange_rate ||= load_exchange_rate
|
|
25
29
|
end
|
|
26
30
|
|
|
27
31
|
def load_exchange_rate
|
|
28
|
-
@exchange_currency =
|
|
32
|
+
@exchange_currency = self.class.base_currency
|
|
29
33
|
return 1.0 if @code == @exchange_currency
|
|
30
|
-
if
|
|
34
|
+
if self.class.import_exchange_rates
|
|
31
35
|
http = Net::HTTP.new('download.finance.yahoo.com', 80)
|
|
32
36
|
response = http.get("/d/quotes.csv?e=.csv&f=sl1d1t1&s=#{@code}#{@exchange_currency}=X")
|
|
33
37
|
rate = response.body.split(',')[1]
|
|
@@ -46,7 +50,41 @@ class Currency
|
|
|
46
50
|
def self.from_code(code)
|
|
47
51
|
self.currencies[code.to_s.upcase]
|
|
48
52
|
end
|
|
49
|
-
|
|
53
|
+
|
|
54
|
+
def self.major_currencies_selection(currencies)
|
|
55
|
+
currencies.select { |code, currency| self.major_codes.include?(code) }.first
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.best_from_currencies(currencies)
|
|
59
|
+
return if currencies.nil? || currencies.empty?
|
|
60
|
+
self.major_currencies_selection(currencies) ? self.major_currencies_selection(currencies)[1] : currencies.first[1]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.list_from_name(name)
|
|
64
|
+
self.currencies.select { |code, currency| currency.name == name }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.list_from_symbol(symbol)
|
|
68
|
+
self.currencies.select { |code, currency| currency.symbol == symbol }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def self.best_from_name(name)
|
|
72
|
+
self.best_from_currencies(self.list_from_name(name))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.best_from_symbol(symbol)
|
|
76
|
+
self.best_from_currencies(self.list_from_symbol(symbol))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def self.best_guess(str)
|
|
80
|
+
return if str.nil? || str.empty?
|
|
81
|
+
self.from_code(str) || self.best_from_symbol(str) || self.best_from_name(str)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.code_from_best_guess(str)
|
|
85
|
+
self.best_guess(str).try(:code)
|
|
86
|
+
end
|
|
87
|
+
|
|
50
88
|
def self.add(new_currency)
|
|
51
89
|
self.currencies ||= {}
|
|
52
90
|
self.currencies[new_currency.code] = new_currency
|
|
@@ -54,5 +92,6 @@ class Currency
|
|
|
54
92
|
|
|
55
93
|
load_file(File.join(File.dirname(__FILE__), '..', 'data', 'iso4217.yaml'))
|
|
56
94
|
self.base_currency = 'USD'
|
|
95
|
+
self.major_codes = [ "USD", "EUR", "GBP" ]
|
|
57
96
|
self.import_exchange_rates = true
|
|
58
|
-
end
|
|
97
|
+
end
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
require 'thread'
|
|
2
|
+
|
|
3
|
+
class ISO4217::Currency
|
|
2
4
|
class ExchangeBank
|
|
3
5
|
def self.instance
|
|
4
6
|
@@singleton
|
|
@@ -30,8 +32,8 @@ class Currency
|
|
|
30
32
|
if rate
|
|
31
33
|
(cents * rate).floor
|
|
32
34
|
else
|
|
33
|
-
from_currency = Currency.from_code(from_currency)
|
|
34
|
-
to_currency = Currency.from_code(to_currency)
|
|
35
|
+
from_currency = ISO4217::Currency.from_code(from_currency)
|
|
36
|
+
to_currency = ISO4217::Currency.from_code(to_currency)
|
|
35
37
|
|
|
36
38
|
if from_currency && to_currency && from_currency.exchange_rate && to_currency.exchange_rate && (from_currency.exchange_currency == to_currency.exchange_currency)
|
|
37
39
|
((cents * from_currency.exchange_rate) / to_currency.exchange_rate).floor
|
|
@@ -43,4 +45,4 @@ class Currency
|
|
|
43
45
|
|
|
44
46
|
@@singleton = ExchangeBank.new
|
|
45
47
|
end
|
|
46
|
-
end
|
|
48
|
+
end
|
data/lib/currencies.rb
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
|
|
2
2
|
|
|
3
|
-
require '
|
|
4
|
-
require 'net/http' unless defined?(Net::HTTP)
|
|
3
|
+
require 'iso4217'
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
require 'currencies/exchange_bank'
|
|
5
|
+
class Currency < ISO4217::Currency
|
|
6
|
+
end
|
data/lib/data/iso4217.yaml
CHANGED
|
@@ -399,12 +399,7 @@ LRD:
|
|
|
399
399
|
name: Dollars
|
|
400
400
|
symbol: $
|
|
401
401
|
unicode_hex: 36
|
|
402
|
-
LSL:
|
|
403
|
-
alt_currency:
|
|
404
|
-
code: ZAR
|
|
405
|
-
name: Rand
|
|
406
|
-
symbol: R
|
|
407
|
-
unicode_hex: 82
|
|
402
|
+
LSL:
|
|
408
403
|
code: LSL
|
|
409
404
|
name: Maloti
|
|
410
405
|
LTL:
|
|
@@ -464,19 +459,14 @@ MYR:
|
|
|
464
459
|
unicode_hex:
|
|
465
460
|
- 82
|
|
466
461
|
- 77
|
|
467
|
-
MZN:
|
|
462
|
+
MZN:
|
|
468
463
|
code: MZN
|
|
469
464
|
name: Meticals
|
|
470
465
|
symbol: MT
|
|
471
466
|
unicode_hex:
|
|
472
467
|
- 77
|
|
473
468
|
- 84
|
|
474
|
-
NAD:
|
|
475
|
-
alt_currency:
|
|
476
|
-
code: ZAR
|
|
477
|
-
name: Rand
|
|
478
|
-
symbol: R
|
|
479
|
-
unicode_hex: 82
|
|
469
|
+
NAD:
|
|
480
470
|
code: NAD
|
|
481
471
|
name: Dollars
|
|
482
472
|
symbol: $
|
|
@@ -515,12 +505,7 @@ OMR:
|
|
|
515
505
|
name: Riais
|
|
516
506
|
symbol: ﷼
|
|
517
507
|
unicode_hex: 65020
|
|
518
|
-
PAB:
|
|
519
|
-
alt_currency:
|
|
520
|
-
code: USD
|
|
521
|
-
name: Dollars
|
|
522
|
-
symbol: $
|
|
523
|
-
unicode_hex: 36
|
|
508
|
+
PAB:
|
|
524
509
|
code: PAB
|
|
525
510
|
name: Balboa
|
|
526
511
|
symbol: B/.
|
|
@@ -662,14 +647,6 @@ THB:
|
|
|
662
647
|
symbol: ฿
|
|
663
648
|
unicode_hex: 3647
|
|
664
649
|
TJS:
|
|
665
|
-
alt_currency:
|
|
666
|
-
code: RUB
|
|
667
|
-
name: Rubles
|
|
668
|
-
symbol: руб
|
|
669
|
-
unicode_hex:
|
|
670
|
-
- 1088
|
|
671
|
-
- 1091
|
|
672
|
-
- 1073
|
|
673
650
|
code: TJS
|
|
674
651
|
name: Somoni
|
|
675
652
|
TND:
|
data/lib/iso4217.rb
ADDED
data/spec/currency_spec.rb
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
|
3
|
-
describe Currency do
|
|
3
|
+
describe ISO4217::Currency do
|
|
4
4
|
before(:all) do
|
|
5
|
-
@usd = Currency.from_code(:USD)
|
|
6
|
-
@gbp = Currency.from_code(:GBP)
|
|
5
|
+
@usd = ISO4217::Currency.from_code(:USD)
|
|
6
|
+
@gbp = ISO4217::Currency.from_code(:GBP)
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
it 'should return code' do
|
|
@@ -35,32 +35,330 @@ describe Currency do
|
|
|
35
35
|
|
|
36
36
|
describe 'from_code' do
|
|
37
37
|
it 'should return new Currency instance when passed iso4217 currency code' do
|
|
38
|
-
Currency.from_code('USD').should be_a(Currency)
|
|
38
|
+
ISO4217::Currency.from_code('USD').should be_a(ISO4217::Currency)
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it 'should return a currency with the same code' do
|
|
42
|
-
Currency.from_code('USD').code.should == 'USD'
|
|
43
|
-
Currency.from_code('GBP').code.should == 'GBP'
|
|
42
|
+
ISO4217::Currency.from_code('USD').code.should == 'USD'
|
|
43
|
+
ISO4217::Currency.from_code('GBP').code.should == 'GBP'
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
it 'should accept symbol' do
|
|
47
|
-
Currency.from_code(:USD).code.should == 'USD'
|
|
48
|
-
Currency.from_code(:GBP).code.should == 'GBP'
|
|
47
|
+
ISO4217::Currency.from_code(:USD).code.should == 'USD'
|
|
48
|
+
ISO4217::Currency.from_code(:GBP).code.should == 'GBP'
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
it 'should work with lower case' do
|
|
52
|
-
Currency.from_code('usd').code.should == 'USD'
|
|
53
|
-
Currency.from_code('gbp').code.should == 'GBP'
|
|
52
|
+
ISO4217::Currency.from_code('usd').code.should == 'USD'
|
|
53
|
+
ISO4217::Currency.from_code('gbp').code.should == 'GBP'
|
|
54
54
|
end
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
describe 'exchange_rate' do
|
|
58
58
|
it 'should return a float' do
|
|
59
|
-
Currency.from_code('GBP').exchange_rate.should be_a(Float)
|
|
59
|
+
ISO4217::Currency.from_code('GBP').exchange_rate.should be_a(Float)
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
it 'should have an exchange rate of 1.0 for the base currency' do
|
|
63
|
-
Currency.from_code(Currency.base_currency).exchange_rate.should == 1.0
|
|
63
|
+
ISO4217::Currency.from_code(ISO4217::Currency.base_currency).exchange_rate.should == 1.0
|
|
64
64
|
end
|
|
65
65
|
end
|
|
66
|
-
|
|
66
|
+
|
|
67
|
+
describe ".major_currencies_selection" do
|
|
68
|
+
let(:usd) { [ "USD", mock ] }
|
|
69
|
+
let(:aud) { [ "AUD", mock ] }
|
|
70
|
+
let(:currencies) { [ usd, aud ] }
|
|
71
|
+
|
|
72
|
+
context "with default settings" do
|
|
73
|
+
subject do
|
|
74
|
+
ISO4217::Currency.major_currencies_selection(currencies)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it { should == usd }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
context "with changed major currency to AUD" do
|
|
81
|
+
subject do
|
|
82
|
+
ISO4217::Currency.major_codes = [ "AUD" ]
|
|
83
|
+
ISO4217::Currency.major_currencies_selection(currencies)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it { should == aud }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
describe ".best_from_currencies" do
|
|
91
|
+
let(:usd) { mock }
|
|
92
|
+
let(:pln) { mock }
|
|
93
|
+
let(:aud) { mock }
|
|
94
|
+
|
|
95
|
+
context "when given currencies is nil" do
|
|
96
|
+
subject { ISO4217::Currency.best_from_currencies(nil) }
|
|
97
|
+
|
|
98
|
+
it { should be_nil }
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
context "when given currencies is empty array" do
|
|
102
|
+
subject { ISO4217::Currency.best_from_currencies([]) }
|
|
103
|
+
|
|
104
|
+
it { should be_nil }
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
context "when major currency exist within currencies" do
|
|
108
|
+
let(:valid_currencies) { mock(:nil? => false, :empty? => false) }
|
|
109
|
+
subject do
|
|
110
|
+
ISO4217::Currency.stub!(:major_currencies_selection => [mock, usd])
|
|
111
|
+
ISO4217::Currency.best_from_currencies(valid_currencies)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it { should == usd }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
context "when major currency does not exist within currencies" do
|
|
118
|
+
let(:valid_currencies) { [ [mock, pln], [mock, aud] ] }
|
|
119
|
+
subject do
|
|
120
|
+
ISO4217::Currency.stub!(:major_currencies_selection => nil)
|
|
121
|
+
ISO4217::Currency.best_from_currencies(valid_currencies)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it { should == pln }
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
describe ".list_from_name" do
|
|
129
|
+
let(:euro_currency) { mock(:name => "Euro") }
|
|
130
|
+
let(:euro) { [ [ "EUR", euro_currency ] ] }
|
|
131
|
+
let(:dollar_currency) { mock(:name => "Dollars") }
|
|
132
|
+
let(:dollars) { [
|
|
133
|
+
[ "USD", dollar_currency ],
|
|
134
|
+
[ "AUD", dollar_currency ]
|
|
135
|
+
] }
|
|
136
|
+
let(:currencies) { euro + dollars }
|
|
137
|
+
|
|
138
|
+
subject do
|
|
139
|
+
ISO4217::Currency.stub!(:currencies => currencies)
|
|
140
|
+
ISO4217::Currency.list_from_name("Dollars")
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it { should == dollars }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
describe ".list_from_symbol" do
|
|
147
|
+
let(:euro_currency) { mock(:symbol => "€") }
|
|
148
|
+
let(:euro) { [ [ "EUR", euro_currency ] ] }
|
|
149
|
+
let(:dollar_currency) { mock(:symbol => "$") }
|
|
150
|
+
let(:dollars) { [
|
|
151
|
+
[ "USD", dollar_currency ],
|
|
152
|
+
[ "AUD", dollar_currency ]
|
|
153
|
+
] }
|
|
154
|
+
let(:currencies) { euro + dollars }
|
|
155
|
+
|
|
156
|
+
subject do
|
|
157
|
+
ISO4217::Currency.stub!(:currencies => currencies)
|
|
158
|
+
ISO4217::Currency.list_from_symbol("$")
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
it { should == dollars }
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
describe ".best_from_name" do
|
|
165
|
+
let(:name) { mock }
|
|
166
|
+
let(:list_from_name) { mock }
|
|
167
|
+
describe "behavior" do
|
|
168
|
+
before do
|
|
169
|
+
ISO4217::Currency.stub!(
|
|
170
|
+
:best_from_currencies => nil,
|
|
171
|
+
:list_from_name => list_from_name
|
|
172
|
+
)
|
|
173
|
+
end
|
|
174
|
+
after { ISO4217::Currency.best_from_name(name) }
|
|
175
|
+
|
|
176
|
+
it "should select best from list of currencies with given name" do
|
|
177
|
+
ISO4217::Currency.should_receive(:best_from_currencies).with(list_from_name)
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
it "should select list of currencies with given name" do
|
|
181
|
+
ISO4217::Currency.should_receive(:list_from_name).with(name).and_return(list_from_name)
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
describe "returns" do
|
|
186
|
+
let(:best_from_currencies) { mock }
|
|
187
|
+
subject do
|
|
188
|
+
ISO4217::Currency.stub!(
|
|
189
|
+
:best_from_currencies => best_from_currencies,
|
|
190
|
+
:list_from_name => list_from_name
|
|
191
|
+
)
|
|
192
|
+
ISO4217::Currency.best_from_name(name)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
it { should == best_from_currencies }
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
describe ".best_from_symbol" do
|
|
200
|
+
let(:symbol) { mock }
|
|
201
|
+
let(:list_from_symbol) { mock }
|
|
202
|
+
describe "behavior" do
|
|
203
|
+
before do
|
|
204
|
+
ISO4217::Currency.stub!(
|
|
205
|
+
:best_from_currencies => nil,
|
|
206
|
+
:list_from_symbol => list_from_symbol
|
|
207
|
+
)
|
|
208
|
+
end
|
|
209
|
+
after { ISO4217::Currency.best_from_symbol(symbol) }
|
|
210
|
+
|
|
211
|
+
it "should select best from list of currencies with given symbol" do
|
|
212
|
+
ISO4217::Currency.should_receive(:best_from_currencies).with(list_from_symbol)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
it "should select list of currencies with given symbol" do
|
|
216
|
+
ISO4217::Currency.should_receive(:list_from_symbol).with(symbol).and_return(list_from_symbol)
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
describe "returns" do
|
|
221
|
+
let(:best_from_currencies) { mock }
|
|
222
|
+
subject do
|
|
223
|
+
ISO4217::Currency.stub!(
|
|
224
|
+
:best_from_currencies => best_from_currencies,
|
|
225
|
+
:list_from_symbol => list_from_symbol
|
|
226
|
+
)
|
|
227
|
+
ISO4217::Currency.best_from_symbol(symbol)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
it { should == best_from_currencies }
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
describe ".best_guess" do
|
|
235
|
+
let(:eur) { mock }
|
|
236
|
+
let(:string) { mock(:nil? => false, :empty? => false) }
|
|
237
|
+
|
|
238
|
+
context "when string not given" do
|
|
239
|
+
subject { ISO4217::Currency.best_guess(nil) }
|
|
240
|
+
|
|
241
|
+
it { should be_nil }
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
context "when given empty string" do
|
|
245
|
+
subject { ISO4217::Currency.best_guess("") }
|
|
246
|
+
|
|
247
|
+
it { should be_nil }
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
context "when code equal to string exist" do
|
|
251
|
+
subject do
|
|
252
|
+
ISO4217::Currency.stub!(:from_code).with(string).and_return(eur)
|
|
253
|
+
ISO4217::Currency.best_guess(string)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
it { should == eur }
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
context "when best symbol equal to string exist" do
|
|
260
|
+
subject do
|
|
261
|
+
ISO4217::Currency.stub!(:from_code).with(string).and_return(nil)
|
|
262
|
+
ISO4217::Currency.stub!(:best_from_symbol).with(string).and_return(eur)
|
|
263
|
+
ISO4217::Currency.best_guess(string)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
it { should == eur }
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
context "when best name equal to string exist" do
|
|
270
|
+
subject do
|
|
271
|
+
ISO4217::Currency.stub!(:from_code).with(string).and_return(nil)
|
|
272
|
+
ISO4217::Currency.stub!(:best_from_symbol).with(string).and_return(nil)
|
|
273
|
+
ISO4217::Currency.stub!(:best_from_name).with(string).and_return(eur)
|
|
274
|
+
ISO4217::Currency.best_guess(string)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
it { should == eur }
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
context "when string not exist in any form" do
|
|
281
|
+
subject do
|
|
282
|
+
ISO4217::Currency.stub!(:from_code).with(string).and_return(nil)
|
|
283
|
+
ISO4217::Currency.stub!(:best_from_symbol).with(string).and_return(nil)
|
|
284
|
+
ISO4217::Currency.stub!(:best_from_name).with(string).and_return(nil)
|
|
285
|
+
ISO4217::Currency.best_guess(string)
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
it { should be_nil }
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
describe "behavior" do
|
|
292
|
+
it "should run methods in proper order" do
|
|
293
|
+
ISO4217::Currency.should_receive(:from_code).ordered.with("string").and_return(nil)
|
|
294
|
+
ISO4217::Currency.should_receive(:best_from_symbol).ordered.with("string").and_return(nil)
|
|
295
|
+
ISO4217::Currency.should_receive(:best_from_name).ordered.with("string").and_return(nil)
|
|
296
|
+
ISO4217::Currency.best_guess("string")
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
describe ".code_from_best_guess" do
|
|
302
|
+
let(:string) { mock }
|
|
303
|
+
let(:code) { mock }
|
|
304
|
+
let(:best_guess) { mock(:try => code) }
|
|
305
|
+
|
|
306
|
+
context "when there is best guess" do
|
|
307
|
+
subject do
|
|
308
|
+
ISO4217::Currency.stub!(:best_guess).with(string).and_return(best_guess)
|
|
309
|
+
ISO4217::Currency.code_from_best_guess(string)
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
it { should == code }
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
context "when there is no best guess" do
|
|
316
|
+
let(:best_guess) { mock(:try => nil) }
|
|
317
|
+
|
|
318
|
+
subject do
|
|
319
|
+
ISO4217::Currency.stub!(:best_guess).with(string).and_return(best_guess)
|
|
320
|
+
ISO4217::Currency.code_from_best_guess(string)
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
it { should be_nil }
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
describe "behavior" do
|
|
327
|
+
before { ISO4217::Currency.stub!(:best_guess).with(string).and_return(best_guess) }
|
|
328
|
+
after { ISO4217::Currency.code_from_best_guess(string) }
|
|
329
|
+
|
|
330
|
+
it "should call .best_guess" do
|
|
331
|
+
ISO4217::Currency.should_receive(:best_guess).with(string).and_return(best_guess)
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
it "should call #code on best guessed" do
|
|
335
|
+
best_guess.should_receive(:try).with(:code)
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
describe "Currency class" do
|
|
341
|
+
context "when loaded via 'iso4217' existance" do
|
|
342
|
+
subject { defined?(Currency) }
|
|
343
|
+
|
|
344
|
+
it { should be_false }
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
context "when loaded via 'currencies'" do
|
|
348
|
+
before { require 'currencies' }
|
|
349
|
+
|
|
350
|
+
describe "existance" do
|
|
351
|
+
subject { defined?(Currency) }
|
|
352
|
+
|
|
353
|
+
it { should be_true }
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
describe "superclass" do
|
|
357
|
+
subject { Currency.superclass }
|
|
358
|
+
|
|
359
|
+
it { should == ISO4217::Currency }
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
end
|
data/spec/exchange_bank_spec.rb
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
|
3
|
-
describe Currency::ExchangeBank do
|
|
3
|
+
describe ISO4217::Currency::ExchangeBank do
|
|
4
4
|
before :each do
|
|
5
|
-
@bank = Currency::ExchangeBank.new
|
|
5
|
+
@bank = ISO4217::Currency::ExchangeBank.new
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
it "returns the previously specified conversion rate" do
|
|
@@ -46,4 +46,4 @@ describe Currency::ExchangeBank do
|
|
|
46
46
|
block = lambda { @bank.exchange(10, "USD", "BUTTON") }
|
|
47
47
|
block.should raise_error(Money::UnknownRate)
|
|
48
48
|
end
|
|
49
|
-
end
|
|
49
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: currencies
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
4
|
+
hash: 15
|
|
5
|
+
prerelease: false
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 4
|
|
9
|
+
- 0
|
|
10
|
+
version: 0.4.0
|
|
5
11
|
platform: ruby
|
|
6
12
|
authors:
|
|
7
13
|
- hexorx
|
|
@@ -9,29 +15,37 @@ autorequire:
|
|
|
9
15
|
bindir: bin
|
|
10
16
|
cert_chain: []
|
|
11
17
|
|
|
12
|
-
date:
|
|
18
|
+
date: 2011-02-07 00:00:00 -07:00
|
|
13
19
|
default_executable:
|
|
14
20
|
dependencies:
|
|
15
21
|
- !ruby/object:Gem::Dependency
|
|
16
22
|
name: rspec
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
23
|
+
prerelease: false
|
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
20
26
|
requirements:
|
|
21
27
|
- - ">="
|
|
22
28
|
- !ruby/object:Gem::Version
|
|
29
|
+
hash: 3
|
|
30
|
+
segments:
|
|
31
|
+
- 0
|
|
23
32
|
version: "0"
|
|
24
|
-
|
|
33
|
+
type: :development
|
|
34
|
+
version_requirements: *id001
|
|
25
35
|
- !ruby/object:Gem::Dependency
|
|
26
36
|
name: yard
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
37
|
+
prerelease: false
|
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
30
40
|
requirements:
|
|
31
41
|
- - ">="
|
|
32
42
|
- !ruby/object:Gem::Version
|
|
43
|
+
hash: 3
|
|
44
|
+
segments:
|
|
45
|
+
- 0
|
|
33
46
|
version: "0"
|
|
34
|
-
|
|
47
|
+
type: :development
|
|
48
|
+
version_requirements: *id002
|
|
35
49
|
description: "If you are tracking any kind of assets the currencies gem is for you. It contains every currency in the ISO 4217 standard and allows you to add your own as well. So if you decide to take sparkly buttons as a form of payment you can use currencies to display the shiny button unicode symbol \xE2\x98\xBC (disclaimer: \xE2\x98\xBC may not look like a shiny button to everyone.) when used with something like the money gem. Speaking of the money gem, currencies gives you an ExchangeBank that the money gem can use to convert from one currency to another. There are plans to have ExchangeRate provider plugin system. Right now the rates are either set manually or pulled from Yahoo Finance."
|
|
36
50
|
email: hexorx@gmail.com
|
|
37
51
|
executables: []
|
|
@@ -44,6 +58,9 @@ extra_rdoc_files:
|
|
|
44
58
|
files:
|
|
45
59
|
- .document
|
|
46
60
|
- .gitignore
|
|
61
|
+
- .rvmrc
|
|
62
|
+
- Gemfile
|
|
63
|
+
- Gemfile.lock
|
|
47
64
|
- LICENSE
|
|
48
65
|
- README.markdown
|
|
49
66
|
- Rakefile
|
|
@@ -54,6 +71,7 @@ files:
|
|
|
54
71
|
- lib/currencies/exchange_bank.rb
|
|
55
72
|
- lib/currencies/extentions.rb
|
|
56
73
|
- lib/data/iso4217.yaml
|
|
74
|
+
- lib/iso4217.rb
|
|
57
75
|
- spec/currency_spec.rb
|
|
58
76
|
- spec/exchange_bank_spec.rb
|
|
59
77
|
- spec/spec_helper.rb
|
|
@@ -67,21 +85,27 @@ rdoc_options:
|
|
|
67
85
|
require_paths:
|
|
68
86
|
- lib
|
|
69
87
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
|
+
none: false
|
|
70
89
|
requirements:
|
|
71
90
|
- - ">="
|
|
72
91
|
- !ruby/object:Gem::Version
|
|
92
|
+
hash: 3
|
|
93
|
+
segments:
|
|
94
|
+
- 0
|
|
73
95
|
version: "0"
|
|
74
|
-
version:
|
|
75
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
|
+
none: false
|
|
76
98
|
requirements:
|
|
77
99
|
- - ">="
|
|
78
100
|
- !ruby/object:Gem::Version
|
|
101
|
+
hash: 3
|
|
102
|
+
segments:
|
|
103
|
+
- 0
|
|
79
104
|
version: "0"
|
|
80
|
-
version:
|
|
81
105
|
requirements: []
|
|
82
106
|
|
|
83
107
|
rubyforge_project:
|
|
84
|
-
rubygems_version: 1.3.
|
|
108
|
+
rubygems_version: 1.3.7
|
|
85
109
|
signing_key:
|
|
86
110
|
specification_version: 3
|
|
87
111
|
summary: Simple gem for working with currencies. It is extracted from the countries gem and contains all the currency information in the ISO 4217 standard.
|