cgminer-api 0.0.4 → 0.1.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: 7756647b4310355759dc0bdd5c012143b3cc8646
4
- data.tar.gz: 58a41157a89c31d15e90ca972915d30ddc6aeb19
3
+ metadata.gz: 258b3f4a7e4028128e92b2bbcb026a2e97cbde53
4
+ data.tar.gz: 85ae5352fd614a55d1085c70e43f54841ace30ee
5
5
  SHA512:
6
- metadata.gz: 2f052bde67c8d0c0ff4c0c95338fc121066340095e4d1c669b6e1dfed5edb0cfa417ccf87bc4999dbde28842e944579f13007a1c67961311cf70e6c2307ba0a8
7
- data.tar.gz: 1bce611fdab7a6ad2f8bcc27043d9b0bf722eaf859c9434f0be2c628e67dda2ec28591032111dad6d1b8cbfdc96791da6ffa1f5e48e56ac8a1fe371bfb2c67d7
6
+ metadata.gz: c593334573da5e8e2ff38fa8ea9b7df34ffdeaca094001cb98a08c0964073847d5d351f853acf09bff655c9a8718df3fdb391bbc12c1036d5b41f1b4357c2ea6
7
+ data.tar.gz: ba4c8d4d7483874049e01c7b2f3fc884745f01f051fce501fe6b3cf7f99e1113f4990835eb0ee6027b32aec2c408bcdb6f18791291eee74df57595e35c038f50
@@ -3,6 +3,10 @@ module CGMiner
3
3
 
4
4
  class Client
5
5
 
6
+ attr_reader :host
7
+
8
+ attr_reader :port
9
+
6
10
  def initialize(host, port)
7
11
  @host = host
8
12
  @port = port
@@ -76,19 +80,11 @@ module CGMiner
76
80
  private
77
81
 
78
82
  def command(symbol, *parameters)
79
- api = Net::Telnet::new('Host' => @host, 'Port' => @port)
80
- results = api.cmd({ command: symbol.to_s, parameter: parameters.join(',') }.to_json)
81
-
82
- begin
83
- json = JSON.parse(results)
84
- status = json['STATUS'][0]
85
- reply = json[symbol.to_s.upcase]
86
- response = CGMiner::API::Response.new(status, reply)
87
- rescue JSON::ParserError => e
88
- raise RuntimeError, "Failed parsing response: #{e}"
89
- end
83
+ telnet = Net::Telnet::new('Host' => @host, 'Port' => @port)
84
+ results = telnet.cmd({ command: symbol.to_s, parameter: parameters.join(',') }.to_json)
85
+ CGMiner::API::Response.new(symbol, results)
90
86
  ensure
91
- api.close unless api.nil?
87
+ telnet.close unless telnet.nil?
92
88
  end
93
89
 
94
90
  end
@@ -7,9 +7,12 @@ module CGMiner
7
7
 
8
8
  attr_reader :body
9
9
 
10
- def initialize(status, body)
11
- @status = status
12
- @body = body
10
+ attr_reader :raw
11
+
12
+ def initialize(command_symbol, raw_command_response)
13
+ @raw = JSON.parse(raw_command_response)
14
+ @status = @raw['STATUS'][0]
15
+ @body = @raw[command_symbol.to_s.upcase]
13
16
  end
14
17
 
15
18
  def received_at
@@ -1,5 +1,5 @@
1
1
  module CGMiner
2
2
  module API
