i18n-complements 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16d692a49edce3b47b6a8ada497b8146cd1205ec
4
- data.tar.gz: 8cf691d258e71da95029badf9114c5d9b9f5d969
3
+ metadata.gz: 7dda6ac868f33ce669414e1ccd72b713018a1540
4
+ data.tar.gz: 5dfb9a290c3b542c2d633b9e41f97e63a90a25ca
5
5
  SHA512:
6
- metadata.gz: 253e77f53716e54ca9bb917c2d7a2900fd3242c82768b13c0025dafdc9934aab9d448141d349d6419878287812dec382c425f4d3e4eead2067259c1ced36b282
7
- data.tar.gz: 9d5867df77df878ae0bece031e7adff3872e3c10f05f3e570ea80b13ebb18b26dd2e9577883ce9204b39bb30cd0dc875e4f8ed380407fae211fec6124623d318
6
+ metadata.gz: 02421e944a7a2d5c1720b1f444b5fe42bd85befc811b2f089248e460bb9e9b08f2e3b83df9479a95651efb593d120ae22222c9e6deec58b3997e05802c2cb097
7
+ data.tar.gz: 3855cdc267a8cdfe9a6763b103e8f26cfdd733ab93e54f4df759e6bedfc29f143154ac23d15c8b793f7c3b57db9a2e204ab75d07fb2dd97b220bb1b032f68bb0
@@ -7,7 +7,7 @@ module I18n
7
7
  return localize_without_complements(locale, object, format, options)
8
8
  elsif object.respond_to?(:abs)
9
9
  if currency = options[:currency]
10
- raise I18n::InvalidCurrency.new("Currency #{currency} does not exist.") unless I18n::Complements::Numisma[currency.to_s]
10
+ raise I18n::InvalidCurrency, "Currency #{currency} does not exist." unless I18n::Complements::Numisma[currency.to_s]
11
11
  return I18n::Complements::Numisma[currency.to_s].localize(object, locale: locale)
12
12
  else
13
13
  formatter = I18n.translate('number.format'.to_sym, locale: locale, default: {})
@@ -22,7 +22,7 @@ module I18n
22
22
  # Returns a hash with active currencies only
23
23
  def active_currencies
24
24
  x = {}
25
- for code, currency in @@currencies
25
+ @@currencies.each do |code, currency|
26
26
  x[code] = currency if currency.active
27
27
  end
28
28
  x
@@ -33,10 +33,21 @@ module I18n
33
33
  @@currencies[currency_code]
34
34
  end
35
35
 
36
+ def fixed_currency_rate(from, to)
37
+ rates = @@fixed_currency_rates[from]
38
+ return nil unless rates
39
+ rates[to]
40
+ end
41
+
36
42
  def currency_rate(from, to)
37
- raise InvalidCurrency.new(":from currency is unknown (#{from.class}:#{from.inspect})") if Numisma[from].nil?
38
- raise InvalidCurrency.new(":to currency is unknown (#{to.class}:#{to.inspect})") if Numisma[to].nil?
39
- rate = nil
43
+ if Numisma[from].nil?
44
+ raise InvalidCurrency, ":from currency is unknown (#{from.class}:#{from.inspect})"
45
+ end
46
+ if Numisma[to].nil?
47
+ raise InvalidCurrency, ":to currency is unknown (#{to.class}:#{to.inspect})"
48
+ end
49
+ rate = fixed_currency_rate(from, to)
50
+ return rate if rate
40
51
  begin
41
52
  uri = URI('http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate')
42
53
  response = Net::HTTP.post_form(uri, 'FromCurrency' => from, 'ToCurrency' => to)
@@ -53,15 +64,31 @@ module I18n
53
64
  # Load currencies
54
65
  def load_currencies
55
66
  @@currencies = {}
56
- for code, details in YAML.load_file(currencies_file)
57
- currency = Currency.new(code, details.inject({}) { |h, p| h[p[0].to_sym] = p[1]; h })
67
+ yaml = YAML.load_file(currencies_file)
68
+ yaml.each do |code, attributes|
69
+ currency = Currency.new(code, attributes.inject({}) { |h, p| h[p[0].to_sym] = p[1]; h })
58
70
  @@currencies[currency.code] = currency
59
71
  end
60
72
  end
73
+
74
+ # Load fixed currency rates with reverse rates too.
75
+ def load_fixed_currency_rates
76
+ @@fixed_currency_rates = {}
77
+ yaml = YAML.load_file(File.join(File.dirname(__FILE__), 'numisma', 'fixed_currency_rates.yml'))
78
+ yaml.each do |from, rates|
79
+ @@fixed_currency_rates[from] ||= {}
80
+ rates.each do |to, rate|
81
+ @@fixed_currency_rates[from][to] = rate
82
+ @@fixed_currency_rates[to] ||= {}
83
+ @@fixed_currency_rates[to][from] = 1 / rate
84
+ end
85
+ end
86
+ end
61
87
  end
