currency-rate 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5cb6b70c3806073d72b5199d009f115eba98e776
4
- data.tar.gz: bcee4a20aa44098a8591477dc5c3dc73759a2b8a
3
+ metadata.gz: 6a7717da50f7013b0f430545b761e05f6fa88ea8
4
+ data.tar.gz: f0e499bed1863467e640448da5f23eb9219bc53d
5
5
  SHA512:
6
- metadata.gz: bc27091d644a2125ae608b4e468bd3b8452862bc745ddc6cc38b1a2abaad5da458a29fd4d2f63a4e94da757d7a469158a0c804efef5f1bbd7ebd5f292b89c531
7
- data.tar.gz: 78e9164106299808e4d5488b6d7d5d0127e23ddd4bddd772c19e2b10b1303582b6af31f3375648bd6114f531690bdb2966cf6437006112568f700d845afa539e
6
+ metadata.gz: 8cedcc1c71864a83e88ce7ae444b139d469f628897ab411ae4c64b517e57c9af81a35531313beb8b712994d3a8f01a4ff28d9ef26c319264a6f53ac4093bd384
7
+ data.tar.gz: f8be39baeccb40a9a6ebe0e1de6bfb0403499b1978c201e9bbd87bc98590f5ae1719d09ddd16a3bfea31201920ad73b50f34e032d0a584f51e0d0f99f0dc61cf
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.3.7
@@ -6,12 +6,12 @@
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "currency-rate"
9
- s.version = "0.3.6"
9
+ s.version = "0.3.7"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Roman Snitko"]
14
- s.date = "2017-02-06"
14
+ s.date = "2017-03-29"
15
15
  s.description = "Fetches exchange rates from various sources and does the conversion"
16
16
  s.email = "roman.snitko@gmail.com"
17
17
  s.extra_rdoc_files = [
data/lib/adapter.rb CHANGED
@@ -6,9 +6,10 @@ module CurrencyRate
6
6
  class CurrencyNotSupported < Exception; end
7
7
 
8
8
  attr_accessor :try_storage_on_fetching_failed
9
+ attr_reader :storage
9
10
 
10
11
  def initialize
11
- @storage = Storage.new
12
+ @storage = Storage.new(adapter_name: self.class.to_s)
12
13
  end
13
14
 
14
15
  def fetch_rates!
@@ -34,16 +35,18 @@ module CurrencyRate
34
35
  def rate_for(from,to)
35
36
 
36
37
  begin
37
- @storage.fetch(self.class.to_s) { self.fetch_rates! }
38
+ @rates = self.storage.fetch(self.class.to_s) { self.fetch_rates! }
38
39
  rescue FetchingFailed => e
39
40
  if @try_storage_on_fetching_failed
40
- @rates = @storage.data[self.class.to_s][:content]
41
+ @rates = self.storage.data[self.class.to_s][:content]
41
42
  else
42
43
  raise e
43
44
  end
44
45
  end
45
46
 
46
- raise CurrencyNotSupported unless supports_currency_pair?(from,to)
47
+ unless supports_currency_pair?(from,to)
48
+ raise CurrencyNotSupported, "Unsupported currencies (one or both) are: #{from} -> #{to}"
49
+ end
47
50
 
48
51
  # This method is further reloaded in Adapter classes, that's why
49
52
  # here it doesn't really return anything useful!
data/lib/storage.rb CHANGED
@@ -3,9 +3,10 @@ module CurrencyRate
3
3
 
4
4
  attr_reader :data
5
5
 
6
- def initialize(timeout: 1800)
7
- @timeout = timeout
8
- @data = {}
6
+ def initialize(adapter_name: nil, timeout: 1800)
7
+ @timeout = timeout
8
+ @data = {}
9
+ @adapter_name = adapter_name
9
10
  end
10
11
 
11
12
  def fetch(key)
@@ -2,35 +2,51 @@ require 'spec_helper'
2
2
 
3
3
  describe CurrencyRate do
4
4
 
5
- before :each do
6
- allow(CurrencyRate::BitstampAdapter.instance).to receive('rate_for').with('BTC', 'USD').and_return(550)
7
- allow(CurrencyRate::BitstampAdapter.instance).to receive('rate_for').with('USD', 'BTC').and_return(1.to_f/550.to_f)
8
- allow(CurrencyRate::BitstampAdapter.instance).to receive('rate_for').with('BTC', 'RUB').and_return(33100)
9
- allow(CurrencyRate::YahooAdapter.instance).to receive('rate_for').with('USD', 'RUB').and_return(60)
10
- allow(CurrencyRate::YahooAdapter.instance).to receive('rate_for').with('RUB', 'USD').and_return(1.to_f/60.to_f)
11
- end
5
+ describe "stubbed requests" do
12
6
 
13
- it "fetches currency rate from a specified exchange" do
14
- expect(CurrencyRate.get('Bitstamp', 'BTC', 'USD')).to eq(550)
15
- expect(CurrencyRate.get('Bitstamp', 'USD', 'BTC')).to eq(1.to_f/550.to_f)
16
- expect(CurrencyRate.get('Bitstamp', 'USD', 'RUB').round(2)).to eq(60.18)
17
- end
18
-
19
- it "converts one currency into another" do
20
- expect(CurrencyRate.convert('Bitstamp', amount: 5, from: 'BTC', to: 'USD')).to eq(2750)
21
- expect(CurrencyRate.convert('Bitstamp', amount: 2750, from: 'USD', to: 'BTC')).to eq(5)
22
- expect(CurrencyRate.convert('Yahoo', amount: 300, from: 'USD', to: 'RUB')).to eq(18000)
23
- expect(CurrencyRate.convert('Yahoo', amount: 19500, from: 'RUB', to: 'USD')).to eq(325)
24
- end
7
+ before :each do
8
+ allow(CurrencyRate::BitstampAdapter.instance).to receive('rate_for').with('BTC', 'USD').and_return(550)
9
+ allow(CurrencyRate::BitstampAdapter.instance).to receive('rate_for').with('USD', 'BTC').and_return(1.to_f/550.to_f)
10
+ allow(CurrencyRate::BitstampAdapter.instance).to receive('rate_for').with('BTC', 'RUB').and_return(33100)
11
+ allow(CurrencyRate::YahooAdapter.instance).to receive('rate_for').with('USD', 'RUB').and_return(60)
12
+ allow(CurrencyRate::YahooAdapter.instance).to receive('rate_for').with('RUB', 'USD').and_return(1.to_f/60.to_f)
13
+ end
14
+
15
+ it "fetches currency rate from a specified exchange" do
16
+ expect(CurrencyRate.get('Bitstamp', 'BTC', 'USD')).to eq(550)
17
+ expect(CurrencyRate.get('Bitstamp', 'USD', 'BTC')).to eq(1.to_f/550.to_f)
18
+ expect(CurrencyRate.get('Bitstamp', 'USD', 'RUB').round(2)).to eq(60.18)
19
+ end
20
+
21
+ it "converts one currency into another" do
22
+ expect(CurrencyRate.convert('Bitstamp', amount: 5, from: 'BTC', to: 'USD')).to eq(2750)
23
+ expect(CurrencyRate.convert('Bitstamp', amount: 2750, from: 'USD', to: 'BTC')).to eq(5)
24
+ expect(CurrencyRate.convert('Yahoo', amount: 300, from: 'USD', to: 'RUB')).to eq(18000)
25
+ expect(CurrencyRate.convert('Yahoo', amount: 19500, from: 'RUB', to: 'USD')).to eq(325)
26
+ end
27
+
28
+ it "converts non-anchor currencies" do
29
+ expect(CurrencyRate.convert('Bitstamp', amount: 1000, from: 'USD', to: 'RUB')).to eq(60181.82)
30
+ expect(CurrencyRate.convert('Bitstamp', amount: 60181.81, from: 'RUB', to: 'USD')).to eq(1000)
31
+ end
32
+
33
+ it "gets default currencies for an adapter" do
34
+ expect(CurrencyRate.default_currencies_for("Bitstamp")).to eq(["USD", "BTC"])
35
+ end
25
36
 
26
- it "converts non-anchor currencies" do
27
- expect(CurrencyRate.convert('Bitstamp', amount: 1000, from: 'USD', to: 'RUB')).to eq(60181.82)
28
- expect(CurrencyRate.convert('Bitstamp', amount: 60181.81, from: 'RUB', to: 'USD')).to eq(1000)
29
37
  end
30
-
31
- it "gets default currencies for an adapter" do
32
- expect(CurrencyRate.default_currencies_for("Bitstamp")).to eq(["USD", "BTC"])
38
+
39
+ it "uses storage to fetch data when adapter raises FetchingFailed" do
40
+
41
+ rates = { "btc_usd" => { "high" => "1177.99", "last" => "1166.89" }}
42
+ original_storage = CurrencyRate::BitstampAdapter.instance.storage
43
+ storage_mock = double("storage")
44
+ allow(storage_mock).to receive(:data).and_return({"CurrencyRate::BitstampAdapter" => { content: rates }})
45
+ allow(storage_mock).to receive(:fetch).and_raise(CurrencyRate::Adapter::FetchingFailed)
46
+ CurrencyRate::BitstampAdapter.instance.instance_variable_set(:@storage, storage_mock)
47
+ expect(CurrencyRate.get('Bitstamp', 'BTC', 'USD', try_storage_on_fetching_failed: true)).to eq(1166.89)
48
+
49
+ Singleton.__init__(CurrencyRate::BitstampAdapter) # to avoid mock leaking
33
50
  end
34
51
 
35
-
36
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: currency-rate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Snitko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-26 00:00:00.000000000 Z
11
+ date: 2017-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: satoshi-unit