money_bank_sources 0.0.2 → 0.1.0.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,49 +4,32 @@ require "rexml/document"
4
4
  class Money
5
5
  module BankSources
6
6
  class AzCentralBank
7
- CODE_MAP = {
8
- # RUR is an old roubles code according to ISO 4217
9
- "RUR" => "RUB"
10
- }
11
-
12
- attr_reader :rates
13
-
14
7
  def initialize(options = {})
15
8
  @date = options[:date] || Date.today
16
9
  @bank = options[:bank] || Money.default_bank
17
- @only = options[:only] ? Array(options[:only]) : nil
18
10
  @data = options[:data]
19
- @rates = {}
20
11
  end
21
12
 
22
13
  def parse
23
14
  xml.elements.each("ValCurs/ValType/Valute") do |currency|
24
15
  code = currency.attributes["Code"]
25
-
26
- code = CODE_MAP.fetch(code, code)
27
- next if skip_code?(code)
28
-
29
16
  rate = currency.elements["Value"].text.to_f
30
- rates[code] = rate
17
+ add_rate code, rate
31
18
  end
32
19
  self
33
20
  end
34
21
 
35
- def store!
36
- rates.each do |code, rate|
37
- if Money::Currency.find(code)
38
- @bank.add_rate(code, :azn, rate)
39
- @bank.add_rate(:azn, code, 1.0/rate)
40
- end
41
- end
42
- @bank.save! if @bank.respond_to?(:save!)
43
- end
44
-
45
22
  def url
46
23
  @url ||= "http://cbar.az/currencies/#{formatted_date}.xml"
47
24
  end
48
25
 
49
- private
26
+ private
27
+
28
+ def add_rate code, rate
29
+ @bank.add_rate(code, :azn, rate)
30
+ @bank.add_rate(:azn, code, 1.0/rate)
31
+ rescue Currency::UnknownCurrency
32
+ end
50
33
 
51
34
  def formatted_date
52
35
  @formatted_date ||= @date.strftime("%d.%m.%Y")
@@ -59,10 +42,6 @@ class Money
59
42
  def xml
60
43
  @xml ||= REXML::Document.new(data)
61
44
  end
62
-
63
- def skip_code?(code)
64
- @only && !@only.include?(code)
65
- end
66
45
  end
67
46
  end
68
47
  end
@@ -2,7 +2,10 @@ namespace :bank_sources do
2
2
  namespace :az_central_bank do
3
3
  task :fetch => :environment do
4
4
  require "money/bank_sources/az_central_bank"
5
- Money::BankSources::AzCentralBank.new(:date => Time.zone.today).parse.store!
5
+ Money::BankSources::AzCentralBank.new(:date => Time.zone.today).parse
6
+ Money.default_bank.export_rates
6
7
  end
7
8
  end
9
+
10
+ task :fetch => "az_central_bank:fetch"
8
11
  end
@@ -1,3 +1,3 @@
1
1
  module MoneyBankSources
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0.alpha"
3
3
  end
@@ -5,34 +5,30 @@ $:.unshift(File.expand_path('../../lib', __FILE__))
5
5
  require "money/bank_sources/az_central_bank"
6
6
 
7
7
  class MoneyBankSourceAzCentralBankTest < MiniTest::Unit::TestCase
8
- include Money::BankSources
8
+ include Money::BankSources, Money::Bank
9
9
 
10
- def test_store_adds_direct_and_reverse_rates
11
- bank = MiniTest::Mock.new
12
-
13
- bank.expect(:add_rate, nil, ["USD", :azn, 2])
14
- bank.expect(:add_rate, nil, [:azn, "USD", 0.5])
15
- bank.expect(:add_rate, nil, ["EUR", :azn, 4])
16
- bank.expect(:add_rate, nil, [:azn, "EUR", 0.25])
17
-
18
- bank.expect(:save!, true)
10
+ def setup
11
+ @bank = VariableExchange.new
12
+ end
19
13
 
20
- source = AzCentralBank.new(:bank => bank)
21
- def source.rates
22
- { "USD" => 2, "EUR" => 4 }
23
- end
14
+ def test_ignores_unknown_currency
15
+ source = AzCentralBank.new :data => fixture(:unknown), :bank => @bank
16
+ refute Money::Currency.find(:sdr)
17
+ source.parse
18
+ end
24
19
 
25
- source.store!
26
- bank.verify
20
+ def test_adds_rates
21
+ source = AzCentralBank.new :data => fixture(:two), :bank => @bank
22
+ source.parse
23
+ assert_equal 0.0245, @bank.get_rate(:rub, :azn)
24
+ assert_equal 1.2166, @bank.get_rate(:gbp, :azn)
27
25
  end
28
26
 
