sibit 0.35.0 → 0.35.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 513237b6306d27583cb85cb5e95665d650557e1913446c6ac1bfea6a3b2ba2ef
4
- data.tar.gz: 36206100588673eb924460d7e5b7774ab2a4f5004eb98effd71eaca77e0a08ff
3
+ metadata.gz: d545c42be046368aee30cdc1baab63944b45c97b8b68b4f9fabe6b44ca036d15
4
+ data.tar.gz: b0dcc6193397326031efdf8089f15682946083f6dac8e221ed8a3f3f18f37abf
5
5
  SHA512:
6
- metadata.gz: 125db74f9d3c3cbbe34dd8bf0456a27717e164141e5e6aa811d11b5ceeaf9cf2744c7f117ab03f0163979eefb3c1d17bc11e0182f90bdafe814b93d8c5b707d4
7
- data.tar.gz: 2f8337f9f35a9afb34ae65d90b9aef46ae631bfaa0c3f9e1622e638b08fc1289018048c93f2037663733d9e73fdd3914ace5384241cbe894db3e894a435976c4
6
+ metadata.gz: 78728c5d74334bd3c02f07bf6c33139d13f98d01f48ecb81790f5901d27106d10e6e069c5e7a779f3b01df73e22b9c2b7e435f0d5039734ec811b435c1a111c0
7
+ data.tar.gz: 32c68a1e19d52ec99aae38c165d8b501d210315fcd843a789c351c2efbba2516ff69c80efcba022a5ab59edad4ef23a69f668351ba640f32a568b505b9facab2
data/lib/sibit/bestof.rb CHANGED
@@ -20,11 +20,12 @@ class Sibit::BestOf
20
20
  @verbose = verbose
21
21
  end
22
22
 
23
- # Current price of BTC in USD (float returned).
23
+ # Current price of BTC in USD (float returned). Exchanges never quote
24
+ # the exact same number, so this one does not vote: it averages all
25
+ # answers and fails when they are more than five percent apart.
24
26
  def price(currency = 'USD')
25
- best_of('price') do |api|
26
- api.price(currency)
27
- end
27
+ return @list.price(currency) unless @list.is_a?(Array)
28
+ average('price', answers('price') { |api| api.price(currency) })
28
29
  end
29
30
 
30
31
  # Gets the balance of the address, in satoshi.
@@ -97,8 +98,12 @@ class Sibit::BestOf
97
98
 
98
99
  private
99
100
 
100
- def best_of(method)
101
+ def best_of(method, &blk)
101
102
  return yield(@list) unless @list.is_a?(Array)
103
+ majority(method, answers(method, &blk))
104
+ end
105
+
106
+ def answers(method)
102
107
  votes = []
103
108
  errors = []
104
109
  @list.each do |api|
@@ -117,7 +122,19 @@ class Sibit::BestOf
117
122
  "#{@list.map { |a| a.class.name }.join(', ')}"
118
123
  )
119
124
  end
120
- majority(method, votes)
125
+ votes
126
+ end
127
+
128
+ def average(method, votes)
129
+ prices = votes.map { |v| v[1] }
130
+ if (prices.max - prices.min) / prices.min > 0.05
131
+ raise(
132
+ Sibit::Error,
133
+ "The answers at #{method}() are more than 5% apart: " \
134
+ "#{votes.map { |name, answer| "#{name}=#{answer}" }.join(', ')}"
135
+ )
136
+ end
137
+ prices.sum / prices.size
121
138
  end
122
139
 
123
140
  def majority(method, votes)
data/lib/sibit/version.rb CHANGED
@@ -8,5 +8,5 @@
8
8
  # Copyright:: Copyright (c) 2019-2026 Yegor Bugayenko
9
9
  # License:: MIT
10
10
  class Sibit
11
- VERSION = '0.35.0' unless defined?(VERSION)
11
+ VERSION = '0.35.1' unless defined?(VERSION)
12
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sibit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.35.0
4
+ version: 0.35.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko