ethereum_client 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ethereum_client/error.rb +3 -0
- data/lib/ethereum_client.rb +23 -6
- data/spec/ethereum_client_spec.rb +46 -21
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26f7e99e4e76eab808a3e94dbb637907c374f1fa
|
4
|
+
data.tar.gz: a46e8fed049ef35f14ed9c7a1e1a98bd06869b38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d3acfc5d06dbc06c876bcd8e5e108f8cb59d758371aba99b676ead538c695a7edc73c3e227728e8521ba4358a5ddfb9ef3653f888230878240e60699e86a6b2e
|
7
|
+
data.tar.gz: cadff1a9b33f40f46385a5dc8e4f4d1b63b68f854d986e415dfd581d8289fd7125a9ffa993e637fc053df162623df96d5ae3b481ab9c72e65c8c4cea4b59a002
|
data/lib/ethereum_client.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'httparty'
|
3
|
+
require 'ethereum_client/error'
|
3
4
|
|
4
5
|
# Ethereum JSON RPC client
|
5
6
|
module EthereumClient
|
6
7
|
class << self
|
8
|
+
HEADERS = {
|
9
|
+
'Content-Type' => 'application/json',
|
10
|
+
'Accept' => 'application/json'
|
11
|
+
}.freeze
|
12
|
+
|
7
13
|
def web3_client_version
|
8
14
|
payload = {
|
9
15
|
method: 'web3_clientVersion'
|
@@ -12,18 +18,29 @@ module EthereumClient
|
|
12
18
|
post(payload)
|
13
19
|
end
|
14
20
|
|
21
|
+
def web3_sha3(string)
|
22
|
+
payload = {
|
23
|
+
method: 'web3_sha3',
|
24
|
+
params: [string]
|
25
|
+
}
|
26
|
+
|
27
|
+
post(payload)
|
28
|
+
end
|
29
|
+
|
15
30
|
private
|
16
31
|
|
17
32
|
def post(payload)
|
18
33
|
body = payload.to_json
|
19
|
-
headers = {
|
20
|
-
'Content-Type' => 'application/json',
|
21
|
-
'Accept' => 'application/json'
|
22
|
-
}
|
23
34
|
|
24
|
-
response = HTTParty.post(url, body: body, headers:
|
35
|
+
response = HTTParty.post(url, body: body, headers: HEADERS)
|
36
|
+
|
37
|
+
json = JSON.parse(response.body)
|
38
|
+
|
39
|
+
if json.key?('error')
|
40
|
+
raise EthereumClient::Error, json['error']['message']
|
41
|
+
end
|
25
42
|
|
26
|
-
|
43
|
+
json['result']
|
27
44
|
end
|
28
45
|
|
29
46
|
def url
|
@@ -5,35 +5,60 @@ RSpec.describe EthereumClient do
|
|
5
5
|
subject { EthereumClient }
|
6
6
|
|
7
7
|
describe '.web3_client_version' do
|
8
|
-
|
8
|
+
context 'method not implemented' do
|
9
|
+
let(:error_class) { EthereumClient::Error }
|
10
|
+
let(:error_message) { 'web3_clientVersion method not implemented' }
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
it 'raises error with a message' do
|
13
|
+
expect(subject).to receive(:post)
|
14
|
+
.once
|
15
|
+
.and_raise(error_class, error_message)
|
16
|
+
|
17
|
+
expect { subject.web3_client_version }.to raise_error(
|
18
|
+
error_class,
|
19
|
+
error_message
|
20
|
+
)
|
21
|
+
end
|
14
22
|
end
|
15
23
|
|
16
|
-
|
17
|
-
{
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
24
|
+
context 'api working properly' do
|
25
|
+
let(:client_version) { 'Geth/v1.3.6/linux/go1.5.1' }
|
26
|
+
|
27
|
+
it 'returns client version' do
|
28
|
+
expect(subject).to receive(:post).once.and_return(client_version)
|
29
|
+
|
30
|
+
expect(subject.web3_client_version).to eq(client_version)
|
31
|
+
end
|
22
32
|
end
|
33
|
+
end
|
23
34
|
|
24
|
-
|
25
|
-
|
26
|
-
'Content-Type' => 'application/json',
|
27
|
-
'Accept' => 'application/json'
|
28
|
-
}
|
35
|
+
describe '.web3_sha3' do
|
36
|
+
let(:string) { '0x68656c6c6f20776f726c64' }
|
29
37
|
|
30
|
-
|
31
|
-
|
32
|
-
|
38
|
+
context 'method not implemented' do
|
39
|
+
let(:error_class) { EthereumClient::Error }
|
40
|
+
let(:error_message) { 'web3_sha3 method not implemented' }
|
41
|
+
|
42
|
+
it 'raises error with a message' do
|
43
|
+
expect(subject).to receive(:post)
|
44
|
+
.once
|
45
|
+
.and_raise(error_class, error_message)
|
46
|
+
|
47
|
+
expect { subject.web3_sha3(string) }.to raise_error(
|
48
|
+
error_class,
|
49
|
+
error_message
|
50
|
+
)
|
51
|
+
end
|
33
52
|
end
|
34
53
|
|
35
|
-
|
36
|
-
|
54
|
+
context 'api working properly' do
|
55
|
+
let(:sha3) { '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad' }
|
56
|
+
|
57
|
+
it 'returns string' do
|
58
|
+
expect(subject).to receive(:post).once.and_return(sha3)
|
59
|
+
|
60
|
+
expect(subject.web3_sha3(string)).to eq(sha3)
|
61
|
+
end
|
37
62
|
end
|
38
63
|
end
|
39
64
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ethereum_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vlad D
|
@@ -59,6 +59,7 @@ extensions: []
|
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
61
|
- lib/ethereum_client.rb
|
62
|
+
- lib/ethereum_client/error.rb
|
62
63
|
- spec/ethereum_client_spec.rb
|
63
64
|
- spec/spec_helper.rb
|
64
65
|
homepage: https://github.com/dmxhZGp1c2hh/ethereum_client
|