currency_converter 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --drb
data/README.md CHANGED
@@ -18,12 +18,27 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ To convert between two currencies:
22
+
23
+ CurrencyConverter.exchange("EUR", "USD", 100)
24
+ => 132.89
25
+
26
+ CurrencyConverter.exchange("USD", "EUR", 100)
27
+ => 75.15
28
+
29
+ To get the most recent exchange rate between two currencies:
30
+
31
+ CurrencyConverter.exchange("EUR", "USD", 1)
32
+ => 1.33
33
+
34
+ CurrencyConverter.exchange("USD", "EUR", 1)
35
+ => 0.75
22
36
 
23
37
  ## Contributing
24
38
 
25
39
  1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
40
+ 2. Add tests for it.
41
+ 3. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 5. Push to the branch (`git push origin my-new-feature`)
44
+ 6. Create new Pull Request
data/Rakefile CHANGED
@@ -1 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ RSpec::Core::RakeTask.new
4
+ task :test => :spec
@@ -27,4 +27,4 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "rspec", "~> 2.14"
28
28
  spec.add_development_dependency "yard", "~> 0.8"
29
29
  spec.add_development_dependency "kramdown", "~> 1.1"
30
- end
30
+ end
@@ -1,3 +1,4 @@
1
+ require "currency_converter/exceptions"
1
2
  require "currency_converter/currencies"
2
3
  require "currency_converter/version"
3
4
  require "net/http"
@@ -22,9 +23,9 @@ module CurrencyConverter
22
23
  def exchange(from, to, fixnum)
23
24
  @from_currency = from.upcase.to_sym
24
25
  @to_currency = to.upcase.to_sym
25
- raise StandardError, "Currency #{from_currency} or #{to_currency} is invalid" unless valid_currency?
26
+ validate_currency
26
27
  ex_rate = exchange_rate
27
- raise StandardError, "Can't find any exchange rate" if ex_rate.nil?
28
+ validate_rate(ex_rate)
28
29
  value = "%.2f" % (ex_rate.to_f * fixnum).to_s
29
30
  value.to_f
30
31
  end
@@ -44,11 +45,15 @@ module CurrencyConverter
44
45
  raise StandardError, "Please check your internet connection"
45
46
  end
46
47
 
47
- # Check if currency is valid
48
- #
49
- # Returns true if currency is valid
50
- def valid_currency?
51
- CurrencyConverter::CURRENCIES.has_key?(from_currency) && CurrencyConverter::CURRENCIES.has_key?(to_currency)
48
+ # Raise errors for invalid currencies.
49
+ def validate_currency
50
+ raise UnknownCurrency.new(from_currency) unless CurrencyConverter::CURRENCIES.has_key?(from_currency)
51
+ raise UnknownCurrency.new(to_currency) unless CurrencyConverter::CURRENCIES.has_key?(to_currency)
52
+ end
53
+
54
+ # Raise errors for missing data.
55
+ def validate_rate(rate)
56
+ raise MissingExchangeRate.new(from_currency, to_currency) if rate.to_f.zero?
52
57
  end
53
58
  end
54
- end
59
+ end
@@ -0,0 +1,19 @@
1
+ module CurrencyConverter
2
+ # = Missing Exchange Rate
3
+ #
4
+ # Raised when the data for a supported currencies is +nil+ or +zero?+.
5
+ class MissingExchangeRate < StandardError
6
+ def initialize(from, to)
7
+ super("Foreign exchange rate from #{from} to #{to} is missing.")
8
+ end
9
+ end
10
+
11
+ # = Unknown Currency
12
+ #
13
+ # Raised when we try to grab data for an unsupported currency code.
14
+ class UnknownCurrency < StandardError
15
+ def initialize(currency_code)
16
+ super("#{currency_code} is not supported.")
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module CurrencyConverter
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,5 +1,22 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe CurrencyConverter do
4
-
5
- end
4
+ describe ".exchange" do
5
+ it "should return the correct rate" do
6
+ CurrencyConverter.exchange("USD", "USD", 1).should == 1.0
7
+ end
8
+ end
9
+
10
+ describe "#validate_currency" do
11
+ describe CurrencyConverter::UnknownCurrency do
12
+ it 'should raise an error on an unsupported currency code' do
13
+ expect { CurrencyConverter.exchange("EUR", "UNKNOWN", 100) }.to raise_error(CurrencyConverter::UnknownCurrency)
14
+ expect { CurrencyConverter.exchange("UNKNOWN", "EUR", 100) }.to raise_error(CurrencyConverter::UnknownCurrency)
15
+ end
16
+
17
+ it 'should not raise an error for supported currency codes' do
18
+ expect { CurrencyConverter.exchange("EUR", "USD", 100) }.not_to raise_error(CurrencyConverter::UnknownCurrency)
19
+ end
20
+ end
21
+ end
22
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,5 @@
1
- $LOAD_PATH.unshift File.dirname(__FILE__)
2
- require 'rspec'
3
1
  require 'currency_converter'
4
2
 
5
3
  RSpec.configure do |config|
6
4
  config.order = "rand"
7
- end
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: currency_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-11 00:00:00.000000000 Z
12
+ date: 2013-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &83669940 !ruby/object:Gem::Requirement
16
+ requirement: &83208720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *83669940
24
+ version_requirements: *83208720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &83669730 !ruby/object:Gem::Requirement
27
+ requirement: &83208520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *83669730
35
+ version_requirements: *83208520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &83669450 !ruby/object:Gem::Requirement
38
+ requirement: &83208240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '2.14'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *83669450
46
+ version_requirements: *83208240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yard
49
- requirement: &83669200 !ruby/object:Gem::Requirement
49
+ requirement: &83207960 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0.8'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *83669200
57
+ version_requirements: *83207960
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: kramdown
60
- requirement: &83668970 !ruby/object:Gem::Requirement
60
+ requirement: &83207520 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '1.1'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *83668970
68
+ version_requirements: *83207520
69
69
  description: Google provides a web site to converts currencies using exchange rates
70
70
  but they have not provided any API for ruby. This is a small library that converts
71
71
  currencies exchange rate. You can convert currencies directly through this library.
@@ -76,7 +76,7 @@ extensions: []
76
76
  extra_rdoc_files: []
77
77
  files:
78
78
  - .gitignore
79
- - CHANGELOG.md
79
+ - .rspec
80
80
  - Gemfile
81
81
  - LICENSE.txt
82
82
  - README.md
@@ -84,6 +84,7 @@ files:
84
84
  - currency_converter.gemspec
85
85
  - lib/currency_converter.rb
86
86
  - lib/currency_converter/currencies.rb
87
+ - lib/currency_converter/exceptions.rb
87
88
  - lib/currency_converter/version.rb
88
89
  - spec/currency_converter_spec.rb
89
90
  - spec/spec_helper.rb
@@ -117,3 +118,4 @@ summary: Google provides a web site to converts currencies using exchange rates
117
118
  test_files:
118
119
  - spec/currency_converter_spec.rb
119
120
  - spec/spec_helper.rb
121
+ has_rdoc:
data/CHANGELOG.md DELETED
@@ -1 +0,0 @@
1
- == 0.0.1