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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87041530e64eeda02260b56560e9150fd120600e
4
- data.tar.gz: 4212dcd6823040b8a70c7fefd362ff659c5e204a
3
+ metadata.gz: 26f7e99e4e76eab808a3e94dbb637907c374f1fa
4
+ data.tar.gz: a46e8fed049ef35f14ed9c7a1e1a98bd06869b38
5
5
  SHA512:
6
- metadata.gz: 1fa1417b4fe646cf16224ab60bcd6c8a7d4dc78f67efdb7f51b4b4df7f4ba4d186f2bb4c4bb92022eab532bf4617f31fe83b06f50e10ae094b3767700a2fbc69
7
- data.tar.gz: 1ec1bf84023c41d0ef51a7073d403337fb160f5a62cec2b3f5a7914c49c7a80c06b8acd9ec11d677ca84174256e26e7b6f7c97b076417319941e5952483f2583
6
+ metadata.gz: d3acfc5d06dbc06c876bcd8e5e108f8cb59d758371aba99b676ead538c695a7edc73c3e227728e8521ba4358a5ddfb9ef3653f888230878240e60699e86a6b2e
7
+ data.tar.gz: cadff1a9b33f40f46385a5dc8e4f4d1b63b68f854d986e415dfd581d8289fd7125a9ffa993e637fc053df162623df96d5ae3b481ab9c72e65c8c4cea4b59a002
@@ -0,0 +1,3 @@
1
+ module EthereumClient
2
+ Error = Class.new(StandardError)
3
+ end
@@ -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: 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
- JSON.parse(response.body)['result']
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
- let(:web3_client_version) { 'Geth/v1.3.6-9e323d65/linux/go1.6' }
8
+ context 'method not implemented' do
9
+ let(:error_class) { EthereumClient::Error }
10
+ let(:error_message) { 'web3_clientVersion method not implemented' }
9
11
 
10
- let(:request_body) do
11
- {
12
- method: 'web3_clientVersion'
13
- }.to_json
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
- let(:response_body) do
17
- {
18
- id: nil,
19
- jsonrpc: '',
20
- result: web3_client_version
21
- }.to_json
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
- before do
25
- headers = {
26
- 'Content-Type' => 'application/json',
27
- 'Accept' => 'application/json'
28
- }
35
+ describe '.web3_sha3' do
36
+ let(:string) { '0x68656c6c6f20776f726c64' }
29
37
 
30
- stub_request(:post, 'http://localhost:8545')
31
- .with(body: request_body, headers: headers)
32
- .to_return(status: 200, body: response_body)
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
- it 'makes proper json rpc request' do
36
- expect(subject.web3_client_version).to eq(web3_client_version)
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.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