currency_switcher 0.0.1 → 0.0.2

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/Manifest CHANGED
@@ -1,3 +1,4 @@
1
+ Manifest
1
2
  README.md
2
3
  Rakefile
3
4
  lib/currency_switcher.rb
@@ -7,4 +8,3 @@ test/helper.rb
7
8
  test/html/eur_to_usd.html
8
9
  test/html/usd_to_gbp.html
9
10
  test/test_currency_switcher.rb
10
- Manifest
data/README.md CHANGED
@@ -10,16 +10,31 @@ $ gem install currency_switcher
10
10
 
11
11
  ## Usage
12
12
 
13
- From the available list of currencies you can create methods in a format specified below and call them on Fixnums:
13
+ There are two ways of using the library:
14
+
15
+ 1) From the available list of currencies you can create methods in a format specified below and call them on Fixnums:
14
16
 
15
17
  'from_currency'_to_'to_currency'
16
18
 
17
19
  For example:
18
20
 
19
21
  require 'currency_switcher'
20
- 3.usd_to_gbp # => converts 3 USD to GBP and returns 1.94
21
- 15.eur_to_aud # => converts 15 EURO to Australian Dollar and returns 19.61
22
22
 
23
+ # Convert 3 US Dollars to British Pounds
24
+ 3.usd_to_gbp
25
+
26
+ # Convert 15 Euros to Australian Dollars
27
+ 15.eur_to_aud
28
+
29
+ 2) Pass currencies as the arguments:
30
+
31
+ For example:
32
+
33
+ require 'currency_switcher'
34
+
35
+ # Convert 3 US Dollars to British Pounds
36
+ 3.exchange("usd", "gbp")
37
+
23
38
  ## Available Currencies
24
39
 
25
40
  aed => United Arab Emirates Dirham
data/Rakefile CHANGED
@@ -2,11 +2,11 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('currency_switcher', '0.0.1') do |p|
5
+ Echoe.new('currency_switcher', '0.0.2') do |p|
6
6
  p.description = "Convert currencies using exchange rates from http://exchange-rates.org"
7
7
  p.url = "http://github.com/staskie/currency_switcher"
8
8
  p.author = "Dominik Staskiewicz"
9
9
  p.email = "stadominik@gmail.com"
10
10
  p.ignore_pattern = ["tmp/*", "script/*"]
11
11
  p.development_dependencies = []
12
- end
12
+ end
@@ -2,27 +2,26 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{currency_switcher}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Dominik Staskiewicz"]
9
9
  s.cert_chain = ["/Users/dominik/Documents/keys/gem-public_cert.pem"]