29
- def test_store_unknown_currency_silently
30
- bank = Money::Bank::VariableExchange.new
31
- source = AzCentralBank.new(:bank => bank)
32
- def source.rates
33
- { "AAA" => 4 }
34
- end
35
- source.store!
27
+ def test_adds_reverse_rates
28
+ source = AzCentralBank.new :data => fixture(:two), :bank => @bank
29
+ source.parse
30
+ assert_in_delta 40.8163, @bank.get_rate(:azn, :rub)
31
+ assert_in_delta 0.8219, @bank.get_rate(:azn, :gbp)
36
32
  end
37
33
 
38
34
  def test_url
@@ -41,84 +37,9 @@ class MoneyBankSourceAzCentralBankTest < MiniTest::Unit::TestCase
41
37
  assert_equal "http://cbar.az/currencies/02.01.2011.xml", source.url
42
38
  end
43
39
 
44
- def test_url_with_default_date
45
- date = Date.today
46
- source = AzCentralBank.new
47
- assert_equal "http://cbar.az/currencies/#{date.strftime("%d.%m.%Y")}.xml", source.url
48
- end
49
-
50
- def test_parse
51
- source = AzCentralBank.new(:data => data)
52
- assert_equal source, source.parse
53
- assert_equal parsed_data, source.rates
54
- end
55
-
56
- def test_parse_only
57
- source = AzCentralBank.new(:only => "USD", :data => data)
58
- assert_equal({ "USD" => parsed_data["USD"] }, source.parse.rates)
59
- end
60
-
61
- def test_only_multiple
62
- source = AzCentralBank.new(:only => ["USD", "EUR"], :data => data)
63
- assert_equal({ "USD" => parsed_data["USD"], "EUR" => parsed_data["EUR"] }, source.parse.rates)
64
- end
65
-
66
- private
67
-
68
- def data
69
- @data ||= IO.read(File.expand_path("../fixtures/az_central_bank.xml", __FILE__))
70
- end
40
+ private
71
41
 
72
- def parsed_data
73
- {
74
- "ZAR"=>0.0963,
75
- "AED"=>0.2142,
76
- "IDR"=>0.0087,
77
- "SDR"=>1.2269,
78
- "KGS"=>0.0174,
79
- "CHF"=>0.8639,
80
- "HKD"=>0.1009,
81
- "NZD"=>0.6072,
82
- "INR"=>0.0159,
83
- "ARS"=>0.1873,
84
- "LVL"=>1.4877,
85
- "KWD"=>2.836,
86
- "EUR"=>1.0553,
87
- "MXN"=>0.0576,
88
- "UZS"=>0.0452,
89
- "GEL"=>0.4735,
90
- "EGP"=>0.1318,
91
- "SEK"=>0.1132,
92
- "CNY"=>0.1232,
93
- "SAR"=>0.2098,
94
- "RUB"=>0.0245,
95
- "LTL"=>0.3081,
96
- "LBP"=>0.0523,
97
- "DKK"=>0.1418,
98
- "KRW"=>0.0664,
99
- "TMT"=>0.2767,
100
- "KZT"=>0.0053,
101
- "BYR"=>0.0142,
102
- "TRY"=>0.429,
103
- "SGD"=>0.6037,
104
- "ILS"=>0.2115,
105
- "AUD"=>0.7663,
106
- "BRL"=>0.4292,
107
- "UAH"=>0.0983,
108
- "CAD"=>0.7619,
109
- "USD"=>0.7869,
110
- "TJS"=>0.1651,
111
- "PLN"=>0.2409,
112
- "JPY"=>1.0298,
113
- "CZK"=>0.0428,
114
- "IRR"=>0.0073,
115
- "MDL"=>0.066,
116
- "NOK"=>0.1342,
117
- "GBP"=>1.2166,
118
- "XPD"=>518.5671,
119
- "XAU"=>1329.0741,
120
- "XPT"=>1299.1719,
121
- "XAG"=>25.889
122
- }
42
+ def fixture name
43
+ @data ||= IO.read(File.expand_path("../fixtures/#{name}.xml", __FILE__))
123
44
  end
124
45
  end