62
88
 
63
89
  # Finally load all currencies
64
90
  load_currencies
91
+ load_fixed_currency_rates
65
92
  end
66
93
  end
67
94
 
@@ -0,0 +1,20 @@
1
+ EUR:
2
+ EEK: 15.6466
3
+ SKK: 30.126
4
+ GRD: 340.75
5
+ PTE: 200.482
6
+ NLG: 2.20371
7
+ BEF: 40.3399
8
+ FRF: 6.55957
9
+ LUF: 40.3399
10
+ LVL: 0.702804
11
+ ITL: 1936.27
12
+ MTL: 0.4293
13
+ LTL: 3.4528
14
+ CYP: 0.585274
15
+ IEP: 0.787564
16
+ DEM: 1.95583
17
+ FIM: 5.94573
18
+ ESP: 166.386
19
+ ATS: 13.7603
20
+ SIT: 239.64
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Complements
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
@@ -44,12 +44,12 @@ class TestCurrencies < I18n::Complements::TestCase
44
44
 
45
45
  def test_number_formatting_with_currency
46
46
  number = 413_500
47
- assert_equal "¥413,500", ::I18n.localize(number, locale: :eng, currency: 'JPY')
47
+ assert_equal '¥413,500', ::I18n.localize(number, locale: :eng, currency: 'JPY')
48
48
  assert_equal "413\u{00A0}500\u{00A0}¥", ::I18n.localize(number, locale: :fra, currency: 'JPY')
49
- assert_equal "413,500円", ::I18n.localize(number, locale: :jpn, currency: 'JPY')
50
- assert_equal "€0.325,1", ::I18n.localize(0.3251, locale: :eng, currency: 'EUR')
49
+ assert_equal '413,500円', ::I18n.localize(number, locale: :jpn, currency: 'JPY')
50
+ assert_equal '€0.325,1', ::I18n.localize(0.3251, locale: :eng, currency: 'EUR')
51
51
  assert_equal "0,325\u{00A0}1\u{00A0}F", ::I18n.localize(0.3251, locale: :fra, currency: 'FRF')
52
- assert_equal "€123,852.325,1", ::I18n.localize(123_852.3251, locale: :eng, currency: 'EUR')
52
+ assert_equal '€123,852.325,1', ::I18n.localize(123_852.3251, locale: :eng, currency: 'EUR')
53
53
  end
54
54
 
55
55
  def test_number_formatting_with_currency_with_core_extensions
@@ -3,6 +3,11 @@ require 'helper'
3
3
 
4
4
  class TestCurrencies < I18n::Complements::TestCase
5
5
  def test_conversion_rate
6
+ rate = I18n::Complements::Numisma.currency_rate('EUR', 'FRF')
7
+ assert !rate.zero?, 'Rate should not be zero'
8
+
9
+ assert rate == 6.55957
10
+
6
11
  assert_raise I18n::InvalidCurrency do
7
12
  I18n::Complements::Numisma.currency_rate(:EUR, 'JPY')
8
13
  end
@@ -17,7 +22,8 @@ class TestCurrencies < I18n::Complements::TestCase
17
22
 
18
23
  r1 = I18n::Complements::Numisma.currency_rate('EUR', 'JPY')
19
24
  r2 = I18n::Complements::Numisma.currency_rate('JPY', 'EUR')
20
- assert 0.01 >= 1 - (r1 * r2).round(2), "EUR -> JPY: #{r1}, JPY -> EUR: #{r2}, #{r1 * r2}, #{(r1 * r2).round(2)}"
25
+ rounded_one = (r1 * r2).round(2)
26
+ assert rounded_one >= 0.99, "EUR -> JPY: #{r1}, JPY -> EUR: #{r2}, #{r1 * r2}, #{rounded_one}"
21
27
 
22
28
  rate = I18n::Complements::Numisma.currency_rate('EUR', 'JPY')
23
29
  assert !rate.nil?, 'Rate cannot be nil'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-complements
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice Texier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-03 00:00:00.000000000 Z
11
+ date: 2016-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -97,6 +97,7 @@ files:
97
97
  - lib/i18n/complements/numisma.rb
98
98
  - lib/i18n/complements/numisma/currencies.yml
99
99
  - lib/i18n/complements/numisma/currency.rb
100
+ - lib/i18n/complements/numisma/fixed_currency_rates.yml
100
101
  - lib/i18n/complements/version.rb
101
102
  - test/helper.rb
102
103
  - test/locales/eng.yml