lib/peatio/goldcash/client.rb in peatio-goldcash-2.6.6 vs lib/peatio/goldcash/client.rb in peatio-goldcash-2.6.7

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true -require 'memoist' -require 'faraday' -require 'better-faraday' +require "memoist" +require "faraday" +require "better-faraday" module Peatio module Goldcash class Client Error = Class.new(StandardError) @@ -25,31 +25,35 @@ 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' } + def json_rpc(method, params=[]) + response = post(method, params) + response.assert_2xx! response = JSON.parse(response.body) - response['error'].tap do |e| - raise ResponseError.new(e['code'], e['message']) if e + + response["error"].tap do |e| + raise ResponseError.new(e["code"], e["message"]) if e end - response.fetch('result') + + response.fetch("result") rescue Faraday::Error => e raise ConnectionError, e end private + def post(method, params) + connection.post("/", {jsonrpc: "1.0", method: method, params: params}.to_json, + "Accept" => "application/json", "Content-Type" => "application/json") + end + def connection @connection ||= Faraday.new(@json_rpc_endpoint) do |f| - f.adapter :net_http_persistent, pool_size: 5 - end.tap do |connection| + 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