Class: Peatio::Bitgo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/peatio/bitgo/client.rb

Defined Under Namespace

Classes: ResponseError

Constant Summary collapse

Error =
Class.new(StandardError)
ConnectionError =
Class.new(Error)

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, access_token) ⇒ Client

Returns a new instance of Client.



16
17
18
19
# File 'lib/peatio/bitgo/client.rb', line 16

def initialize(endpoint, access_token)
  @endpoint = URI.parse(endpoint)
  @access_token = access_token
end

Instance Method Details

#rest_api(verb, path, data = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/peatio/bitgo/client.rb', line 21

def rest_api(verb, path, data = nil)
  args = [@endpoint.to_s + path]

  if data
    if %i[post put patch].include?(verb)
      args << data.compact.to_json
      args << { 'Content-Type' => 'application/json' }
    else
      args << data.compact
      args << {}
    end
  else
    args << nil
    args << {}
  end

  args.last['Accept']        = 'application/json'
  args.last['Authorization'] = 'Bearer ' + @access_token

  response = Faraday.send(verb, *args)
  response.assert_2xx!
  response = JSON.parse(response.body)
  response['error'].tap { |error| raise ResponseError.new(error) if error }
  response
rescue Faraday::Error => e
  if e.is_a?(Faraday::ConnectionFailed) || e.is_a?(Faraday::TimeoutError)
    raise ConnectionError, e
  else
    raise ConnectionError, JSON.parse(e.response.body)['message']
  end
end