3
- VERSION = '0.0.4'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ describe CGMiner::API::Client do
4
+
5
+ let(:host) { 'my-hostname' }
6
+
7
+ let(:port) { 1234 }
8
+
9
+ let(:telnet) do
10
+ telnet = double('Net::Telnet')
11
+ end
12
+
13
+ before(:each) do
14
+ stub_const 'Net::Telnet', double(new: telnet)
15
+ @client = described_class.new(host, port)
16
+ end
17
+
18
+ subject { @client }
19
+
20
+ context 'successful communication' do
21
+
22
+ let(:reply) do
23
+ <<-EOF
24
+ {"STATUS":[{"STATUS":"S","When":1391459548,"Code":78,"Msg":"CGMiner coin","Description":"cgminer 3.7.2"}],"COIN":[{"Hash Method":"scrypt","Current Block Time":1391457585.060405,"Current Block Hash":"0012a85cfa3d1b7cc00bf56a5c8cd283e8ca95995d040c42ff4c66f219e24567","LP":true,"Network Difficulty":10.87349847}],"id":1}
25
+ EOF
26
+ end
27
+
28
+ before(:each) do
29
+ telnet.stub(:cmd).and_return(reply)
30
+ telnet.should_receive(:close)
31
+ end
32
+
33
+ its(:coin) { should be_a(CGMiner::API::Response) }
34
+
35
+ end
36
+
37
+ context 'with parameters' do
38
+
39
+ let(:reply) do
40
+ <<-EOF
41
+ {"STATUS":[{"STATUS":"S","When":1391459548,"Code":78,"Msg":"CGMiner coin","Description":"cgminer 3.7.2"}],"ASC":[{"Hash Method":"scrypt","Current Block Time":1391457585.060405,"Current Block Hash":"0012a85cfa3d1b7cc00bf56a5c8cd283e8ca95995d040c42ff4c66f219e24567","LP":true,"Network Difficulty":10.87349847}],"id":1}
42
+ EOF
43
+ end
44
+
45
+ before(:each) do
46
+ expected = '{"command":"asc","parameter":"1"}'
47
+ telnet.stub(:cmd).with(expected).and_return(reply)
48
+ telnet.should_receive(:close)
49
+ end
50
+
51
+ specify { expect(subject.asc(1)).to be_a(CGMiner::API::Response) }
52
+
53
+ end
54
+
55
+ context 'failed communication' do
56
+
57
+ before(:each) do
58
+ telnet.stub(:cmd).and_raise(TimeoutError)
59
+ telnet.should_receive(:close)
60
+ end
61
+
62
+ specify { expect { subject.asc(0) }.to raise_error(TimeoutError) }
63
+
64
+ specify { expect { subject.asccount }.to raise_error(TimeoutError) }
65
+
66
+ specify { expect { subject.check(:version) }.to raise_error(TimeoutError) }
67
+
68
+ specify { expect { subject.coin }.to raise_error(TimeoutError) }
69
+
70
+ specify { expect { subject.config }.to raise_error(TimeoutError) }
71
+
72
+ specify { expect { subject.devdetails }.to raise_error(TimeoutError) }
73
+
74
+ specify { expect { subject.devs }.to raise_error(TimeoutError) }
75
+
76
+ specify { expect { subject.gpu(0) }.to raise_error(TimeoutError) }
77
+
78
+ specify { expect { subject.gpucount }.to raise_error(TimeoutError) }
79
+
80
+ specify { expect { subject.pga(0) }.to raise_error(TimeoutError) }
81
+
82
+ specify { expect { subject.pgacount }.to raise_error(TimeoutError) }
83
+
84
+ specify { expect { subject.pools }.to raise_error(TimeoutError) }
85
+
86
+ specify { expect { subject.stats }.to raise_error(TimeoutError) }
87
+
88
+ specify { expect { subject.summary }.to raise_error(TimeoutError) }
89
+
90
+ specify { expect { subject.usbstats }.to raise_error(TimeoutError) }
91
+
92
+ specify { expect { subject.version }.to raise_error(TimeoutError) }
93
+
94
+ end
95
+
96
+ context 'invalid JSON response' do
97
+
98
+ let(:reply) { "I AM A PRETTY BAD RESPONSE" }
99
+
100
+ before(:each) do
101
+ telnet.stub(:cmd).and_return(reply)
102
+ telnet.should_receive(:close)
103
+ end
104
+
105
+ specify { expect { subject.asc(0) }.to raise_error(JSON::ParserError) }
106
+
107
+ specify { expect { subject.asccount }.to raise_error(JSON::ParserError) }
108
+
109
+ specify { expect { subject.check(:version) }.to raise_error(JSON::ParserError) }
110
+
111
+ specify { expect { subject.coin }.to raise_error(JSON::ParserError) }
112
+
113
+ specify { expect { subject.config }.to raise_error(JSON::ParserError) }
114
+
115
+ specify { expect { subject.devdetails }.to raise_error(JSON::ParserError) }
116
+
117
+ specify { expect { subject.devs }.to raise_error(JSON::ParserError) }
118
+
119
+ specify { expect { subject.gpu(0) }.to raise_error(JSON::ParserError) }
120
+
121
+ specify { expect { subject.gpucount }.to raise_error(JSON::ParserError) }
122
+
123
+ specify { expect { subject.pga(0) }.to raise_error(JSON::ParserError) }
124
+
125
+ specify { expect { subject.pgacount }.to raise_error(JSON::ParserError) }
126
+
127
+ specify { expect { subject.pools }.to raise_error(JSON::ParserError) }
128
+
129
+ specify { expect { subject.stats }.to raise_error(JSON::ParserError) }
130
+
131
+ specify { expect { subject.summary }.to raise_error(JSON::ParserError) }
132
+
133
+ specify { expect { subject.usbstats }.to raise_error(JSON::ParserError) }
134
+
135
+ specify { expect { subject.version }.to raise_error(JSON::ParserError) }
136
+
137
+ end
138
+
139
+ its(:host) { should eql('my-hostname') }
140
+
141
+ its(:port) { should eql(1234) }
142
+
143
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe CGMiner::API::Response do
4
+
5
+ before(:each) do
6
+ @response = described_class.new(:coin, raw)
7
+ end
8
+
9
+ subject { @response }
10
+
11
+ context 'successful response' do
12
+
13
+ let(:raw) do
14
+ <<-EOF
15
+ {"STATUS":[{"STATUS":"S","When":1391459548,"Code":78,"Msg":"CGMiner coin","Description":"cgminer 3.7.2"}],"COIN":[{"Hash Method":"scrypt","Current Block Time":1391457585.060405,"Current Block Hash":"0012a85cfa3d1b7cc00bf56a5c8cd283e8ca95995d040c42ff4c66f219e24567","LP":true,"Network Difficulty":10.87349847}],"id":1}
16
+ EOF
17
+ end
18
+
19
+ its(:raw) { should be_a(Hash) }
20
+
21
+ its(:body) { should be_a(Array) }
22
+
23
+ its(:status) { should be_a(Hash) }
24
+
25
+ its(:received_at) { should be_a(DateTime) }
26
+
27
+ its(:success?) { should be_true }
28
+
29
+ end
30
+
31
+ context 'invalid command response' do
32
+
33
+ let(:raw) do
34
+ <<-EOF
35
+ {"STATUS":[{"STATUS":"E","When":1391459330,"Code":14,"Msg":"Invalid command","Description":"cgminer 3.7.2"}],"id":1}
36
+ EOF
37
+ end
38
+
39
+ its(:raw) { should be_a(Hash) }
40
+
41
+ its(:body) { should be_nil }
42
+
43
+ its(:status) { should be_a(Hash) }
44
+
45
+ its(:received_at) { should be_a(DateTime) }
46
+
47
+ its(:success?) { should be_false }
48
+
49
+ end
50
+
51
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cgminer-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Veys
@@ -139,6 +139,8 @@ files:
139
139
  - lib/cgminer/api/client.rb
140
140
  - lib/cgminer/api/response.rb
141
141
  - lib/cgminer/api/version.rb
142
+ - spec/client_spec.rb
143
+ - spec/response_spec.rb
142
144
  - spec/spec_helper.rb
143
145
  homepage: ''
144
146
  licenses:
@@ -165,4 +167,6 @@ signing_key:
165
167
  specification_version: 4
166
168
  summary: cgminer api gem
167
169
  test_files:
170
+ - spec/client_spec.rb
171
+ - spec/response_spec.rb
168
172
  - spec/spec_helper.rb