waffle_api 0.1.2 → 0.2.0

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: b37de94f955112e583f831d8a16ef579c1afc5ec
4
- data.tar.gz: e1ae1d9128a4835d55610e2b475c4b64a79911b8
3
+ metadata.gz: 1fad819993b7da91f6552c4a5248d5135c80152a
4
+ data.tar.gz: 0e25efe461072be56c26cd0b736ddb8815aa4303
5
5
  SHA512:
6
- metadata.gz: 15f87d7fe974b901553c758274cdf48dcd5affd67c4214349f3b74d374624a520bed35c09e6d67e4459eb440f2e206215d7d91941bed4c90764ce3fa78cd9559
7
- data.tar.gz: f15a48e4b2a09c51841cf1099b4c3152e5579cef02de7b8cd50e7bf4d77dfce7de27f70d73f73097fda07c3fff91269f9e52b08e2576ee4154a73b324cc0991d
6
+ metadata.gz: 82607fe7ec0d0279ed86aabbd135e2d272868643af918f765038eece6585cab8a2cb04157a9236bf16ad1d485f4c400a52a5c232133d7f6cc89a58e63e3266f2
7
+ data.tar.gz: 708e73919a1190d8b6b234875fc3a8b19c3c523a78129ad20cdfb7b3c034ceb9028fb9e6f071285e78535a74aaa50dc156f0c44793ae08f18dee9eeb216496bd
data/.travis.yml CHANGED
@@ -1,4 +1,10 @@
1
1
  language: ruby
2
+
3
+ addons:
4
+ code_climate:
5
+ repo_token:
6
+ secure: I1x0uh8d7/sfhmTJA3+V4w/2X55nT4+jLTqiTiIVJHSRhbMts+SJr8DTDUHYlOENIX5ZNzZjDY2LkD3tpC98ysxwyAq3gx3lA6HDE5aTbBuzOuQgkm8nxkMBy7NaBYCwmEldaZxsefL8qmV2omAxsmdoGnFAV5Ozhue8Bv8EjkU=
7
+
2
8
  cache: bundler
3
9
  rvm:
4
10
  - 2.0.0
data/README.md CHANGED
@@ -23,81 +23,86 @@ client = WaffleAPI::Client.new address: '1Ju8U9Ukfc5kiMqzQrRgQBP1JvRkeSv94V'
23
23
  ```
24
24
 
25
25
 
26
- ### Get current total hashrate
26
+ ### WaffleAPI::Client
27
27
  ```ruby
28
+ # Hash rate in H/s
28
29
  client.hashrate
29
- ```
30
+ #=> 1_234_567
30
31
 
32
+ # List of all workers
33
+ workers = client.workers
34
+ #=> [ #<WaffleAPI::Worker>, #<WaffleAPI::Worker>, ... ]
31
35
 
32
- ### Get workers infos
36
+ # Current balances
37
+ balances = client.balances
38
+ #=> #<WaffleAPI::Balances>
33
39
 
34
- ```ruby
35
- client.worker_hashrates
40
+ # 10 last recent payments
41
+ payments = client.payments
42
+ #=> [ #<WaffleAPI::Payment>, #<WaffleAPI::Payment>, ... ]
36
43
  ```
37
44
 
38
- Will return a JSON array
39
-
40
- ```json
41
- [
42
- {
43
- "username": "1Ju8U9Ukfc5kiMqzQrRgQBP1JvRkeSv94V_calyhrerig1",
44
- "hashrate": 2529705,
45
- "stalerate": 3.33,
46
- "last_seen": 1398022030,
47
- "str": "2.53 MH/s"
48
- },
49
- {
50
- "username": "1Ju8U9Ukfc5kiMqzQrRgQBP1JvRkeSv94V_dqmsrig1",
51
- "hashrate": 1773702,
52
- "stalerate": 1.61,
53
- "last_seen": 1398022030,
54
- "str": "1.77 MH/s"
55
- }
56
- ]
57
- ```
45
+ ### WaffleAPI::Worker
46
+ ```ruby
47
+ worker = workers.first
48
+ #=> #<WaffleAPI::Worker>
58
49
 
50
+ # Worker name, without an underscore in it
51
+ worker.name
52
+ #=> 1Ju8U9Ukfc5kiMqzQrRgQBP1JvRkeSv94V
59
53
 
60
- ### Get balances
54
+ # Worker name, with an underscore in it e.g: 1Ju8U...94V_worker0
55
+ worker.name
56
+ #=> worker0
61
57
 
62
- ```ruby
63
- client.balances
64
- ```
58
+ # Hash rate in H/s
59
+ worker.hash_rate
60
+ #=> 723_456
65
61
 
66
- Will return a JSON array
62
+ # Stale rate in %
63
+ worker.stale_rate
64
+ #=> 3.6
67
65
 
68
- ```json
69
- {
70
- "sent": 1.61039009,
71
- "confirmed": 0.00383488,
72
- "unconverted": 0.011964213459441
73
- }
66
+ # Last seen
67
+ worker.last_seen
68
+ #=> 2014-03-13 20:22:42 +0100
74
69
  ```
75
70
 
71
+ ### WaffleAPI::Balances
72
+ ```ruby
73
+ # BTC already paid
74
+ balances.sent
75
+ #=> 1.623456
76
76
 
77
- ### Get payments
77
+ # BTC confirmed but not yet paid
78
+ balances.confirmed
79
+ #=> 0.012345
78
80
 
79
- ```ruby
80
- client.recent_payments
81
+ # BTC not yet converted (approximate)
82
+ balances.unconverted
83
+ #=> 0.012345
84
+
85
+ # Expected BTC (confirmed + unconverted, approximate)
86
+ balances.expected
87
+ #=> 0.02469
81
88
  ```
82
89
 
83
- Will return a JSON array containing the last 10 payments
84
-
85
- ```json
86
- [
87
- {
88
- "amount": "0.01715311",
89
- "time": "2014-04-20 16:03:24",
90
- "txn": "8706e0a..."
91
- },
92
- {
93
- "amount": "0.01600557",
94
- "time": "2014-04-20 04:36:01",
95
- "txn": "258dda8..."
96
- },
97
- {
98
- "..."
99
- }
100
- ]
90
+ ### WaffleAPI::Payment
91
+ ```ruby
92
+ payment = payments.first
93
+ #=> #<WaffleAPI::Payment>
94
+
95
+ # Payment amount
96
+ payment.amount
97
+ #=> 0.02469
98
+
99
+ # Payment paid date
100
+ payment.paid_at
101
+ #=> 2014-03-13 20:22:42 +0100
102
+
103
+ # Payment transaction hash
104
+ payment.transaction_hash
105
+ #=> "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
101
106
  ```
102
107
 
103
108
 
@@ -0,0 +1,13 @@
1
+ module WaffleAPI
2
+ # Balances object give access to all balances from Wafflepool
3
+ class Balances
4
+ attr_reader :sent, :confirmed, :unconverted, :expected
5
+
6
+ def initialize(sent: 0.0, confirmed: 0.0, unconverted: 0.0)
7
+ @sent = sent
8
+ @confirmed = confirmed
9
+ @unconverted = unconverted
10
+ @expected = @confirmed + @unconverted
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,8 @@
1
1
  require 'waffle_api/helpers'
2
2
  require 'waffle_api/address_validator'
3
+ require 'waffle_api/worker'
4
+ require 'waffle_api/balances'
5
+ require 'waffle_api/payment'
3
6
 
4
7
  module WaffleAPI
5
8
  # A Ruby class to call the Waffle stat API. You might use this if you want to
@@ -14,20 +17,51 @@ module WaffleAPI
14
17
  include WaffleAPI::Helpers
15
18
  include WaffleAPI::AddressValidator
16
19
 
17
- attr_accessor :address, :https_only
20
+ attr_accessor :address
18
21
 
19
- def initialize(address: nil, ignore_bad_address: false)
22
+ def initialize(address: nil)
20
23
  @address = address || ENV['BTC_ADDRESS']
21
- @https_only = https_only
22
24
 
23
25
  fail Error::EmptyAddress if @address.nil? || @address.empty?
24
- fail Error::BadAddress, @address unless valid_address? || ignore_bad_address
26
+ fail Error::BadAddress, @address unless valid_address?
25
27
  end
26
28
 
27
29
  def hashrate
28
30
  stats 'hash_rate'
29
31
  end
30
32
 
33
+ def workers
34
+ stats('worker_hashrates').map do |worker|
35
+ WaffleAPI::Worker.new(
36
+ name: worker['username'],
37
+ hash_rate: worker['hashrate'],
38
+ stale_rate: worker['stalerate'],
39
+ last_seen: worker['last_seen']
40
+ )
41
+ end
42
+ end
43
+
44
+ def balances
45
+ balances = stats 'balances'
46
+
47
+ WaffleAPI::Balances.new(
48
+ sent: balances['sent'],
49
+ confirmed: balances['confirmed'],
50
+ unconverted: balances['unconverted']
51
+ )
52
+ end
53
+
54
+ def payments
55
+ stats('recent_payments').map do |payment|
56
+ WaffleAPI::Payment.new(
57
+ amount: payment['amount'],
58
+ paid_at: payment['time'],
59
+ transaction_hash: payment['txn']
60
+ )
61
+ end
62
+ end
63
+
64
+ # This is for not yet suported keys
31
65
  def method_missing(method, *args, &block)
32
66
  stats method.to_s
33
67
  rescue Error::UnknownKey
@@ -0,0 +1,13 @@
1
+ module WaffleAPI
2
+ # Payment object give access to recent payments from Wafflepool. It give
3
+ # access to amount, paid_at and transaction_hash
4
+ class Payment
5
+ attr_reader :amount, :paid_at, :transaction_hash
6
+
7
+ def initialize(amount: 0.0, paid_at: Time.now.to_s, transaction_hash: '')
8
+ @amount = amount
9
+ @paid_at = DateTime.parse(paid_at).to_time
10
+ @transaction_hash = transaction_hash
11
+ end
12
+ end
13
+ end
@@ -1,4 +1,4 @@
1
1
  # WaffleAPI Version
2
2
  module WaffleAPI
3
- VERSION = '0.1.2'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -0,0 +1,14 @@
1
+ module WaffleAPI
2
+ # Worker object give access to it's hashrate, stalerate, etc...
3
+ class Worker
4
+ attr_reader :name, :hash_rate, :stale_rate, :last_seen
5
+
6
+ def initialize(name: '', hash_rate: 0, stale_rate: 0.0, last_seen: 0)
7
+ @name = name.split('_', 2).last
8
+ @name = name if @name.nil? || @name.empty?
9
+ @hash_rate = hash_rate
10
+ @stale_rate = stale_rate
11
+ @last_seen = Time.at(last_seen)
12
+ end
13
+ end
14
+ end
data/spec/factories.rb ADDED
@@ -0,0 +1,26 @@
1
+ FactoryGirl.define do
2
+ factory :worker, class: WaffleAPI::Worker do
3
+ name { Faker::Bitcoin.address }
4
+ hash_rate { Faker::Number.number(7).to_i }
5
+ stale_rate { Faker::Number.decimal(1).to_f }
6
+ last_seen { Time.now.to_i }
7
+
8
+ initialize_with { new(attributes) }
9
+ end
10
+
11
+ factory :payment, class: WaffleAPI::Payment do
12
+ amount { Faker::Number.decimal(0, 6).to_f }
13
+ paid_at { Time.now.to_s }
14
+ transaction_hash { Faker::Lorem.characters(64) }
15
+
16
+ initialize_with { new(attributes) }
17
+ end
18
+
19
+ factory :balances, class: WaffleAPI::Balances do
20
+ sent { Faker::Number.decimal(1, 6).to_f }
21
+ confirmed { Faker::Number.decimal(0, 6).to_f }
22
+ unconverted { Faker::Number.decimal(0, 6).to_f }
23
+
24
+ initialize_with { new(attributes) }
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe WaffleAPI::Balances do
4
+ subject { build :balances }
5
+
6
+ it 'should have expected attributes' do
7
+ subject.should respond_to :sent
8
+ subject.should respond_to :confirmed
9
+ subject.should respond_to :unconverted
10
+ subject.should respond_to :expected
11
+ end
12
+
13
+ it 'should correctly set expected' do
14
+ subject.expected.should == subject.confirmed + subject.unconverted
15
+ end
16
+ end
@@ -17,47 +17,87 @@ describe WaffleAPI::Client do
17
17
  end
18
18
  end
19
19
 
20
- describe 'with a valid address' do
20
+ describe 'If wafflepool has moved his api endpoint' do
21
+ subject do
22
+ stub_waffle_with Wafflepool::EndPointMoved
23
+ WaffleAPI::Client.new address: BTC_ADDRESS
24
+ end
25
+
26
+ it 'raises WaffleAPI::Error::EndPointMoved' do
27
+ expect do
28
+ subject.hashrate
29
+ end.to raise_error(WaffleAPI::Error::EndPointMoved)
30
+ end
31
+ end
32
+
33
+ describe 'with address which not exist on waffle' do
34
+ subject do
35
+ stub_waffle_with Wafflepool::AddressNotFound
36
+ WaffleAPI::Client.new address: BTC_ADDRESS
37
+ end
38
+
39
+ it 'raises WaffleAPI::Error::AddressNotFound' do
40
+ expect do
41
+ subject.hashrate
42
+ end.to raise_error(WaffleAPI::Error::AddressNotFound)
43
+ end
44
+ end
45
+
46
+ describe 'with a not yet supported error' do
21
47
  subject do
48
+ stub_waffle_with Wafflepool::UnknownError
22
49
  WaffleAPI::Client.new address: BTC_ADDRESS
23
50
  end
24
51
 
25
- describe 'which not exist on waffle' do
26
- it 'raises WaffleAPI::Error::AddressNotFound' do
27
- stub_waffle_with Wafflepool::AddressNotFound
52
+ it 'raises WaffleAPI::Error::UnknownError' do
53
+ expect do
54
+ subject.hashrate
55
+ end.to raise_error(WaffleAPI::Error::UnknownError)
56
+ end
57
+ end
58
+
59
+ describe 'with a valid address' do
60
+ subject do
61
+ stub_waffle_with Wafflepool::Success
62
+ WaffleAPI::Client.new address: BTC_ADDRESS
63
+ end
28
64
 
65
+ describe 'asking for a non existing key' do
66
+ it 'raises NoMethodError' do
29
67
  expect do
30
- subject.hashrate
31
- end.to raise_error(WaffleAPI::Error::AddressNotFound)
68
+ subject.i_hope_this_key_will_never_exist
69
+ end.to raise_error(NoMethodError)
32
70
  end
33
71
  end
34
72
 
35
- describe 'which exist on waffle' do
36
- describe 'with a not yet supported error' do
37
- it 'raises WaffleAPI::Error::UnknownError' do
38
- stub_waffle_with Wafflepool::UnknownError
39
-
40
- expect do
41
- subject.i_hope_this_key_will_never_exist
42
- end.to raise_error(WaffleAPI::Error::UnknownError)
43
- end
73
+ describe 'hashrate' do
74
+ it 'returns correct hashrate' do
75
+ subject.hash_rate.should == 4_303_407
44
76
  end
45
77
 
46
- describe 'asking for a non existing key' do
47
- it 'raises NoMethodError' do
48
- stub_waffle_with Wafflepool::Success
78
+ it 'returns correct hashrate' do
79
+ subject.hash_rate.should == 4_303_407
80
+ end
81
+ end
49
82
 
50
- expect do
51
- subject.i_hope_this_key_will_never_exist
52
- end.to raise_error(NoMethodError)
83
+ describe 'workers' do
84
+ it 'returns an array of WaffleAPI::Worker' do
85
+ subject.workers.each do |worker|
86
+ worker.should be_an_instance_of WaffleAPI::Worker
53
87
  end
54
88
  end
89
+ end
55
90
 
56
- describe 'asking for an existing key' do
57
- it 'just return it' do
58
- stub_waffle_with Wafflepool::Success
91
+ describe 'balances' do
92
+ it 'returns an instance of WaffleAPI::Balnaces' do
93
+ subject.balances.should be_an_instance_of WaffleAPI::Balances
94
+ end
95
+ end
59
96
 
60
- expect(subject.hashrate).to eq 4_303_407
97
+ describe 'payments' do
98
+ it 'returns an array of WaffleAPI::Payment' do
99
+ subject.payments.each do |payment|
100
+ payment.should be_an_instance_of WaffleAPI::Payment
61
101
  end
62
102
  end
63
103
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe WaffleAPI::Payment do
4
+ subject { build :payment }
5
+
6
+ it 'should have expected attributes' do
7
+ subject.should respond_to :amount
8
+ subject.should respond_to :paid_at
9
+ subject.should respond_to :transaction_hash
10
+ end
11
+
12
+ it 'should parse paid_at to Time' do
13
+ subject.paid_at.should be_an_instance_of Time
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe WaffleAPI::Worker do
4
+ subject { build :worker }
5
+
6
+ it 'should have expected attributes' do
7
+ subject.should respond_to :name
8
+ subject.should respond_to :hash_rate
9
+ subject.should respond_to :stale_rate
10
+ subject.should respond_to :last_seen
11
+ end
12
+
13
+ it 'should parse last_seen to Time' do
14
+ subject.last_seen.should be_an_instance_of Time
15
+ end
16
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,14 +1,18 @@
1
1
  if ENV['TRAVIS']
2
2
  require 'coveralls'
3
+ require 'codeclimate-test-reporter'
3
4
  Coveralls.wear!
5
+ CodeClimate::TestReporter.start
4
6
  end
5
7
 
6
- require 'webmock/rspec'
7
8
  require 'waffle_api'
9
+ require 'webmock/rspec'
10
+ require 'faker'
11
+ require 'factory_girl'
8
12
 
9
13
  Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |f| require f }
10
14
 
11
- WebMock.disable_net_connect! allow_localhost: true
15
+ WebMock.disable_net_connect! allow_localhost: true, allow: 'codeclimate.com'
12
16
 
13
17
  BTC_ADDRESS = '1Ju8U9Ukfc5kiMqzQrRgQBP1JvRkeSv94V'
14
18
  URL = "http://wafflepool.com/tmp_api?address=#{BTC_ADDRESS}"
@@ -0,0 +1,5 @@
1
+ RSpec.configure do |config|
2
+ config.include FactoryGirl::Syntax::Methods
3
+ end
4
+
5
+ FactoryGirl.find_definitions
data/waffle_api.gemspec CHANGED
@@ -20,5 +20,8 @@ Gem::Specification.new do |s|
20
20
  s.add_development_dependency 'rspec', '~> 2.14'
21
21
  s.add_development_dependency 'sinatra', '~> 1.4'
22
22
  s.add_development_dependency 'webmock', '~> 1.17'
23
+ s.add_development_dependency 'factory_girl', '~> 4.0'
24
+ s.add_development_dependency 'faker', '~> 1.3'
23
25
  s.add_development_dependency 'coveralls', '~> 0.7'
26
+ s.add_development_dependency 'codeclimate-test-reporter', '~> 0.3'
24
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waffle_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Calyhre
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faker
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.3'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: coveralls
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +108,20 @@ dependencies:
80
108
  - - "~>"
81
109
  - !ruby/object:Gem::Version
82
110
  version: '0.7'
111
+ - !ruby/object:Gem::Dependency
112
+ name: codeclimate-test-reporter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.3'
83
125
  description: Simple gem to make fast and cached calls to Wafflepool API
84
126
  email:
85
127
  - contact@calyh.re
@@ -96,12 +138,20 @@ files:
96
138
  - Rakefile
97
139
  - lib/waffle_api.rb
98
140
  - lib/waffle_api/address_validator.rb
141
+ - lib/waffle_api/balances.rb
99
142
  - lib/waffle_api/client.rb
100
143
  - lib/waffle_api/errors.rb
101
144
  - lib/waffle_api/helpers.rb
145
+ - lib/waffle_api/payment.rb
102
146
  - lib/waffle_api/version.rb
147
+ - lib/waffle_api/worker.rb
148
+ - spec/factories.rb
149
+ - spec/lib/waffle_api/balances_spec.rb
103
150
  - spec/lib/waffle_api/client_spec.rb
151
+ - spec/lib/waffle_api/payment_spec.rb
152
+ - spec/lib/waffle_api/worker_spec.rb
104
153
  - spec/spec_helper.rb
154
+ - spec/support/factory_girl.rb
105
155
  - spec/support/fixtures/error.json
106
156
  - spec/support/fixtures/not_found.json
107
157
  - spec/support/fixtures/success.json
@@ -132,8 +182,13 @@ signing_key:
132
182
  specification_version: 4
133
183
  summary: Waffle pool API gem
134
184
  test_files:
185
+ - spec/factories.rb
186
+ - spec/lib/waffle_api/balances_spec.rb
135
187
  - spec/lib/waffle_api/client_spec.rb
188
+ - spec/lib/waffle_api/payment_spec.rb
189
+ - spec/lib/waffle_api/worker_spec.rb
136
190
  - spec/spec_helper.rb
191
+ - spec/support/factory_girl.rb
137
192
  - spec/support/fixtures/error.json
138
193
  - spec/support/fixtures/not_found.json
139
194
  - spec/support/fixtures/success.json