spot-rate 0.1.0 → 0.2.0

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/README.md CHANGED
@@ -18,13 +18,39 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ A SpotRate instance will by default use the GoogleCurrencyConverter if no other
22
+ currency converters are registered:
23
+
21
24
  ```ruby
22
25
  require 'spot-rate'
23
26
 
24
27
  puts Time.now
25
28
  puts SpotRate.new(:from_currency => 'USD', :to_currency => 'JPY').spot_rate
26
- #=> 2013-05-01 18:34:09 -0700
27
- #=> 98.2221786
29
+ # => 2013-05-01 18:34:09 -0700
30
+ # => 98.2221786
31
+ ```
32
+
33
+ If you'd like to register your own currency converter, use the
34
+ `.register_currency_converter` method:
35
+
36
+ ```ruby
37
+ class MyRandomCurrencyConverter
38
+ def initialize from_currency, to_currency
39
+ @from_currency = from_currency
40
+ @to_currency = to_currency
41
+ end
42
+
43
+ def spot_rate
44
+ rand # yolo
45
+ end
46
+ end
47
+
48
+ SpotRate.register_currency_converter(:random_converter, MyRandomCurrencyConverter)
49
+ spot_rate = SpotRate.new(:from_currency => 'USD', :to_currency => 'JPY')
50
+ puts spot_rate.use(:random_converter).spot_rate
51
+ # => 0.5363022464905228
52
+ puts spot_rate.spot_rate # will go back to using the pre-packaged default Google converter
53
+ # => 98.2221786
28
54
  ```
29
55
 
30
56
  ## Contributing
@@ -1,14 +1,14 @@
1
1
  require 'net/http'
2
2
 
3
3
  class SpotRate
4
- class GoogCurrencyConverter
5
- GOOG_CURRENCY_URI = 'http://www.google.com/ig/calculator?hl=en&q=1%s=?%s'
4
+ class GoogleCurrencyConverter
5
+ GOOGLE_CURRENCY_URI = 'http://www.google.com/ig/calculator?hl=en&q=1%s=?%s'
6
6
 
7
7
  attr_accessor :from_currency, :to_currency
8
8
 
9
9
  def initialize from_currency, to_currency
10
- self.from_currency = from_currency
11
- self.to_currency = to_currency
10
+ @from_currency = from_currency
11
+ @to_currency = to_currency
12
12
  end
13
13
 
14
14
  def spot_rate
@@ -19,7 +19,7 @@ class SpotRate
19
19
  private
20
20
 
21
21
  def ccy_api_uri
22
- URI(GOOG_CURRENCY_URI % [from_currency, to_currency])
22
+ URI(GOOGLE_CURRENCY_URI % [from_currency, to_currency])
23
23
  end
24
24
 
25
25
  def validate_converter_response!
@@ -1,3 +1,3 @@
1
1
  class SpotRate
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/spot_rate.rb CHANGED
@@ -3,19 +3,33 @@ class SpotRate
3
3
 
4
4
  attr_accessor :from_currency, :to_currency
5
5
 
6
+ def self.available_converters
7
+ @@available_converters ||= {}
8
+ end
9
+
10
+ def self.register_currency_converter converter_key, converter_class
11
+ self.available_converters[converter_key] = converter_class
12
+ end
13
+
6
14
  def initialize config = {}
7
- self.from_currency = config[:from_currency]
8
- self.to_currency = config[:to_currency]
15
+ @from_currency = config[:from_currency]
16
+ @to_currency = config[:to_currency]
9
17
  end
10
18
 
11
- def spot_rate
12
- default_converter.new(self.from_currency, self.to_currency).spot_rate
19
+ def use requested_converter_key
20
+ self.class
21
+ .available_converters[requested_converter_key]
22
+ .new(@from_currency, @to_currency)
13
23
  end
14
24
 
15
- private
25
+ def spot_rate
26
+ default_currency_converter.spot_rate
27
+ end
16
28
 
17
- def default_converter
18
- require './lib/spot_rate/goog_currency_converter'
19
- GoogCurrencyConverter
29
+ def default_currency_converter
30
+ use(:default)
20
31
  end
21
32
  end
33
+
34
+ require './lib/spot_rate/google_currency_converter'
35
+ SpotRate.register_currency_converter :default, SpotRate::GoogleCurrencyConverter
@@ -0,0 +1,23 @@
1
+ require 'net/http'
2
+ require './lib/spot_rate/google_currency_converter'
3
+
4
+ describe SpotRate::GoogleCurrencyConverter do
5
+ describe "#spot_rate" do
6
+ it "returns a number (no stubbing + requires internet connection)" do
7
+ converter = described_class.new 'USD', 'JPY'
8
+ expect(converter.spot_rate.class).to eq Float
9
+ end
10
+
11
+ it "returns the spot conversion rate from the from_currency to the to_currency" do
12
+ Net::HTTP.stub(:get).and_return('{lhs: "1 U.S. dollar",rhs: "98.222 Japanese yen",error: "",icc: true}')
13
+ converter = described_class.new 'USD', 'JPY'
14
+ expect(converter.spot_rate).to eq 98.222
15
+ end
16
+
17
+ it "handles errors when we get an error code back" do
18
+ Net::HTTP.stub(:get).and_return('{lhs: "does not matter",rhs: "does not matter",error: "4",icc: true}')
19
+ converter = described_class.new 'USD', 'BAD'
20
+ expect { converter.spot_rate }.to raise_error SpotRate::CurrencyNotFound
21
+ end
22
+ end
23
+ end
@@ -1,27 +1,45 @@
1
- require 'net/http'
2
1
  require './lib/spot_rate'
