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 +4 -4
- data/VERSION +1 -1
- data/currency-rate.gemspec +2 -2
- data/lib/adapter.rb +7 -4
- data/lib/storage.rb +4 -3
- data/spec/currency_rate_spec.rb +42 -26
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a7717da50f7013b0f430545b761e05f6fa88ea8
|
4
|
+
data.tar.gz: f0e499bed1863467e640448da5f23eb9219bc53d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cedcc1c71864a83e88ce7ae444b139d469f628897ab411ae4c64b517e57c9af81a35531313beb8b712994d3a8f01a4ff28d9ef26c319264a6f53ac4093bd384
|
7
|
+
data.tar.gz: f8be39baeccb40a9a6ebe0e1de6bfb0403499b1978c201e9bbd87bc98590f5ae1719d09ddd16a3bfea31201920ad73b50f34e032d0a584f51e0d0f99f0dc61cf
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.7
|
data/currency-rate.gemspec
CHANGED
@@ -6,12 +6,12 @@
|
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "currency-rate"
|
9
|
-
s.version = "0.3.
|
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-
|
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 =
|
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
|
-
|
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
data/spec/currency_rate_spec.rb
CHANGED
@@ -2,35 +2,51 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe CurrencyRate do
|
4
4
|
|
5
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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 "
|
32
|
-
|
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.
|
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-
|
11
|
+
date: 2017-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: satoshi-unit
|