money_bank_sources 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in money_bank_sources.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake/testtask"
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = Dir['test/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1 @@
1
+ require "money/bank_sources/cbar_az"
@@ -0,0 +1,68 @@
1
+ require "open-uri"
2
+ require "rexml/document"
3
+
4
+ class Money
5
+ module BankSources
6
+ class CbarAz
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
+ def initialize(options = {})
15
+ @date = options[:date] || Date.today
16
+ @bank = options[:bank] || Money.default_bank
17
+ @only = options[:only] ? Array(options[:only]) : nil
18
+ @data = options[:data]
19
+ @rates = {}
20
+ end
21
+
22
+ def parse
23
+ xml.elements.each("ValCurs/ValType/Valute") do |currency|
24
+ code = currency.attributes["Code"]
25
+
26
+ code = CODE_MAP.fetch(code, code)
27
+ next if skip_code?(code)
28
+
29
+ rate = currency.elements["Value"].text.to_f
30
+ rates[code] = rate
31
+ end
32
+ self
33
+ end
34
+
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
+ def url
46
+ @url ||= "http://cbar.az/currencies/#{formatted_date}.xml"
47
+ end
48
+
49
+ private
50
+
51
+ def formatted_date
52
+ @formatted_date ||= @date.strftime("%d.%m.%Y")
53
+ end
54
+
55
+ def data
56
+ @data ||= open(url).read
57
+ end
58
+
59
+ def xml
60
+ @xml ||= REXML::Document.new(data)
61
+ end
62
+
63
+ def skip_code?(code)
64
+ @only && !@only.include?(code)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,2 @@
1
+ require "money/bank_sources"
2
+ require "money_bank_sources/railtie" if defined? Rails
@@ -0,0 +1,7 @@
1
+ module MoneyBankSources
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "money_bank_sources/tasks/cbar_az.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ namespace :bank_sources do
2
+ namespace :cbar_az do
3
+ task :fetch => :environment do
4
+ Money::BankSources::CbarAz.new(:date => Time.zone.today).parse.store!
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module MoneyBankSources
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "money_bank_sources/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "money_bank_sources"
7
+ s.version = MoneyBankSources::VERSION
8
+ s.authors = ["Semyon Perepelitsa"]
9
+ s.email = ["sema@sema.in"]
10
+ s.homepage = "https://github.com/semaperepelitsa/money_bank_sources"
11
+ s.summary = %q{Adds bank sources to Money gem}
12
+ # s.description = %q{TODO: Write a gem description}
13
+
14
+ s.rubyforge_project = "money_bank_sources"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "money"
22
+
23
+ # specify any dependencies here; for example:
24
+ # s.add_development_dependency "rspec"
25
+ # s.add_runtime_dependency "rest-client"
26
+ end
@@ -0,0 +1,124 @@
1
+ require "bundler/setup"
2
+ require 'minitest/autorun'
3
+ require 'money'
4
+ $:.unshift(File.expand_path('../../lib', __FILE__))
5
+ require "money/bank_sources/cbar_az"
6
+
7
+ class MoneyBankSourceCbarAzTest < MiniTest::Unit::TestCase
8
+ include Money::BankSources
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)
19
+
20
+ source = CbarAz.new(:bank => bank)
21
+ def source.rates
22
+ { "USD" => 2, "EUR" => 4 }
23
+ end
24
+
25
+ source.store!
26
+ bank.verify
27
+ end
28
+
29
+ def test_store_unknown_currency_silently
30
+ bank = Money::Bank::VariableExchange.new
31
+ source = CbarAz.new(:bank => bank)
32
+ def source.rates
33
+ { "AAA" => 4 }
34
+ end
35
+ source.store!
36
+ end
37
+
38
+ def test_url
39
+ date = Date.new(2011, 01, 02)
40
+ source = CbarAz.new(:date => date)
41
+ assert_equal "http://cbar.az/currencies/02.01.2011.xml", source.url
42
+ end
43
+
44
+ def test_url_with_default_date
45
+ date = Date.today
46
+ source = CbarAz.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 = CbarAz.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 = CbarAz.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 = CbarAz.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/cbar_az.xml", __FILE__))
70
+ end
71
+
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
+ }
123
+ end
124
+ end
@@ -0,0 +1,2 @@
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>
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: money_bank_sources
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Semyon Perepelitsa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: money
16
+ requirement: &70311994243560 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70311994243560
25
+ description:
26
+ email:
27
+ - sema@sema.in
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - lib/money/bank_sources.rb
36
+ - lib/money/bank_sources/cbar_az.rb
37
+ - lib/money_bank_sources.rb
38
+ - lib/money_bank_sources/railtie.rb
39
+ - lib/money_bank_sources/tasks/cbar_az.rake
40
+ - lib/money_bank_sources/version.rb
41
+ - money_bank_sources.gemspec
42
+ - test/cbar_az_test.rb
43
+ - test/fixtures/cbar_az.xml
44
+ homepage: https://github.com/semaperepelitsa/money_bank_sources
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project: money_bank_sources
64
+ rubygems_version: 1.8.11
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Adds bank sources to Money gem
68
+ test_files:
69
+ - test/cbar_az_test.rb
70
+ - test/fixtures/cbar_az.xml
71
+ has_rdoc: