Class: Peatio::Bitcoincash::Client

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/peatio/bitcoincash/client.rb

Defined Under Namespace

Classes: ConnectionError, ResponseError

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
# File 'lib/peatio/bitcoincash/client.rb', line 25

def initialize(endpoint)
  @json_rpc_endpoint = URI.parse(endpoint)
  @path = @json_rpc_endpoint.path.empty? ? "/" : @json_rpc_endpoint.path
  @idle_timeout = idle_timeout
end

Instance Method Details

#json_rpc(method, params = []) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/peatio/bitcoincash/client.rb', line 31

def json_rpc(method, params = [])
  response = connection.post \
  @path,
  { jsonrpc: '1.0', method: method, params: params }.to_json,
  { 'Accept' => 'application/json',
    'coin-name' => 'bitcoincash',
    'Content-Type' => 'application/json' }
  response.assert_2xx!
  response = JSON.parse(response.body)
  response['error'].tap { |e| raise ResponseError.new(e['code'], e['message']) if e }
  response.fetch('result')
rescue => e
  if e.is_a?(Error)
    raise e
  elsif e.is_a?(Faraday::Error)
    raise ConnectionError, e
  else
    raise Error, e
  end
end