class DruidDB::Connection

Constants

CONTENT_TYPE
VERB_MAP

Attributes

http[R]

Public Class Methods

new(endpoint) click to toggle source
# File lib/druiddb/connection.rb, line 16
def initialize(endpoint)
  if endpoint.is_a? String
    uri = URI.parse(endpoint)
    host = uri.host
    port = uri.port
  else
    host, port = endpoint.values_at(:host, :port)
  end

  @http = ::Net::HTTP.new(host, port)
end

Public Instance Methods

delete(path, params = {}) click to toggle source
# File lib/druiddb/connection.rb, line 40
def delete(path, params = {})
  request :delete, path, params
end
get(path, params = {}) click to toggle source
# File lib/druiddb/connection.rb, line 28
def get(path, params = {})
  request :get, path, params
end
post(path, params = {}) click to toggle source
# File lib/druiddb/connection.rb, line 32
def post(path, params = {})
  request :post, path, params
end
put(path, params = {}) click to toggle source
# File lib/druiddb/connection.rb, line 36
def put(path, params = {})
  request :put, path, params
end

Private Instance Methods

encode_path_params(path, params) click to toggle source
# File lib/druiddb/connection.rb, line 46
def encode_path_params(path, params)
  encoded = URI.encode_www_form(params)
  [path, encoded].join('?')
end
request(method, path, params) click to toggle source
# File lib/druiddb/connection.rb, line 51
def request(method, path, params)
  case method
  when :get
    full_path = encode_path_params(path, params)
    request = VERB_MAP[method].new(full_path)
  else
    request = VERB_MAP[method].new(path)
    request.body = params.to_json
  end

  request.content_type = CONTENT_TYPE
  begin
    response = http.request(request)
  rescue Timeout::Error, *DruidDB::NET_HTTP_EXCEPTIONS => e
    raise ConnectionError, e.message
  end

  response
end