Sha256: dd711acafc0afcb2e2f1298df871de58295e932f374375cdc5e52f983e896984

Contents?: true

Size: 1.45 KB

Versions: 9

Compression:

Stored size: 1.45 KB

Contents

require 'faraday'
require 'better-faraday'

module Peatio
  module Bitgo
    class Client
      Error = Class.new(StandardError)
      class ConnectionError < Error; end

      class ResponseError < Error
        def initialize(msg)
          super "#{msg}"
        end
      end

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

      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_success!
        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
      rescue StandardError => e
        raise Error, e
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
peatio-bitgo-2.5.2 lib/peatio/bitgo/client.rb
peatio-bitgo-jruby-2.5.1 lib/peatio/bitgo/client.rb
peatio-bitgo-2.5.1 lib/peatio/bitgo/client.rb
peatio-bitgo-2.5.0 lib/peatio/bitgo/client.rb
peatio-bitgo-1.1.6 lib/peatio/bitgo/client.rb
peatio-bitgo-1.1.5 lib/peatio/bitgo/client.rb
peatio-bitgo-1.1.4 lib/peatio/bitgo/client.rb
peatio-bitgo-1.1.3 lib/peatio/bitgo/client.rb
peatio-bitgo-1.1.2 lib/peatio/bitgo/client.rb