@@ -0,0 +1,15 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <ValCurs Date="26.09.2011" Name="AZN məzənnələri" Description="Azərbaycan Respublikası Mərkəzi Bankının 26.09.2011 tarixdən etibarən xarici valyutaların və bank metallarının Azərbaycan manatına qarşı rəsmi məzənnələri bülleteni">
3
+ <ValType Type="Xarici valyutalar">
4
+ <Valute Code="RUB">
5
+ <Nominal>1</Nominal>
6
+ <Name>Rusiya rublu</Name>
7
+ <Value>0.0245 </Value>
8
+ </Valute>
9
+ <Valute Code="GBP">
10
+ <Nominal>1</Nominal>
11
+ <Name>İngilis funt sterlinqi</Name>
12
+ <Value>1.2166 </Value>
13
+ </Valute>
14
+ </ValType>
15
+ </ValCurs>
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <ValCurs Date="26.09.2011" Name="AZN məzənnələri" Description="Azərbaycan Respublikası Mərkəzi Bankının 26.09.2011 tarixdən etibarən xarici valyutaların və bank metallarının Azərbaycan manatına qarşı rəsmi məzənnələri bülleteni">
3
+ <ValType Type="Xarici valyutalar">
4
+ <Valute Code="SDR">
5
+ <Nominal>1</Nominal>
6
+ <Name>SDR (BVF-nun xüsusi borcalma hüquqları)</Name>
7
+ <Value>1.2269 </Value>
8
+ </Valute>
9
+ </ValType>
10
+ </ValCurs>
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money_bank_sources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.1.0.alpha
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Semyon Perepelitsa
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-20 00:00:00.000000000 Z
12
+ date: 2012-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: money
16
- requirement: &70210171490480 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '4'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70210171490480
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '4'
25
30
  description:
26
31
  email:
27
32
  - sema@sema.in
@@ -40,7 +45,8 @@ files:
40
45
  - lib/money_bank_sources/version.rb
41
46
  - money_bank_sources.gemspec
42
47
  - test/az_central_bank_test.rb
43
- - test/fixtures/az_central_bank.xml
48
+ - test/fixtures/two.xml
49
+ - test/fixtures/unknown.xml
44
50
  homepage: https://github.com/semaperepelitsa/money_bank_sources
45
51
  licenses: []
46
52
  post_install_message:
@@ -56,15 +62,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
62
  required_rubygems_version: !ruby/object:Gem::Requirement
57
63
  none: false
58
64
  requirements:
59
- - - ! '>='
65
+ - - ! '>'
60
66
  - !ruby/object:Gem::Version
61
- version: '0'
67
+ version: 1.3.1
62
68
  requirements: []
63
69
  rubyforge_project: money_bank_sources
64
- rubygems_version: 1.8.11
70
+ rubygems_version: 1.8.23
65
71
  signing_key:
66
72
  specification_version: 3
67
73
  summary: Adds bank sources to Money gem
68
74
  test_files:
69
75
  - test/az_central_bank_test.rb
