currency_updater 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +15 -0
- data/Gemfile.lock +76 -0
- data/README.markdown +91 -0
- data/autotest/discover.rb +1 -0
- data/init.rb +1 -0
- data/lib/currency_updater/currencies.rb +73 -0
- data/lib/currency_updater/currency.rb +34 -0
- data/lib/currency_updater/updater.rb +62 -0
- data/lib/currency_updater.rb +30 -0
- data/remote_spec/remote_spec.rb +79 -0
- data/spec/currency_updater/currencies_spec.rb +31 -0
- data/spec/currency_updater/updater_spec.rb +154 -0
- data/spec/spec_helper.rb +23 -0
- metadata +82 -0
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem "i18n"
|
4
|
+
gem "iso_country_codes"
|
5
|
+
gem "yajl-ruby"
|
6
|
+
gem "builder", "~> 2.1.2"
|
7
|
+
gem "actionpack", "~> 3.0.3"
|
8
|
+
|
9
|
+
group :test, :development do
|
10
|
+
gem "rspec"
|
11
|
+
gem "autotest"
|
12
|
+
gem "autotest-fsevent" if RUBY_PLATFORM =~ /darwin/
|
13
|
+
gem "ruby-debug19"
|
14
|
+
gem "mocha"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionpack (3.0.3)
|
6
|
+
activemodel (= 3.0.3)
|
7
|
+
activesupport (= 3.0.3)
|
8
|
+
builder (~> 2.1.2)
|
9
|
+
erubis (~> 2.6.6)
|
10
|
+
i18n (~> 0.4)
|
11
|
+
rack (~> 1.2.1)
|
12
|
+
rack-mount (~> 0.6.13)
|
13
|
+
rack-test (~> 0.5.6)
|
14
|
+
tzinfo (~> 0.3.23)
|
15
|
+
activemodel (3.0.3)
|
16
|
+
activesupport (= 3.0.3)
|
17
|
+
builder (~> 2.1.2)
|
18
|
+
i18n (~> 0.4)
|
19
|
+
activesupport (3.0.3)
|
20
|
+
archive-tar-minitar (0.5.2)
|
21
|
+
autotest (4.4.4)
|
22
|
+
autotest-fsevent (0.2.3)
|
23
|
+
sys-uname
|
24
|
+
builder (2.1.2)
|
25
|
+
columnize (0.3.2)
|
26
|
+
diff-lcs (1.1.2)
|
27
|
+
erubis (2.6.6)
|
28
|
+
abstract (>= 1.0.0)
|
29
|
+
i18n (0.4.2)
|
30
|
+
iso_country_codes (0.2.2)
|
31
|
+
linecache19 (0.5.11)
|
32
|
+
ruby_core_source (>= 0.1.4)
|
33
|
+
mocha (0.9.9)
|
34
|
+
rake
|
35
|
+
rack (1.2.1)
|
36
|
+
rack-mount (0.6.13)
|
37
|
+
rack (>= 1.0.0)
|
38
|
+
rack-test (0.5.6)
|
39
|
+
rack (>= 1.0)
|
40
|
+
rake (0.8.7)
|
41
|
+
rspec (2.1.0)
|
42
|
+
rspec-core (~> 2.1.0)
|
43
|
+
rspec-expectations (~> 2.1.0)
|
44
|
+
rspec-mocks (~> 2.1.0)
|
45
|
+
rspec-core (2.1.0)
|
46
|
+
rspec-expectations (2.1.0)
|
47
|
+
diff-lcs (~> 1.1.2)
|
48
|
+
rspec-mocks (2.1.0)
|
49
|
+
ruby-debug-base19 (0.11.24)
|
50
|
+
columnize (>= 0.3.1)
|
51
|
+
linecache19 (>= 0.5.11)
|
52
|
+
ruby_core_source (>= 0.1.4)
|
53
|
+
ruby-debug19 (0.11.6)
|
54
|
+
columnize (>= 0.3.1)
|
55
|
+
linecache19 (>= 0.5.11)
|
56
|
+
ruby-debug-base19 (>= 0.11.19)
|
57
|
+
ruby_core_source (0.1.4)
|
58
|
+
archive-tar-minitar (>= 0.5.2)
|
59
|
+
sys-uname (0.8.4)
|
60
|
+
tzinfo (0.3.23)
|
61
|
+
yajl-ruby (0.7.8)
|
62
|
+
|
63
|
+
PLATFORMS
|
64
|
+
ruby
|
65
|
+
|
66
|
+
DEPENDENCIES
|
67
|
+
actionpack (~> 3.0.3)
|
68
|
+
autotest
|
69
|
+
autotest-fsevent
|
70
|
+
builder (~> 2.1.2)
|
71
|
+
i18n
|
72
|
+
iso_country_codes
|
73
|
+
mocha
|
74
|
+
rspec
|
75
|
+
ruby-debug19
|
76
|
+
yajl-ruby
|
data/README.markdown
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# Currency Updater
|
2
|
+
|
3
|
+
The currency updater loads currency rates based on google search.
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
All rates conversions are based on the Euro (EUR).
|
8
|
+
This version doesn't use a database.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Standalone:
|
13
|
+
|
14
|
+
gem install currency_updater
|
15
|
+
|
16
|
+
With bundler:
|
17
|
+
|
18
|
+
gem "currency_updater"
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### Fetching Currencies
|
23
|
+
|
24
|
+
In order to fetch all currencies defined by ISO 4217:
|
25
|
+
|
26
|
+
currencies = CurrencyUpdater.new
|
27
|
+
|
28
|
+
or to fetch only one currency:
|
29
|
+
|
30
|
+
currencies = CurrencyUpdater.new(USD)
|
31
|
+
|
32
|
+
or to fetch some currencies:
|
33
|
+
|
34
|
+
currencies = CurrencyUpdater.new([USD,GBP,RUB])
|
35
|
+
|
36
|
+
### Accessing a currency
|
37
|
+
|
38
|
+
To access the currency rate, code or name:
|
39
|
+
|
40
|
+
currencies.usd.rate # => 1.333
|
41
|
+
currencies.usd.code # => "USD"
|
42
|
+
currencies.usd.name # => I18n.t('currency.usd')
|
43
|
+
|
44
|
+
### Output currencies
|
45
|
+
|
46
|
+
You're able to convert the currencies into different formats:
|
47
|
+
|
48
|
+
currencies.to_json
|
49
|
+
currencies.to_yaml
|
50
|
+
currencies.to_xml
|
51
|
+
|
52
|
+
Have a look at the remote specs to get an idea how the output looks like:
|
53
|
+
|
54
|
+
ruby remote_spec/remote_spec.rb
|
55
|
+
|
56
|
+
## Changelog
|
57
|
+
|
58
|
+
* 2010-11-19: Initial commit
|
59
|
+
|
60
|
+
## Contributors
|
61
|
+
|
62
|
+
* Jan Riethmayer (@riethmayer, maintainer)
|
63
|
+
|
64
|
+
## Credits
|
65
|
+
|
66
|
+
Thanks to [Alex Rabarts](http://statelesssystems.com) for his [iso_country_codes](https://github.com/alexrabarts/iso_country_codes).
|
67
|
+
|
68
|
+
## LICENSE
|
69
|
+
|
70
|
+
(The MIT License)
|
71
|
+
|
72
|
+
Copyright (c) 2010 [urbanvention](http://urbanvention.com)
|
73
|
+
|
74
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
75
|
+
a copy of this software and associated documentation files (the
|
76
|
+
'Software'), to deal in the Software without restriction, including
|
77
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
78
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
79
|
+
permit persons to whom the Software is furnished to do so, subject to
|
80
|
+
the following conditions:
|
81
|
+
|
82
|
+
The above copyright notice and this permission notice shall be
|
83
|
+
included in all copies or substantial portions of the Software.
|
84
|
+
|
85
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
88
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
89
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
90
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
91
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1 @@
|
|
1
|
+
Autotest.add_discovery { "rspec2" }
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'currency_updater'
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module CurrencyUpdater
|
2
|
+
|
3
|
+
class Currencies
|
4
|
+
# EUR is unsupported because it's our reference currency
|
5
|
+
# TRY is unsupported because it's a method used in actionpack
|
6
|
+
# the other currencies are not supported because of google
|
7
|
+
UNSUPPORTED = %w(EUR TRY AFN ALL AMD AOA AZN BAM BIF BMD BSD BTN CDF CUP ETB FIM FKP GEL GNF GYD IMP JEP KGS KMF LRD LSL LYD MGA MMK MNT MRO MZN SBD SHP SOS STD TJS TMM TOP TVD VUV WST ZWD)
|
8
|
+
|
9
|
+
@@currencies = {}
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def add(currency)
|
13
|
+
currency = currency.to_s.downcase
|
14
|
+
result = @@currencies[currency] = Currency.new(currency)
|
15
|
+
define_currency(currency)
|
16
|
+
result
|
17
|
+
end
|
18
|
+
|
19
|
+
def all
|
20
|
+
@@currencies.values
|
21
|
+
end
|
22
|
+
|
23
|
+
def include?(currency)
|
24
|
+
@@currencies.keys.include?(currency.downcase)
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete
|
28
|
+
@@currencies = {}
|
29
|
+
end
|
30
|
+
|
31
|
+
# for better guessing
|
32
|
+
alias_method :clear, :delete
|
33
|
+
|
34
|
+
def all_codes
|
35
|
+
result = IsoCountryCodes.all.map(&:currency).sort
|
36
|
+
result = result - UNSUPPORTED
|
37
|
+
result.uniq
|
38
|
+
end
|
39
|
+
|
40
|
+
def define_currency(currency)
|
41
|
+
define_method(currency) do
|
42
|
+
@@currencies[currency]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def unsupported?(currency)
|
47
|
+
UNSUPPORTED.include?(currency)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_json
|
52
|
+
@@currencies.to_json
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_xml
|
56
|
+
xml = Builder::XmlMarkup.new(:indent=>2)
|
57
|
+
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
|
58
|
+
xml.currencies do
|
59
|
+
@@currencies.values.each do |currency|
|
60
|
+
xml.currency(:name => currency.currency) do
|
61
|
+
xml.code(currency.code)
|
62
|
+
xml.rate(currency.rate)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
xml.target!
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_yaml
|
70
|
+
@@currencies.to_yaml
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
module CurrencyUpdater
|
3
|
+
class Currency
|
4
|
+
attr_accessor :rate
|
5
|
+
attr_accessor :currency
|
6
|
+
attr_accessor :code
|
7
|
+
def initialize(currency)
|
8
|
+
raise "Need a 3LC for currency #{currency}" unless currency.length == 3
|
9
|
+
@currency = currency
|
10
|
+
@code = currency.upcase
|
11
|
+
@rate = 1.0
|
12
|
+
end
|
13
|
+
|
14
|
+
def name
|
15
|
+
::I18n.t("currency.#{@currency}")
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_json
|
19
|
+
{ :rate => rate,
|
20
|
+
:code => code,
|
21
|
+
:currency => currency }.to_json
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_xml
|
25
|
+
xml = Builder::XmlMarkup.new(:indent => 2)
|
26
|
+
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
|
27
|
+
xml.currency(:name => currency) do
|
28
|
+
xml.code(code)
|
29
|
+
xml.rate(rate)
|
30
|
+
end
|
31
|
+
xml.target!
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module CurrencyUpdater
|
2
|
+
class Updater
|
3
|
+
class << self
|
4
|
+
def select_country_codes(currency)
|
5
|
+
@@currency_codes = if currency.nil?
|
6
|
+
Currencies.all_codes
|
7
|
+
else
|
8
|
+
Array.new([currency]).flatten
|
9
|
+
end
|
10
|
+
initialize_currencies
|
11
|
+
update_currencies
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize_currencies
|
15
|
+
Currencies.clear
|
16
|
+
@@currency_codes.each do |currency|
|
17
|
+
Currencies.add(currency)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def update_currencies
|
22
|
+
currencies = Currencies.all
|
23
|
+
currencies.each do |currency|
|
24
|
+
rate = search_rate_for(currency.code)
|
25
|
+
currency.rate = rate
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def search_rate_for(currency)
|
30
|
+
raise "invalid currency" unless currency
|
31
|
+
CurrencyUpdater.rate_finder.call(currency)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
#----------------------------------------------------------------------#
|
35
|
+
# instance methods
|
36
|
+
#----------------------------------------------------------------------#
|
37
|
+
def initialize(currency = nil)
|
38
|
+
@currencies = Currencies.new
|
39
|
+
Updater.select_country_codes(currency)
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_missing(method,*args,&block)
|
43
|
+
if @currencies.respond_to?(method)
|
44
|
+
@currencies.send(method,*args,&block)
|
45
|
+
else
|
46
|
+
super
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_json
|
51
|
+
@currencies.to_json
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_xml
|
55
|
+
@currencies.to_xml
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_yaml
|
59
|
+
@currencies.to_yaml
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'iso_country_codes'
|
2
|
+
require 'yajl/json_gem'
|
3
|
+
require 'builder'
|
4
|
+
|
5
|
+
module CurrencyUpdater
|
6
|
+
|
7
|
+
autoload :Currency, 'currency_updater/currency'
|
8
|
+
autoload :Currencies, 'currency_updater/currencies'
|
9
|
+
autoload :Updater, 'currency_updater/updater'
|
10
|
+
|
11
|
+
# Default operation for fetching a currency
|
12
|
+
mattr_accessor :rate_finder
|
13
|
+
@@rate_finder = lambda {|currency|
|
14
|
+
client = Net::HTTP.new("www.google.de")
|
15
|
+
resp = client.get("/search?hl=de&q=1+EUR+to+#{currency}&btnG=Suche&meta=")
|
16
|
+
text_only = resp.body.gsub(/<[^>]*>/, "")
|
17
|
+
match = text_only.match(/1\s+Euro\s+=\s+([0-9\.\s]*)/mix)
|
18
|
+
rate = if match
|
19
|
+
match[1]
|
20
|
+
else
|
21
|
+
warn "currency #{currency} is unsupported"
|
22
|
+
"1.00"
|
23
|
+
end
|
24
|
+
rate.gsub!(/\s+/, "").to_f
|
25
|
+
}
|
26
|
+
|
27
|
+
def self.setup
|
28
|
+
yield self
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup
|
4
|
+
|
5
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec', 'spec_helper.rb'))
|
6
|
+
|
7
|
+
## These test cases will fail because the conversion rate is wrong
|
8
|
+
## This is intentionall to let you see some output
|
9
|
+
describe "RemoteSpecs" do
|
10
|
+
describe "Fetching only USD" do
|
11
|
+
before :all do
|
12
|
+
@cu = CurrencyUpdater.new("USD")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should respond to USD" do
|
16
|
+
currency = @cu.usd
|
17
|
+
currency.rate.should_not == 1.000
|
18
|
+
currency.code.should == "USD"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "Fetching multiple currencies" do
|
23
|
+
before :all do
|
24
|
+
@cu = CurrencyUpdater.new(["USD", "GBP"])
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should respond to USD" do
|
28
|
+
@cu.usd.rate.should == 1.3333
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should respond to GBP" do
|
32
|
+
@cu.gbp.rate.should == 1.3333
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should not respond to RUB" do
|
36
|
+
lambda {@cu.rub}.should raise_error(NoMethodError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "Output Currencies" do
|
41
|
+
context "to_json" do
|
42
|
+
it "should output one currency" do
|
43
|
+
cu = CurrencyUpdater.new("USD")
|
44
|
+
cu.to_json.should == "{\"usd\":{\"rate\":1.3333,\"code\":\"USD\",\"currency\":\"usd\"}}"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should output multiple currencies" do
|
48
|
+
cu = CurrencyUpdater.new(["USD", "GBP"])
|
49
|
+
cu.to_json.should == "{\"usd\":{\"rate\":1.3333,\"code\":\"USD\",\"currency\":\"usd\"},\"gbp\":{\"rate\":1.3333,\"code\":\"GBP\",\"currency\":\"gbp\"}}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "to_xml" do
|
54
|
+
it "should output one currency" do
|
55
|
+
cu = CurrencyUpdater.new("USD")
|
56
|
+
cu.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<currencies>\n <currency name=\"usd\">\n <code>USD</code>\n <rate>1.3333</rate>\n </currency>\n</currencies>\n"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should output multiple currencies" do
|
60
|
+
cu = CurrencyUpdater.new(["USD","GBP"])
|
61
|
+
|
62
|
+
cu.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<currencies>\n <currency name=\"usd\">\n <code>USD</code>\n <rate>1.3333</rate>\n </currency>\n <currency name=\"gbp\">\n <code>GBP</code>\n <rate>1.3333</rate>\n </currency>\n</currencies>\n"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "to_yaml" do
|
67
|
+
it "should output one currency" do
|
68
|
+
cu = CurrencyUpdater.new("USD")
|
69
|
+
cu.to_yaml.should == "--- \nusd: !ruby/object:Currency \n code: USD\n currency: usd\n rate: 1.3333\n"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should output multiple currencies" do
|
73
|
+
cu = CurrencyUpdater.new(["USD","GBP"])
|
74
|
+
|
75
|
+
cu.to_yaml.should == "--- \nusd: !ruby/object:Currency \n code: USD\n currency: usd\n rate: 1.3333\ngbp: !ruby/object:Currency \n code: GBP\n currency: gbp\n rate: 1.3333\n"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
|
2
|
+
|
3
|
+
module CurrencyUpdater
|
4
|
+
describe Currencies do
|
5
|
+
context "Adding currencies" do
|
6
|
+
before :each do
|
7
|
+
Currencies.delete
|
8
|
+
end
|
9
|
+
it "should add usd" do
|
10
|
+
Currencies.add("USD").should be_instance_of(Currency)
|
11
|
+
end
|
12
|
+
it "should include usd" do
|
13
|
+
Currencies.add("USD")
|
14
|
+
Currencies.should include("USD")
|
15
|
+
end
|
16
|
+
it "should include usd in #all" do
|
17
|
+
Currencies.add("USD")
|
18
|
+
Currencies.all.map(&:code).should include("USD")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should have no values initially" do
|
22
|
+
Currencies.all.should == []
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should have an xml output" do
|
26
|
+
Currencies.add("USD")
|
27
|
+
Currencies.new.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<currencies>\n <currency name=\"usd\">\n <code>USD</code>\n <rate>1.0</rate>\n </currency>\n</currencies>\n"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..','spec_helper.rb')
|
2
|
+
|
3
|
+
module CurrencyUpdater
|
4
|
+
describe Updater do
|
5
|
+
# mock the http_access (comment it out to see actual values)
|
6
|
+
def Updater.search_rate_for(foo)
|
7
|
+
return 1.3333
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "Fetching only USD" do
|
11
|
+
before :each do
|
12
|
+
@cu = Updater.new("USD")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should respond to USD" do
|
16
|
+
currency = @cu.usd
|
17
|
+
currency.rate.should_not == 1.000
|
18
|
+
currency.code.should == "USD"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should not respond to RUB" do
|
22
|
+
lambda {@cu.rub}.should raise_error(NoMethodError)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "Fetching multiple currencies" do
|
27
|
+
before :each do
|
28
|
+
@cu = Updater.new(["USD", "GBP"])
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should respond to USD" do
|
32
|
+
@cu.usd.rate.should == 1.3333
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should respond to GBP" do
|
36
|
+
@cu.gbp.rate.should == 1.3333
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not respond to RUB" do
|
40
|
+
lambda {@cu.rub}.should raise_error(NoMethodError)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "Fetching all currencies" do
|
45
|
+
|
46
|
+
before :each do
|
47
|
+
@cu = Updater.new
|
48
|
+
@codes = Currencies.all_codes
|
49
|
+
end
|
50
|
+
|
51
|
+
# to avoid bad method_missing behaviour
|
52
|
+
it "should not include all" do
|
53
|
+
@codes.should_not include("all")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should fetch all currencies defined by ISO 4217" do
|
57
|
+
@codes.each do |code|
|
58
|
+
currency = @cu.send code.downcase rescue warn("code #{code} unsupported")
|
59
|
+
currency.should_not be_nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should override all default rates" do
|
64
|
+
@codes.each do |code|
|
65
|
+
currency = @cu.send code.downcase
|
66
|
+
currency.rate.should_not == 1.0000
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should set all three letter codes properly" do
|
71
|
+
@codes.each do |code|
|
72
|
+
currency = @cu.send code.downcase
|
73
|
+
currency.code.should == code
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should translate all names" do
|
78
|
+
@codes.each do |code|
|
79
|
+
code_name = code.downcase
|
80
|
+
currency = @cu.send code_name
|
81
|
+
currency.name.should == I18n.t("currency.#{code_name}")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should always include USD" do
|
86
|
+
@cu.usd.rate.should == 1.3333
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should always include RUB" do
|
90
|
+
@cu.rub.rate.should == 1.3333
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should always include GBP" do
|
94
|
+
@cu.gbp.rate.should == 1.3333
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "Output Currencies" do
|
99
|
+
context "to_json" do
|
100
|
+
it "should output one currency" do
|
101
|
+
cu = Updater.new("USD")
|
102
|
+
cu.to_json.should == "{\"usd\":{\"rate\":1.3333,\"code\":\"USD\",\"currency\":\"usd\"}}"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should output multiple currencies" do
|
106
|
+
cu = Updater.new(["USD", "GBP"])
|
107
|
+
cu.to_json.should == "{\"usd\":{\"rate\":1.3333,\"code\":\"USD\",\"currency\":\"usd\"},\"gbp\":{\"rate\":1.3333,\"code\":\"GBP\",\"currency\":\"gbp\"}}"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should output all currencies" do
|
111
|
+
cu = Updater.new
|
112
|
+
cu.to_json.should match(/\"rub\":{\"rate\":1\.3333,\"code\":\"RUB\",\"currency\":\"rub\"}/)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
context "to_xml" do
|
117
|
+
it "should output one currency" do
|
118
|
+
cu = Updater.new("USD")
|
119
|
+
cu.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<currencies>\n <currency name=\"usd\">\n <code>USD</code>\n <rate>1.3333</rate>\n </currency>\n</currencies>\n"
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should output multiple currencies" do
|
123
|
+
cu = Updater.new(["USD","GBP"])
|
124
|
+
|
125
|
+
cu.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<currencies>\n <currency name=\"usd\">\n <code>USD</code>\n <rate>1.3333</rate>\n </currency>\n <currency name=\"gbp\">\n <code>GBP</code>\n <rate>1.3333</rate>\n </currency>\n</currencies>\n"
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should output all currencies" do
|
129
|
+
cu = Updater.new
|
130
|
+
cu.to_xml.should match(/rub/)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "to_yaml" do
|
135
|
+
it "should output one currency" do
|
136
|
+
Currencies.clear
|
137
|
+
cu = Updater.new("USD")
|
138
|
+
cu.to_yaml.should == "--- \nusd: !ruby/object:CurrencyUpdater::Currency \n code: USD\n currency: usd\n rate: 1.3333\n"
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should output multiple currencies" do
|
142
|
+
cu = Updater.new(["USD","GBP"])
|
143
|
+
|
144
|
+
cu.to_yaml.should == "--- \nusd: !ruby/object:CurrencyUpdater::Currency \n code: USD\n currency: usd\n rate: 1.3333\ngbp: !ruby/object:CurrencyUpdater::Currency \n code: GBP\n currency: gbp\n rate: 1.3333\n"
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should output all currencies" do
|
148
|
+
cu = Updater.new
|
149
|
+
cu.to_yaml.should match(/rub/)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
gem "actionpack", "3.0.3"
|
5
|
+
gem "activemodel", "3.0.3"
|
6
|
+
|
7
|
+
require 'active_model'
|
8
|
+
require 'action_controller'
|
9
|
+
|
10
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib', 'currency_updater')
|
11
|
+
require 'currency_updater'
|
12
|
+
|
13
|
+
begin
|
14
|
+
require 'ruby-debug'
|
15
|
+
rescue LoadError
|
16
|
+
end
|
17
|
+
|
18
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
|
19
|
+
I18n.default_locale = :en
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.mock_with :mocha
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: currency_updater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jan Riethmayer
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-20 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Updating currency rates made easy.
|
22
|
+
email: jan@urbanvention.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- Gemfile
|
31
|
+
- Gemfile.lock
|
32
|
+
- README.markdown
|
33
|
+
- init.rb
|
34
|
+
- lib/currency_updater.rb
|
35
|
+
- lib/currency_updater/currencies.rb
|
36
|
+
- lib/currency_updater/currency.rb
|
37
|
+
- lib/currency_updater/updater.rb
|
38
|
+
- autotest/discover.rb
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
- spec/currency_updater/currencies_spec.rb
|
41
|
+
- spec/currency_updater/updater_spec.rb
|
42
|
+
- remote_spec/remote_spec.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/urbanvention/currency_updater
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 1
|
59
|
+
- 8
|
60
|
+
- 7
|
61
|
+
version: 1.8.7
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Updating currency rates made easy.
|
77
|
+
test_files:
|
78
|
+
- autotest/discover.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
- spec/currency_updater/currencies_spec.rb
|
81
|
+
- spec/currency_updater/updater_spec.rb
|
82
|
+
- remote_spec/remote_spec.rb
|