Sha256: 8859a3d2874c4c97e56608ae6e3872f783a412a6a19f014f3cf912e48ee6847d

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'memoist'
require 'faraday'
require 'better-faraday'

module Peatio
  module Goldcash
    class Client
      Error = Class.new(StandardError)
      ConnectionError = Class.new(Error)

      class ResponseError < Error
        def initialize(code, msg)
          @code = code
          @msg = msg
        end

        def message
          "#{@msg} (#{@code})"
        end
      end

      extend Memoist

      def initialize(endpoint)
        @json_rpc_endpoint = URI.parse(endpoint)
      end

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

      private

      def connection
        @connection ||= Faraday.new(@json_rpc_endpoint) do |f|
                          f.adapter :net_http_persistent, pool_size: 5
                        end.tap do |connection|
          unless @json_rpc_endpoint.user.blank?
            connection.basic_auth(@json_rpc_endpoint.user,
                                  @json_rpc_endpoint.password)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
peatio-goldcash-2.6.6 lib/peatio/goldcash/client.rb
peatio-goldcash-2.6.5 lib/peatio/goldcash/client.rb
peatio-goldcash-2.6.4 lib/peatio/goldcash/client.rb
peatio-goldcash-2.6.3 lib/peatio/goldcash/client.rb
peatio-goldcash-2.6.2 lib/peatio/goldcash/client.rb
peatio-goldcash-2.6.1 lib/peatio/goldcash/client.rb