70
- - test/fixtures/az_central_bank.xml
76
+ - test/fixtures/two.xml
77
+ - test/fixtures/unknown.xml
78
+ has_rdoc:
@@ -1,2 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <ValCurs Date="26.09.2011" Name="AZN məzənnələri" Description="Azərbaycan Respublikası Mərkəzi Bankının 26.09.2011 tarixdən etibarən xarici valyutaların və bank metallarının Azərbaycan manatına qarşı rəsmi məzənnələri bülleteni"><ValType Type="Xarici valyutalar"><Valute Code="ZAR"><Nominal>1</Nominal><Name>Cənubi Afrika randı</Name><Value>0.0963 </Value></Valute><Valute Code="AED"><Nominal>1</Nominal><Name>BƏƏ dirhəmi</Name><Value>0.2142 </Value></Valute><Valute Code="IDR"><Nominal>100</Nominal><Name>İndoneziya rupiası</Name><Value>0.0087 </Value></Valute><Valute Code="SDR"><Nominal>1</Nominal><Name>SDR (BVF-nun xüsusi borcalma hüquqları)</Name><Value>1.2269 </Value></Valute><Valute Code="KGS"><Nominal>1</Nominal><Name>Qırğız somu</Name><Value>0.0174 </Value></Valute><Valute Code="CHF"><Nominal>1</Nominal><Name>İsveçrə frankı</Name><Value>0.8639 </Value></Valute><Valute Code="HKD"><Nominal>1</Nominal><Name>Honq Konq dolları</Name><Value>0.1009 </Value></Valute><Valute Code="NZD"><Nominal>1</Nominal><Name>Yeni Zelandiya dolları</Name><Value>0.6072 </Value></Valute><Valute Code="INR"><Nominal>1</Nominal><Name>Hindistan rupisi</Name><Value>0.0159 </Value></Valute><Valute Code="ARS"><Nominal>1</Nominal><Name>Argentina pesosu</Name><Value>0.1873 </Value></Valute><Valute Code="LVL"><Nominal>1</Nominal><Name>Latviya latı</Name><Value>1.4877 </Value></Valute><Valute Code="KWD"><Nominal>1</Nominal><Name>Küveyt dinarı</Name><Value>2.8360 </Value></Valute><Valute Code="EUR"><Nominal>1</Nominal><Name>Avro</Name><Value>1.0553 </Value></Valute><Valute Code="MXN"><Nominal>1</Nominal><Name>Meksika pesosu</Name><Value>0.0576 </Value></Valute><Valute Code="UZS"><Nominal>100</Nominal><Name>Özbək somu</Name><Value>0.0452 </Value></Valute><Valute Code="GEL"><Nominal>1</Nominal><Name>Gürcü larisi</Name><Value>0.4735 </Value></Valute><Valute Code="EGP"><Nominal>1</Nominal><Name>Misir funtu</Name><Value>0.1318 </Value></Valute><Valute Code="SEK"><Nominal>1</Nominal><Name>İsveç kronu</Name><Value>0.1132 </Value></Valute><Valute Code="CNY"><Nominal>1</Nominal><Name>Çin yuanı</Name><Value>0.1232 </Value></Valute><Valute Code="SAR"><Nominal>1</Nominal><Name>Səudiyyə Ərəbistanı rialı</Name><Value>0.2098 </Value></Valute><Valute Code="RUR"><Nominal>1</Nominal><Name>Rusiya rublu</Name><Value>0.0245 </Value></Valute><Valute Code="LTL"><Nominal>1</Nominal><Name>Litva liti</Name><Value>0.3081 </Value></Valute><Valute Code="LBP"><Nominal>100</Nominal><Name>Livan funtu</Name><Value>0.0523 </Value></Valute><Valute Code="DKK"><Nominal>1</Nominal><Name>Danimarka kronu</Name><Value>0.1418 </Value></Valute><Valute Code="KRW"><Nominal>100</Nominal><Name>Cənubi Korea vonu</Name><Value>0.0664 </Value></Valute><Valute Code="TMT"><Nominal>1</Nominal><Name>Yeni türkmən manatı</Name><Value>0.2767 </Value></Valute><Valute Code="KZT"><Nominal>1</Nominal><Name>Qazaxıstan tengəsi</Name><Value>0.0053 </Value></Valute><Valute Code="BYR"><Nominal>100</Nominal><Name>Belarus rublu</Name><Value>0.0142 </Value></Valute><Valute Code="TRY"><Nominal>1</Nominal><Name>Türk lirəsi</Name><Value>0.4290 </Value></Valute><Valute Code="SGD"><Nominal>1</Nominal><Name>Sinqapur dolları</Name><Value>0.6037 </Value></Valute><Valute Code="ILS"><Nominal>1</Nominal><Name>İsrail şekeli</Name><Value>0.2115 </Value></Valute><Valute Code="AUD"><Nominal>1</Nominal><Name>Avstraliya dolları</Name><Value>0.7663 </Value></Valute><Valute Code="BRL"><Nominal>1</Nominal><Name>Braziliya rialı</Name><Value>0.4292 </Value></Valute><Valute Code="UAH"><Nominal>1</Nominal><Name>Ukrayna qrivnası</Name><Value>0.0983 </Value></Valute><Valute Code="CAD"><Nominal>1</Nominal><Name>Kanada dolları</Name><Value>0.7619 </Value></Valute><Valute Code="USD"><Nominal>1</Nominal><Name>ABŞ dolları</Name><Value>0.7869 </Value></Valute><Valute Code="TJS"><Nominal>1</Nominal><Name>Tacik somonisi</Name><Value>0.1651 </Value></Valute><Valute Code="PLN"><Nominal>1</Nominal><Name>Polşa zlotası</Name><Value>0.2409 </Value></Valute><Valute Code="JPY"><Nominal>100</Nominal><Name>Yapon yeni</Name><Value>1.0298 </Value></Valute><Valute Code="CZK"><Nominal>1</Nominal><Name>Çexiya kronu</Name><Value>0.0428 </Value></Valute><Valute Code="IRR"><Nominal>100</Nominal><Name>İran rialı</Name><Value>0.0073 </Value></Valute><Valute Code="MDL"><Nominal>1</Nominal><Name>Moldova leyi</Name><Value>0.0660 </Value></Valute><Valute Code="NOK"><Nominal>1</Nominal><Name>Norveç kronu</Name><Value>0.1342 </Value></Valute><Valute Code="GBP"><Nominal>1</Nominal><Name>İngilis funt sterlinqi</Name><Value>1.2166 </Value></Valute></ValType><ValType Type="Bank metalları"><Valute Code="XPD"><Nominal>1 t.u.</Nominal><Name>Palladium</Name><Value>518.5671 </Value></Valute><Valute Code="XAU"><Nominal>1 t.u.</Nominal><Name>Qızıl</Name><Value>1329.0741 </Value></Valute><Valute Code="XPT"><Nominal>1 t.u.</Nominal><Name>Platin</Name><Value>1299.1719 </Value></Valute><Valute Code="XAG"><Nominal>1 t.u.</Nominal><Name>Gümüş</Name><Value>25.8890 </Value></Valute></ValType></ValCurs>