class Apitool::Client::ApitoolClient def initialize(params = {}) @host ||= params[:host] @port ||= params[:port] @ssl ||= params[:ssl] || false @token ||= params[:token] @version ||= params[:version] || 'v1' @symbolize ||= params[:symbolize] || true default_ssl_verification = (@ssl) ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE @verify_ssl ||= params[:verify_ssl] || default_ssl_verification end def response @response.nil? ? nil : parse(@response) end def request @request.nil? ? nil : @request.args end def result @result.nil? ? nil : @result.code.to_i end protected def get_connection @client ||= RestClient::Resource.new( base_uri, verify_ssl: @verify_ssl ) end def get(path, options = {}) get_connection[request_uri(path)].get(headers) { |response, request, result, &block| @response = response @request = request @result = result yield response, request, result if block_given? } end def post(path, parameters) get_connection[request_uri(path)].post(parameters.to_json, headers) { |response, request, result, &block| @response = response @request = request @result = result yield response, request, result if block_given? } end def put(path, parameters) get_connection[request_uri(path)].put(parameters.to_json, headers) { |response, request, result, &block| @response = response @request = request @result = result yield response, request, result if block_given? } end def delete(path) get_connection[request_uri(path)].delete(headers) { |response, request, result, &block| @response = response @request = request @result = result yield response, request, result if block_given? } end def parse(data) JSON.parse(data, symbolize_names: @symbolize) end private def base_uri if @ssl "https://#{@host}:#{@port}" else "http://#{@host}:#{@port}" end end def request_uri(path) "/api/#{@version}#{path}" end def headers { content_type: :json, accept: :json, authorization: "Token token=#{@token}" } end end