Sha256: 560dace46ce35664c53bc16981a6ca9ced6fa8d2ec0ed38a4906d9b4a604bb51

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 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
        raise ConnectionError, e
      rescue StandardError => e
        raise Error, e
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
peatio-bitgo-1.1.1 lib/peatio/bitgo/client.rb
peatio-bitgo-1.1.0 lib/peatio/bitgo/client.rb
peatio-bitgo-1.0.1 lib/peatio/bitgo/client.rb
peatio-bitgo-1.0.0 lib/peatio/bitgo/client.rb