Sha256: 861f1432a9aa5d9d30de33d17966e5b4da4ed9b6ba3583f2d0e17a543dbe87d2

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require 'uri'
require 'net/http'
require 'json'

require_relative 'client_ext'

module ScaleRb
  class HttpClient
    include ClientExt
    attr_accessor :supported_methods

    def initialize(url)
      # check if the url is started with http or https
      url_regex = %r{^https?://}
      raise 'url format is not correct' unless url.match?(url_regex)

      @uri = URI.parse(url)
      @supported_methods = request('rpc_methods', [])[:methods]
    end

    def request(method, params = [])
      # don't check for rpc_methods, because there is no @supported_methods when initializing
      if method != 'rpc_methods' && !@supported_methods.include?(method)
        raise "Method `#{method}` is not supported. It should be in [#{@supported_methods.join(', ')}]."
      end

      http = Net::HTTP.new(@uri.host, @uri.port)
      http.use_ssl = @uri.scheme == 'https'

      request = Net::HTTP::Post.new(@uri, 'Content-Type' => 'application/json')
      request.body = { jsonrpc: '2.0', method: method, params: params, id: Time.now.to_i }.to_json
      ScaleRb.logger.debug "Request: #{request.body}"

      # https://docs.ruby-lang.org/en/master/Net/HTTPResponse.html
      response = http.request(request)
      raise response unless response.is_a?(Net::HTTPOK)

      # parse response, make key symbol
      body = JSON.parse(response.body, symbolize_names: true)
      ScaleRb.logger.debug "Response: #{body}"
      raise body[:error] if body[:error]

      body[:result]
    end

    def respond_to_missing?(*_args)
      true
    end

    def method_missing(method, *args)
      # ScaleRb.logger.debug "#{method}(#{args.join(', ')})"

      request(method.to_s, args)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
scale_rb-0.4.1 lib/client/http_client.rb
scale_rb-0.4.0 lib/client/http_client.rb