expedition 0.1.0 → 0.2.0
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 +4 -4
- data/.travis.yml +0 -1
- data/expedition.gemspec +3 -1
- data/lib/expedition.rb +10 -2
- data/lib/expedition/client.rb +26 -5
- data/lib/expedition/response.rb +15 -10
- data/lib/expedition/status.rb +10 -10
- data/lib/expedition/version.rb +1 -1
- data/spec/expedition/client_spec.rb +1 -1
- data/spec/expedition/response_spec.rb +23 -31
- data/spec/spec_helper.rb +4 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88b001358532eca38c34e829587ff88f5ce5e0f0
|
4
|
+
data.tar.gz: 38bf70bc6eec3aee3453487d19833170eac1c4af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 111ba58baef943aac22bd2b4a618f56e40b375d27b8395b0f6fcd016bf30922eadb588f9275cb5e9c91ee2f96b67a1bbb3f63cde0b53b07c764f7614c200b7d2
|
7
|
+
data.tar.gz: b51384cbb26b4c65bb5a075f59a9ba4a24a562cce966c8f8a875051af0585e27cb313866a4b70cec78c54a5d52709e317e6b537de0af3fa8214452267abd73c2
|
data/.travis.yml
CHANGED
data/expedition.gemspec
CHANGED
@@ -13,13 +13,15 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = 'https://github.com/gevans/expedition'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
|
+
spec.required_ruby_version = '>= 2.0.0'
|
17
|
+
|
16
18
|
spec.files = `git ls-files -z`.split("\x0")
|
17
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
20
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
21
|
spec.require_paths = %w(lib)
|
20
22
|
|
21
23
|
spec.add_dependency 'multi_json', '~> 1.3'
|
22
|
-
spec.add_dependency 'activesupport', '
|
24
|
+
spec.add_dependency 'activesupport', '>= 3.2'
|
23
25
|
|
24
26
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
25
27
|
spec.add_development_dependency 'coveralls'
|
data/lib/expedition.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
|
-
require 'active_support'
|
2
|
-
require 'active_support/core_ext'
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
3
2
|
|
4
3
|
require 'expedition/client'
|
5
4
|
require 'expedition/version'
|
6
5
|
|
7
6
|
module Expedition
|
8
7
|
|
8
|
+
class << self
|
9
|
+
##
|
10
|
+
# @return [Client]
|
11
|
+
# A client for accessing the API of a cgminer-compatible service.
|
12
|
+
attr_accessor :client
|
13
|
+
|
14
|
+
delegate(*Client.public_instance_methods(false), to: :client)
|
15
|
+
end
|
16
|
+
|
9
17
|
##
|
10
18
|
# Initializes a new {Expedition::Client}.
|
11
19
|
#
|
data/lib/expedition/client.rb
CHANGED
@@ -29,6 +29,27 @@ module Expedition
|
|
29
29
|
@port = port
|
30
30
|
end
|
31
31
|
|
32
|
+
def devices
|
33
|
+
send(:devdetails) do |body|
|
34
|
+
body[:devdetails].collect { |attrs|
|
35
|
+
attrs.delete(:devdetails)
|
36
|
+
attrs[:variant] = attrs.delete(:name).downcase
|
37
|
+
attrs
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def metrics
|
43
|
+
send(:devs) do |body|
|
44
|
+
body[:devs].collect { |attrs|
|
45
|
+
attrs.merge(
|
46
|
+
enabled: attrs[:enabled] == 'Y',
|
47
|
+
status: attrs[:status].downcase
|
48
|
+
)
|
49
|
+
}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
32
53
|
##
|
33
54
|
# Sends the supplied `command` with optionally supplied `parameters` to the
|
34
55
|
# service and returns the result, if any.
|
@@ -44,21 +65,21 @@ module Expedition
|
|
44
65
|
#
|
45
66
|
# @return [Response]
|
46
67
|
# The service's response.
|
47
|
-
def send(command, *parameters)
|
68
|
+
def send(command, *parameters, &block)
|
48
69
|
socket = TCPSocket.new(host, port)
|
49
70
|
socket.puts command_json(command, *parameters)
|
50
71
|
|
51
|
-
parse(socket.gets)
|
72
|
+
parse(socket.gets, &block)
|
52
73
|
ensure
|
53
74
|
socket.close if socket.respond_to?(:close)
|
54
75
|
end
|
55
76
|
|
56
|
-
|
77
|
+
alias_method :method_missing, :send
|
57
78
|
|
58
79
|
private
|
59
80
|
|
60
|
-
def parse(response)
|
61
|
-
Response.parse(MultiJson.load(response.chomp("\x0")))
|
81
|
+
def parse(response, &block)
|
82
|
+
Response.parse(MultiJson.load(response.chomp("\x0")), &block)
|
62
83
|
end
|
63
84
|
|
64
85
|
def command_json(command, *parameters)
|
data/lib/expedition/response.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
2
|
+
|
1
3
|
require 'expedition/status'
|
2
4
|
|
3
5
|
module Expedition
|
@@ -10,27 +12,29 @@ module Expedition
|
|
10
12
|
attr_reader :status
|
11
13
|
|
12
14
|
def self.parse(raw)
|
13
|
-
|
15
|
+
normalized = normalize(raw)
|
16
|
+
normalized = yield normalized if block_given?
|
17
|
+
new(normalized, raw)
|
14
18
|
end
|
15
19
|
|
16
20
|
def initialize(normalized, raw)
|
17
|
-
@body
|
18
|
-
@raw
|
19
|
-
@status = Status.new(
|
21
|
+
@body = normalized
|
22
|
+
@raw = raw
|
23
|
+
@status = Status.new(raw['STATUS'])
|
20
24
|
end
|
21
25
|
|
22
|
-
delegate :success?, :
|
26
|
+
delegate :success?, :info?, :warn?, :error?, :fatal?, :ok?,
|
23
27
|
:executed_at, to: :status
|
24
28
|
|
25
|
-
delegate :[], :to_hash, to: :body
|
29
|
+
delegate :[], :to_h, :to_hash, :to_a, :to_ary, to: :body
|
26
30
|
|
27
31
|
def respond_to_missing?(method_name, include_private = false)
|
28
|
-
body.
|
32
|
+
body.respond_to?(method_name) || super
|
29
33
|
end
|
30
34
|
|
31
35
|
def method_missing(method_name, *arguments, &block)
|
32
36
|
if respond_to_missing?(method_name)
|
33
|
-
body
|
37
|
+
body.send(method_name, *arguments, &block)
|
34
38
|
else
|
35
39
|
super
|
36
40
|
end
|
@@ -41,7 +45,7 @@ module Expedition
|
|
41
45
|
def self.normalize(value)
|
42
46
|
case value
|
43
47
|
when Hash
|
44
|
-
Hash[value.collect { |k, v| [normalize_key(k), normalize(v)] }]
|
48
|
+
Hash[value.collect { |k, v| [normalize_key(k), normalize(v)] }].with_indifferent_access
|
45
49
|
when Array
|
46
50
|
value.collect { |v| normalize(v) }
|
47
51
|
else
|
@@ -50,7 +54,8 @@ module Expedition
|
|
50
54
|
end
|
51
55
|
|
52
56
|
def self.normalize_key(key)
|
53
|
-
key.gsub(/(\w)%/, '\\1_percent').gsub('%', 'percent').gsub(
|
57
|
+
key = key.gsub(/(\w)%/, '\\1_percent').gsub('%', 'percent').gsub(/[^\w]+/, ' ')
|
58
|
+
key.strip.gsub(/\s+/, '_').downcase
|
54
59
|
end
|
55
60
|
end # Response
|
56
61
|
end # Expedition
|
data/lib/expedition/status.rb
CHANGED
@@ -4,12 +4,12 @@ module Expedition
|
|
4
4
|
SEVERITIES = {
|
5
5
|
'S' => :success,
|
6
6
|
'I' => :info,
|
7
|
-
'W' => :
|
7
|
+
'W' => :warn,
|
8
8
|
'E' => :error,
|
9
9
|
'F' => :fatal
|
10
10
|
}.freeze
|
11
11
|
|
12
|
-
OK_SEVERITIES = %i(success info
|
12
|
+
OK_SEVERITIES = %i(success info warn).freeze
|
13
13
|
|
14
14
|
attr_reader :severity
|
15
15
|
|
@@ -22,13 +22,13 @@ module Expedition
|
|
22
22
|
attr_reader :executed_at
|
23
23
|
|
24
24
|
def initialize(body)
|
25
|
-
status = body.
|
25
|
+
status = body ? body.first : {}
|
26
26
|
|
27
|
-
@severity = SEVERITIES[status[
|
28
|
-
@code = status[
|
29
|
-
@message = status[
|
30
|
-
@description = status[
|
31
|
-
@executed_at = Time.at(status[
|
27
|
+
@severity = SEVERITIES[status['STATUS']]
|
28
|
+
@code = status['Code']
|
29
|
+
@message = status['Msg']
|
30
|
+
@description = status['Description']
|
31
|
+
@executed_at = Time.at(status['When']) rescue nil
|
32
32
|
end
|
33
33
|
|
34
34
|
def success?
|
@@ -39,8 +39,8 @@ module Expedition
|
|
39
39
|
severity == :info
|
40
40
|
end
|
41
41
|
|
42
|
-
def
|
43
|
-
severity == :
|
42
|
+
def warn?
|
43
|
+
severity == :warn
|
44
44
|
end
|
45
45
|
|
46
46
|
def error?
|
data/lib/expedition/version.rb
CHANGED
@@ -45,7 +45,9 @@ describe Expedition::Response do
|
|
45
45
|
'Last Valid Work' => 1394735379,
|
46
46
|
'Device Hardware%' => 0.3998,
|
47
47
|
'Device Rejected%' => 0.4283,
|
48
|
-
'Device
|
48
|
+
'Device*Elapsed' => 178612,
|
49
|
+
'*Inconsistent-Bullshit*' => 'teehee',
|
50
|
+
'*Dev-* Throttle***' => 0
|
49
51
|
}
|
50
52
|
],
|
51
53
|
'id' => 1
|
@@ -95,11 +97,13 @@ describe Expedition::Response do
|
|
95
97
|
last_valid_work: 1394735379,
|
96
98
|
device_hardware_percent: 0.3998,
|
97
99
|
device_rejected_percent: 0.4283,
|
98
|
-
device_elapsed: 178612
|
100
|
+
device_elapsed: 178612,
|
101
|
+
inconsistent_bullshit: 'teehee',
|
102
|
+
dev_throttle: 0
|
99
103
|
}
|
100
104
|
],
|
101
105
|
id: 1
|
102
|
-
}
|
106
|
+
}.with_indifferent_access
|
103
107
|
end
|
104
108
|
|
105
109
|
let(:response) do
|
@@ -144,7 +148,7 @@ describe Expedition::Response do
|
|
144
148
|
expect(status).to be_success
|
145
149
|
|
146
150
|
expect(status).not_to be_info
|
147
|
-
expect(status).not_to
|
151
|
+
expect(status).not_to be_warn
|
148
152
|
expect(status).not_to be_info
|
149
153
|
expect(status).not_to be_info
|
150
154
|
expect(status).not_to be_info
|
@@ -165,7 +169,7 @@ describe Expedition::Response do
|
|
165
169
|
expect(status).to be_info
|
166
170
|
|
167
171
|
expect(status).not_to be_success
|
168
|
-
expect(status).not_to
|
172
|
+
expect(status).not_to be_warn
|
169
173
|
expect(status).not_to be_error
|
170
174
|
expect(status).not_to be_fatal
|
171
175
|
end
|
@@ -175,14 +179,14 @@ describe Expedition::Response do
|
|
175
179
|
end
|
176
180
|
end
|
177
181
|
|
178
|
-
context 'when
|
182
|
+
context 'when warn' do
|
179
183
|
|
180
184
|
before do
|
181
185
|
original_data['STATUS'].first['STATUS'] = 'W'
|
182
186
|
end
|
183
187
|
|
184
188
|
it 'is warning' do
|
185
|
-
expect(status).to
|
189
|
+
expect(status).to be_warn
|
186
190
|
|
187
191
|
expect(status).not_to be_success
|
188
192
|
expect(status).not_to be_info
|
@@ -206,7 +210,7 @@ describe Expedition::Response do
|
|
206
210
|
|
207
211
|
expect(status).not_to be_success
|
208
212
|
expect(status).not_to be_info
|
209
|
-
expect(status).not_to
|
213
|
+
expect(status).not_to be_warn
|
210
214
|
expect(status).not_to be_fatal
|
211
215
|
end
|
212
216
|
|
@@ -226,7 +230,7 @@ describe Expedition::Response do
|
|
226
230
|
|
227
231
|
expect(status).not_to be_success
|
228
232
|
expect(status).not_to be_info
|
229
|
-
expect(status).not_to
|
233
|
+
expect(status).not_to be_warn
|
230
234
|
expect(status).not_to be_error
|
231
235
|
end
|
232
236
|
|
@@ -236,7 +240,7 @@ describe Expedition::Response do
|
|
236
240
|
end
|
237
241
|
end
|
238
242
|
|
239
|
-
%i(success?
|
243
|
+
%i(success? info? warn? error? fatal? ok? executed_at).each do |method_name|
|
240
244
|
|
241
245
|
describe "##{method_name}" do
|
242
246
|
|
@@ -247,7 +251,7 @@ describe Expedition::Response do
|
|
247
251
|
end
|
248
252
|
end
|
249
253
|
|
250
|
-
%i([] to_hash).each do |method_name|
|
254
|
+
%i([] to_h to_hash to_a to_ary).each do |method_name|
|
251
255
|
|
252
256
|
describe "##{method_name}" do
|
253
257
|
|
@@ -260,28 +264,15 @@ describe Expedition::Response do
|
|
260
264
|
|
261
265
|
describe '#respond_to_missing?' do
|
262
266
|
|
263
|
-
|
264
|
-
described_class.new(body, {})
|
265
|
-
end
|
266
|
-
|
267
|
-
context 'when #body has key' do
|
268
|
-
|
269
|
-
let(:body) do
|
270
|
-
{
|
271
|
-
foo: 'blah'
|
272
|
-
}
|
273
|
-
end
|
267
|
+
context 'when #body responds' do
|
274
268
|
|
275
269
|
it 'returns true' do
|
270
|
+
expect(response.body).to receive(:respond_to?).with(:foo).and_return(true)
|
276
271
|
expect(response).to respond_to(:foo)
|
277
272
|
end
|
278
273
|
end
|
279
274
|
|
280
|
-
context 'when #body does not
|
281
|
-
|
282
|
-
let(:body) do
|
283
|
-
{}
|
284
|
-
end
|
275
|
+
context 'when #body does not respond' do
|
285
276
|
|
286
277
|
it 'returns false' do
|
287
278
|
expect(response).not_to respond_to(:foo)
|
@@ -291,18 +282,19 @@ describe Expedition::Response do
|
|
291
282
|
|
292
283
|
describe '#method_missing' do
|
293
284
|
|
294
|
-
context 'when
|
285
|
+
context 'when #body responds' do
|
295
286
|
|
296
287
|
before do
|
297
|
-
allow(response).to receive(:
|
288
|
+
allow(response.body).to receive(:respond_to?).and_return(true)
|
289
|
+
allow(response.body).to receive(:something).and_return('stuff')
|
298
290
|
end
|
299
291
|
|
300
|
-
it 'returns
|
292
|
+
it 'returns method call' do
|
301
293
|
expect(response.something).to eq('stuff')
|
302
294
|
end
|
303
295
|
end
|
304
296
|
|
305
|
-
context 'when
|
297
|
+
context 'when #body does not respond' do
|
306
298
|
|
307
299
|
it 'raises NoMethodError' do
|
308
300
|
expect {
|
data/spec/spec_helper.rb
CHANGED
@@ -5,7 +5,10 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
|
5
5
|
SimpleCov::Formatter::HTMLFormatter,
|
6
6
|
Coveralls::SimpleCov::Formatter
|
7
7
|
]
|
8
|
-
SimpleCov.start
|
8
|
+
SimpleCov.start do
|
9
|
+
add_filter '/.bundle/'
|
10
|
+
add_filter '/spec/'
|
11
|
+
end
|
9
12
|
|
10
13
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
11
14
|
require 'expedition'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expedition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabe Evans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '3.2'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '3.2'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -263,7 +263,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
263
263
|
requirements:
|
264
264
|
- - ">="
|
265
265
|
- !ruby/object:Gem::Version
|
266
|
-
version:
|
266
|
+
version: 2.0.0
|
267
267
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
268
268
|
requirements:
|
269
269
|
- - ">="
|