10
- s.date = %q{2011-01-07}
10
+ s.date = %q{2011-10-09}
11
11
  s.description = %q{Convert currencies using exchange rates from http://exchange-rates.org}
12
12
  s.email = %q{stadominik@gmail.com}
13
13
  s.extra_rdoc_files = ["README.md", "lib/currency_switcher.rb", "lib/currency_switcher/currencies.rb", "lib/currency_switcher/ext/fixnum.rb"]
14
- s.files = ["README.md", "Rakefile", "lib/currency_switcher.rb", "lib/currency_switcher/currencies.rb", "lib/currency_switcher/ext/fixnum.rb", "test/helper.rb", "test/html/eur_to_usd.html", "test/html/usd_to_gbp.html", "test/test_currency_switcher.rb", "Manifest", "currency_switcher.gemspec"]
14
+ s.files = ["Manifest", "README.md", "Rakefile", "lib/currency_switcher.rb", "lib/currency_switcher/currencies.rb", "lib/currency_switcher/ext/fixnum.rb", "test/helper.rb", "test/html/eur_to_usd.html", "test/html/usd_to_gbp.html", "test/test_currency_switcher.rb", "currency_switcher.gemspec"]
15
15
  s.homepage = %q{http://github.com/staskie/currency_switcher}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Currency_switcher", "--main", "README.md"]
17
17
  s.require_paths = ["lib"]
18
18
  s.rubyforge_project = %q{currency_switcher}
19
- s.rubygems_version = %q{1.3.7}
19
+ s.rubygems_version = %q{1.6.2}
20
20
  s.signing_key = %q{/Users/dominik/Documents/keys/gem-private_key.pem}
21
21
  s.summary = %q{Convert currencies using exchange rates from http://exchange-rates.org}
22
22
  s.test_files = ["test/test_currency_switcher.rb"]
23
23
 
24
24
  if s.respond_to? :specification_version then
25
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
26
25
  s.specification_version = 3
27
26
 
28
27
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -1,4 +1,20 @@
1
1
  class Numeric
2
+ # Exchange a value using from and to currencies
3
+ #
4
+ # from - From currency
5
+ # to - To currency
6
+ #
7
+ # Example
8
+ #
9
+ # 5.exchange("gbp", "usd")
10
+ #
11
+ # Returns float value of currency exchange result
12
+ # Raises StandardError if any currency is invalid
13
+ # or could not work out the exchange rate
14
+ def exchange(from, to)
15
+ CurrencySwitcher.exchange(self, from, to)
16
+ end
17
+
2
18
  # Overriding method_missing to add CurrencySwitcher methods
3
19
  #
4
20
  # sym - standard method_missing argument
@@ -52,11 +52,32 @@ module CurrencySwitcher
52
52
  #
53
53
  # Returns true if botch currencies are valid or false if one of them is invalid
54
54
  def self.both_currencies_valid?
55
- CURRENCIES.has_key?(from_currency) && CURRENCIES.has_key?(to_currency)
55
+ self.from_currency_valid? && self.to_currency_valid?
56
56
  end
57
-
58
-
59
- # Calculate the value from fixnum and exchange rate
57
+
58
+ # Calculate the value from fixnum, from and to currency
59
+ #
60
+ # fixnum - The Fixnum representing how much should be converted
61
+ # from - From currency
62
+ # to - To currency
63
+ #
64
+ # Examples
65
+ # CurrencySwitcher.exchange(3,"usd", "gbp")
66
+ # # => 5.23
67
+ #
68
+ # Returns float value of fixnum multiplied by exchange rate
69
+ # Raises StandardError if currencies are not valid
70
+ def self.exchange(fixnum, from, to)
71
+ @from_currency = from.to_sym
72
+ @to_currency = to.to_sym
73
+
74
+ raise StandardError, "From currency #{from_currency} is invalid" unless self.from_currency_valid?
75
+ raise StandardError, "To currency #{to_currency} is invalid" unless self.to_currency_valid?
76
+
77
+ self.calculate_value(fixnum)
78
+ end
79
+
80
+ # Calculate the value from fixnum
60
81
  #
61
82
  # fixnum - The Fixnum representing how much should be converted
62
83
  #
@@ -98,4 +119,20 @@ module CurrencySwitcher
98
119
  def self.currencies
99
120
  CURRENCIES.keys.sort.each { |symbol| puts "#{symbol} => #{CURRENCIES[symbol]}"}
100
121
  end
122
+
123
+ private
124
+
125
+ # Check if from currency if valid
126
+ #
127
+ # Returns true if from currency if valid
128
+ def self.from_currency_valid?
129
+ CURRENCIES.has_key?(from_currency)
130
+ end
131
+
132
+ # Check if to currency is valid
133
+ #
134
+ # Returns true if to currency is valid
135
+ def self.to_currency_valid?
136
+ CURRENCIES.has_key?(to_currency)
137
+ end
101
138
  end
@@ -43,12 +43,23 @@ class TestCurrencySwitcher < Test::Unit::TestCase
43
43
  end
44
44
 
45
45
  def test_usd_to_gbp
46
- assert_equal(format(2, 0.64539), 2.usd_to_gbp)
47
- end
48
-
49
- def test_eur_to_usd
50
- assert_equal(format(3, 1.3171), 3.eur_to_usd)
51
- end
46
+ assert_equal(format(2, 0.64539), 2.usd_to_gbp)
47
+ assert_equal(format(2, 0.64539), 2.exchange("usd","gbp"))
48
+ end
49
+
50
+
51
+ def test_eur_to_usd
52
+ assert_equal(format(3, 1.3171), 3.eur_to_usd)
53
+ assert_equal(format(3, 1.3171), 3.exchange("eur","usd"))
54
+ end
55
+
56
+ def test_invalid_currency_error
57
+ assert_raise(StandardError) { 2.exchange("usd","invalid_currency") }
58
+ end
59
+
60
+ def test_argument_error_for_exchange
61
+ assert_raise(ArgumentError) { 2.exchange("usd") }
62
+ end
52
63
 
53
64
  def test_exchange_rate_error
54
65
  Nokogiri.stubs(:HTML).returns(nil)
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: currency_switcher
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 1
9
- version: 0.0.1
4
+ prerelease:
5
+ version: 0.0.2
10
6
  platform: ruby
11
7
  authors:
12
8
  - Dominik Staskiewicz
@@ -35,7 +31,7 @@ cert_chain:
35
31
  wYwJN5o4GnGcdg==
36
32
  -----END CERTIFICATE-----
37
33
 
38
- date: 2011-01-07 00:00:00 +01:00
34
+ date: 2011-10-09 00:00:00 +02:00
39
35
  default_executable:
40
36
  dependencies: []
41
37
 
@@ -51,6 +47,7 @@ extra_rdoc_files:
51
47
  - lib/currency_switcher/currencies.rb
52
48
  - lib/currency_switcher/ext/fixnum.rb
53
49
  files:
50
+ - Manifest
54
51
  - README.md
55
52
  - Rakefile
56
53
  - lib/currency_switcher.rb
@@ -60,7 +57,6 @@ files:
60
57
  - test/html/eur_to_usd.html
61
58
  - test/html/usd_to_gbp.html
62
59
  - test/test_currency_switcher.rb
63
- - Manifest
64
60
  - currency_switcher.gemspec
65
61
  has_rdoc: true
66
62
  homepage: http://github.com/staskie/currency_switcher
@@ -81,22 +77,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
77
  requirements:
82
78
  - - ">="
83
79
  - !ruby/object:Gem::Version
84
- segments:
85
- - 0
86
80
  version: "0"
87
81
  required_rubygems_version: !ruby/object:Gem::Requirement
88
82
  none: false
89
83
  requirements:
90
84
  - - ">="
91
85
  - !ruby/object:Gem::Version
92
- segments:
93
- - 1
94
- - 2
95
86
  version: "1.2"
96
87
  requirements: []
97
88
 
98
89
  rubyforge_project: currency_switcher
99
- rubygems_version: 1.3.7
90
+ rubygems_version: 1.6.2
100
91
  signing_key:
101
92
  specification_version: 3
102
93
  summary: Convert currencies using exchange rates from http://exchange-rates.org
metadata.gz.sig CHANGED
Binary file