currency-rate 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +11 -0
  5. data/Gemfile.lock +91 -0
  6. data/LICENSE.txt +20 -0
  7. data/README.md +52 -0
  8. data/Rakefile +24 -0
  9. data/VERSION +1 -0
  10. data/currency-rate.gemspec +95 -0
  11. data/lib/adapter.rb +42 -0
  12. data/lib/btc_adapter.rb +17 -0
  13. data/lib/btc_adapters/average_rate_adapter.rb +50 -0
  14. data/lib/btc_adapters/bitpay_adapter.rb +18 -0
  15. data/lib/btc_adapters/bitstamp_adapter.rb +14 -0
  16. data/lib/btc_adapters/btce_adapter.rb +14 -0
  17. data/lib/btc_adapters/coinbase_adapter.rb +13 -0
  18. data/lib/btc_adapters/kraken_adapter.rb +14 -0
  19. data/lib/btc_adapters/localbitcoins_adapter.rb +13 -0
  20. data/lib/btc_adapters/okcoin_adapter.rb +14 -0
  21. data/lib/core_ext/classify.rb +7 -0
  22. data/lib/core_ext/deep_get.rb +11 -0
  23. data/lib/currency_rate.rb +49 -0
  24. data/lib/fiat_adapter.rb +30 -0
  25. data/lib/fiat_adapters/fixer_adapter.rb +11 -0
  26. data/lib/fiat_adapters/yahoo_adapter.rb +22 -0
  27. data/lib/storage.rb +17 -0
  28. data/spec/currency_rate_spec.rb +28 -0
  29. data/spec/lib/btc_adapter_spec.rb +55 -0
  30. data/spec/lib/btc_adapters/average_rate_adapter_spec.rb +51 -0
  31. data/spec/lib/btc_adapters/bitpay_adapter_spec.rb +35 -0
  32. data/spec/lib/btc_adapters/bitstamp_adapter_spec.rb +35 -0
  33. data/spec/lib/btc_adapters/btce_adapter_spec.rb +35 -0
  34. data/spec/lib/btc_adapters/coinbase_adapter_spec.rb +35 -0
  35. data/spec/lib/btc_adapters/kraken_adapter_spec.rb +35 -0
  36. data/spec/lib/btc_adapters/localbitcoins_adapter_spec.rb +35 -0
  37. data/spec/lib/btc_adapters/okcoin_adapter_spec.rb +35 -0
  38. data/spec/lib/fiat_adapters/fixer_adapter_spec.rb +22 -0
  39. data/spec/lib/fiat_adapters/yahoo_adapter_spec.rb +22 -0
  40. data/spec/lib/storage_spec.rb +25 -0
  41. data/spec/spec_helper.rb +13 -0
  42. metadata +169 -0
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CurrencyRate::BtceAdapter do
4
+
5
+ before :all do
6
+ VCR.insert_cassette 'exchange_rate_adapters/btc_adapters/btce_adapter'
7
+ end
8
+
9
+ after :all do
10
+ VCR.eject_cassette
11
+ end
12
+
13
+ before(:each) do
14
+ @exchange_adapter = CurrencyRate::BtceAdapter.instance
15
+ end
16
+
17
+ it "finds the rate for currency code" do
18
+ expect(@exchange_adapter.rate_for('USD')).to be_kind_of(Float)
19
+ expect( -> { @exchange_adapter.rate_for('FEDcoin') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
20
+ end
21
+
22
+ it "rases exception if rate is nil" do
23
+ json_response_1 = '{"ticker":{}}'
24
+ json_response_2 = '{"ticker":{"high":235,"low":215.89999,"avg":225.449995,"vol":2848293.72397,"vol_cur":12657.55799,"bambo":221.444,"buy":221.629,"sell":220.98,"updated":1422678812,"server_time":1422678813}}'
25
+ json_response_3 = '{"ticker":{"high":235,"low":215.89999,"avg":225.449995,"vol":2848293.72397,"vol_cur":12657.55799,"last":null,"buy":221.629,"sell":220.98,"updated":1422678812,"server_time":1422678813}}'
26
+ uri_mock = double('uri mock')
27
+ allow(uri_mock).to receive(:read).with(read_timeout: 4).and_return(json_response_1, json_response_2, json_response_3)
28
+ allow(URI).to receive(:parse).and_return(uri_mock)
29
+ 3.times do
30
+ @exchange_adapter.fetch_rates!
31
+ expect( -> { @exchange_adapter.rate_for('USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CurrencyRate::CoinbaseAdapter do
4
+
5
+ before :all do
6
+ VCR.insert_cassette 'exchange_rate_adapters/btc_adapters/coinbase_adapter'
7
+ end
8
+
9
+ after :all do
10
+ VCR.eject_cassette
11
+ end
12
+
13
+ before(:each) do
14
+ @exchange_adapter = CurrencyRate::CoinbaseAdapter.instance
15
+ end
16
+
17
+ it "finds the rate for currency code" do
18
+ expect(@exchange_adapter.rate_for('USD')).to be_kind_of(Float)
19
+ expect( -> { @exchange_adapter.rate_for('FEDcoin') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
20
+ end
21
+
22
+ it "raises exception if rate is nil" do
23
+ json_response_1 = '{}'
24
+ json_response_2 = '{"btc_to_urd":"224.41","usd_to_xpf":"105.461721","bsd_to_btc":"0.004456"}'
25
+ json_response_3 = '{"btc_to_usd":null,"usd_to_xpf":"105.461721","bsd_to_btc":"0.004456"}'
26
+ uri_mock = double('uri mock')
27
+ allow(uri_mock).to receive(:read).with(read_timeout: 4).and_return(json_response_1, json_response_2, json_response_3)
28
+ allow(URI).to receive(:parse).and_return(uri_mock)
29
+ 3.times do
30
+ @exchange_adapter.fetch_rates!
31
+ expect( -> { @exchange_adapter.rate_for('USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CurrencyRate::KrakenAdapter do
4
+
5
+ before :all do
6
+ VCR.insert_cassette 'exchange_rate_adapters/btc_adapters/kraken_adapter'
7
+ end
8
+
9
+ after :all do
10
+ VCR.eject_cassette
11
+ end
12
+
13
+ before(:each) do
14
+ @exchange_adapter = CurrencyRate::KrakenAdapter.instance
15
+ end
16
+
17
+ it "finds the rate for currency code" do
18
+ expect(@exchange_adapter.rate_for('USD')).to be_kind_of(Float)
19
+ expect( -> { @exchange_adapter.rate_for('FEDcoin') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
20
+ end
21
+
22
+ it "raises exception if rate is nil" do
23
+ json_response_1 = '{"error":[],"result":{}}'
24
+ json_response_2 = '{"error":[],"result":{"XXBTZUSD":{"a":["244.95495","1"],"b":["227.88761","1"],"abc":["228.20494","0.10000000"],"v":["5.18645337","24.04033530"],"p":["234.92296","234.41356"],"t":[45,74],"l":["228.20494","228.20494"],"h":["239.36069","239.36069"],"o":"231.82976"}}}'
25
+ json_response_3 = '{"error":[],"result":{"XXBTZUSD":{"a":["244.95495","1"],"b":["227.88761","1"],"c":[null,"0.10000000"],"v":["5.18645337","24.04033530"],"p":["234.92296","234.41356"],"t":[45,74],"l":["228.20494","228.20494"],"h":["239.36069","239.36069"],"o":"231.82976"}}}'
26
+ uri_mock = double('uri mock')
27
+ allow(uri_mock).to receive(:read).with(read_timeout: 4).and_return(json_response_1, json_response_2, json_response_3)
28
+ allow(URI).to receive(:parse).and_return(uri_mock)
29
+ 3.times do
30
+ @exchange_adapter.fetch_rates!
31
+ expect( -> { @exchange_adapter.rate_for('USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CurrencyRate::LocalbitcoinsAdapter do
4
+
5
+ before :all do
6
+ VCR.insert_cassette 'exchange_rate_adapters/btc_adapters/localbitcoins_adapter'
7
+ end
8
+
9
+ after :all do
10
+ VCR.eject_cassette
11
+ end
12
+
13
+ before(:each) do
14
+ @exchange_adapter = CurrencyRate::LocalbitcoinsAdapter.instance
15
+ end
16
+
17
+ it "finds the rate for currency code" do
18
+ expect(@exchange_adapter.rate_for('USD')).to be_kind_of(Float)
19
+ expect( -> { @exchange_adapter.rate_for('FEDcoin') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
20
+ end
21
+
22
+ it "rases exception if rate is nil" do
23
+ json_response_1 = '{"USD": {}}'
24
+ json_response_2 = '{"USD": {"volume_btc": "2277.85", "rates": {"bambo": "263.78"}, "avg_1h": 287.6003904801631, "avg_24h": 253.58144543993674, "avg_12h": 252.29202866050034}}'
25
+ json_response_3 = '{"USD": {"volume_btc": "2277.85", "rates": {"last": null}, "avg_1h": 287.6003904801631, "avg_24h": 253.58144543993674, "avg_12h": 252.29202866050034}}'
26
+ uri_mock = double('uri mock')
27
+ allow(uri_mock).to receive(:read).with(read_timeout: 4).and_return(json_response_1, json_response_2, json_response_3)
28
+ allow(URI).to receive(:parse).and_return(uri_mock)
29
+ 3.times do
30
+ @exchange_adapter.fetch_rates!
31
+ expect( -> { @exchange_adapter.rate_for('USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CurrencyRate::OkcoinAdapter do
4
+
5
+ before :all do
6
+ VCR.insert_cassette 'exchange_rate_adapters/btc_adapters/okcoin_adapter'
7
+ end
8
+
9
+ after :all do
10
+ VCR.eject_cassette
11
+ end
12
+
13
+ before(:each) do
14
+ @exchange_adapter = CurrencyRate::OkcoinAdapter.instance
15
+ end
16
+
17
+ it "finds the rate for currency code" do
18
+ expect(@exchange_adapter.rate_for('USD')).to be_kind_of(Float)
19
+ expect( -> { @exchange_adapter.rate_for('FEDcoin') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
20
+ end
21
+
22
+ it "raises exception if rate is nil" do
23
+ response = [
24
+ '{"date":"1422679981","ticker":{}}',
25
+ '{"date":"1422679981","ticker":{"buy":"227.27","high":"243.55","bambo":"226.89","low":"226.0","sell":"227.74","vol":"16065.2085"}}',
26
+ '{"date":"1422679981","ticker":{"buy":"227.27","high":"243.55","last":null,"low":"226.0","sell":"227.74","vol":"16065.2085"}}',
27
+ ]
28
+ 3.times do |i|
29
+ @exchange_adapter.instance_variable_set :@rates_updated_at, Time.now
30
+ @exchange_adapter.instance_variable_set :@rates, JSON.parse(response[i])
31
+ expect( -> { @exchange_adapter.rate_for('USD') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CurrencyRate::FixerAdapter do
4
+
5
+ before :all do
6
+ VCR.insert_cassette 'exchange_rate_adapters/fiat_adapters/fixer_adapter'
7
+ end
8
+
9
+ after :all do
10
+ VCR.eject_cassette
11
+ end
12
+
13
+ before(:each) do
14
+ @exchange_adapter = CurrencyRate::FixerAdapter.instance
15
+ end
16
+
17
+ it "finds the rate for currency code" do
18
+ expect(@exchange_adapter.rate_for('RUB')).to be_kind_of(Numeric)
19
+ expect( -> { @exchange_adapter.rate_for('KZT') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
20
+ end
21
+
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe CurrencyRate::YahooAdapter do
4
+
5
+ before :all do
6
+ VCR.insert_cassette 'exchange_rate_adapters/fiat_adapters/yahoo_adapter'
7
+ end
8
+
9
+ after :all do
10
+ VCR.eject_cassette
11
+ end
12
+
13
+ before(:each) do
14
+ @exchange_adapter = CurrencyRate::YahooAdapter.instance
15
+ end
16
+
17
+ it "finds the rate for currency code" do
18
+ expect(@exchange_adapter.rate_for('RUB')).to be_kind_of(Numeric)
19
+ expect( -> { @exchange_adapter.rate_for('KZT') }).to raise_error(CurrencyRate::Adapter::CurrencyNotSupported)
20
+ end
21
+
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe CurrencyRate::Storage do
4
+
5
+ before(:each) do
6
+ @source = double("Source") # source object from which the data is received
7
+ @storage = CurrencyRate::Storage.new
8
+ end
9
+
10
+ it "fetches from storage when timestamp isn't expired" do
11
+ expect(@source).to receive(:get).once.and_return('hi')
12
+ @storage.fetch('hello_world') { @source.get }
13
+ expect(@storage.fetch('hello_world')).to eq('hi')
14
+ end
15
+
16
+ it "runs a passed block to receive new result when timestamp is expired" do
17
+ expect(@source).to receive(:get).twice.and_return('hi', 'hello')
18
+ @storage.fetch('hello_world') { @source.get }
19
+ @storage.instance_variable_set(:@timeout, -1)
20
+ @storage.fetch('hello_world') { @source.get }
21
+ @storage.instance_variable_set(:@timeout, 1800)
22
+ expect(@storage.fetch('hello_world') { '' }).to eq('hello')
23
+ end
24
+
25
+ end
@@ -0,0 +1,13 @@
1
+ require_relative "../lib/currency_rate"
2
+ require 'webmock/rspec'
3
+ require 'vcr'
4
+
5
+ VCR.configure do |config|
6
+ config.cassette_library_dir = 'spec/fixtures/vcr'
7
+ config.hook_into :webmock
8
+ end
9
+
10
+ if ENV['VCR_OFF']
11
+ WebMock.allow_net_connect!
12
+ VCR.turn_off! ignore_cassettes: true
13
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: currency-rate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Roman Snitko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: satoshi-unit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jeweler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.1.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.1.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Fetches exchange rates from various sources and does the conversion
98
+ email: roman.snitko@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.md
104
+ files:
105
+ - ".document"
106
+ - ".rspec"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - VERSION
113
+ - currency-rate.gemspec
114
+ - lib/adapter.rb
115
+ - lib/btc_adapter.rb
116
+ - lib/btc_adapters/average_rate_adapter.rb
117
+ - lib/btc_adapters/bitpay_adapter.rb
118
+ - lib/btc_adapters/bitstamp_adapter.rb
119
+ - lib/btc_adapters/btce_adapter.rb
120
+ - lib/btc_adapters/coinbase_adapter.rb
121
+ - lib/btc_adapters/kraken_adapter.rb
122
+ - lib/btc_adapters/localbitcoins_adapter.rb
123
+ - lib/btc_adapters/okcoin_adapter.rb
124
+ - lib/core_ext/classify.rb
125
+ - lib/core_ext/deep_get.rb
126
+ - lib/currency_rate.rb
127
+ - lib/fiat_adapter.rb
128
+ - lib/fiat_adapters/fixer_adapter.rb
129
+ - lib/fiat_adapters/yahoo_adapter.rb
130
+ - lib/storage.rb
131
+ - spec/currency_rate_spec.rb
132
+ - spec/lib/btc_adapter_spec.rb
133
+ - spec/lib/btc_adapters/average_rate_adapter_spec.rb
134
+ - spec/lib/btc_adapters/bitpay_adapter_spec.rb
135
+ - spec/lib/btc_adapters/bitstamp_adapter_spec.rb
136
+ - spec/lib/btc_adapters/btce_adapter_spec.rb
137
+ - spec/lib/btc_adapters/coinbase_adapter_spec.rb
138
+ - spec/lib/btc_adapters/kraken_adapter_spec.rb
139
+ - spec/lib/btc_adapters/localbitcoins_adapter_spec.rb
140
+ - spec/lib/btc_adapters/okcoin_adapter_spec.rb
141
+ - spec/lib/fiat_adapters/fixer_adapter_spec.rb
142
+ - spec/lib/fiat_adapters/yahoo_adapter_spec.rb
143
+ - spec/lib/storage_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: http://github.com/snitko/currency-rate
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.5.1
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Converter for fiat and crypto currencies
169
+ test_files: []