3
2
 
4
- puts Time.now
5
- puts SpotRate.new(from_currency: 'USD', to_currency: 'JPY').spot_rate
6
- #describe SpotRate do
7
- # describe "#spot_rate" do
8
- # it "returns a number (no stubbing + requires internet connection)" do
9
- # converter = described_class.new(from_currency: 'USD', to_currency: 'JPY')
10
- # expect(converter.spot_rate.class).to eq Float
11
- # end
12
- #
13
- # it "returns the spot conversion rate from the from_currency to the to_currency" do
14
- # Net::HTTP.stub(:get).and_return('{lhs: "1 U.S. dollar",rhs: "98.222 Japanese yen",error: "",icc: true}')
15
- # converter = described_class.new(from_currency: 'USD', to_currency: 'JPY')
16
- # expect(converter.spot_rate).to eq 98.222
17
- # end
18
- #
19
- # it "handles errors when we get an error code back" do
20
- # Net::HTTP.stub(:get).and_return('{lhs: "does not matter",rhs: "does not matter",error: "4",icc: true}')
21
- # converter = described_class.new
22
- # converter.from_currency = 'USD'
23
- # converter.to_currency = 'BAD'
24
- # expect { converter.spot_rate }.to raise_error(SpotRate::CurrencyNotFound)
25
- # end
26
- # end
27
- #end
3
+ class SomeCurrencyConverter
4
+ def initialize from, to
5
+ @from = from
6
+ @to = to
7
+ end
8
+
9
+ def spot_rate
10
+ "#{@from} #{@to}"
11
+ end
12
+ end
13
+
14
+ describe SpotRate do
15
+ describe ".register_currency_converter" do
16
+ it "adds the converter to the list of available currency converters" do
17
+ expect(SpotRate.available_converters).to_not include SomeCurrencyConverter
18
+ SpotRate.register_currency_converter :some_currency_converter, SomeCurrencyConverter
19
+ expect(SpotRate.available_converters[:some_currency_converter]).to eq SomeCurrencyConverter
20
+ end
21
+ end
22
+
23
+ describe "#default_currency_converter" do
24
+ it "is an instance of the Google Currency Converter" do
25
+ spot_rate = SpotRate.new
26
+ expect(spot_rate.default_currency_converter).to be_a SpotRate::GoogleCurrencyConverter
27
+ end
28
+ end
29
+
30
+ describe "#use" do
31
+ it "returns the spot rate as determined by the requested converter" do
32
+ SpotRate.register_currency_converter :some_currency_converter, SomeCurrencyConverter
33
+ spot_rate = SpotRate.new(from_currency: 'something', to_currency: 'another thing')
34
+ expect(spot_rate.use(:some_currency_converter).spot_rate).to eq "something another thing"
35
+ end
36
+ end
37
+
38
+ describe "#spot_rate" do
39
+ it "returns the spot rate as determined by the default converter" do
40
+ SpotRate.register_currency_converter :default, SomeCurrencyConverter
41
+ spot_rate = SpotRate.new(from_currency: 'something', to_currency: 'another thing')
42
+ expect(spot_rate.spot_rate).to eq "something another thing"
43
+ end
44
+ end
45
+ end
data/spot_rate.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
6
6
  gem.email = ["jeff.iacono@gmail.com"]
7
7
  gem.description = %q{get current currency spot rates with ruby}
8
8
  gem.summary = %q{current currency spot rates}
9
- gem.homepage = ""
9
+ gem.homepage = "https://github.com/jeffreyiacono/spot-rate"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spot-rate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -88,11 +88,12 @@ files:
88
88
  - README.md
89
89
  - Rakefile
90
90
  - lib/spot_rate.rb
91
- - lib/spot_rate/goog_currency_converter.rb
91
+ - lib/spot_rate/google_currency_converter.rb
92
92
  - lib/spot_rate/version.rb
93
+ - spec/spot_rate/google_currency_converter_spec.rb
93
94
  - spec/spot_rate_spec.rb
94
95
  - spot_rate.gemspec
95
- homepage: ''
96
+ homepage: https://github.com/jeffreyiacono/spot-rate
96
97
  licenses: []
97
98
  post_install_message:
98
99
  rdoc_options: []
@@ -112,9 +113,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
113
  version: '0'
113
114
  requirements: []
114
115
  rubyforge_project:
115
- rubygems_version: 1.8.21
116
+ rubygems_version: 1.8.24
116
117
  signing_key:
117
118
  specification_version: 3
118
119
  summary: current currency spot rates
119
120
  test_files:
121
+ - spec/spot_rate/google_currency_converter_spec.rb
120
122
  - spec/spot_rate_spec.rb