Class: EventQL::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/eventql_query.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, query_str, query_opts = {}) ⇒ Query

Returns a new instance of Query.



8
9
10
11
12
# File 'lib/eventql_query.rb', line 8

def initialize(client, query_str, query_opts = {})
  @client = client
  @query_str = query_str
  @query_opts = query_opts
end

Instance Method Details

#execute!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eventql_query.rb', line 14

def execute!
  request = Net::HTTP::Post.new("/api/v1/sql")
  request.add_field("Content-Type", "application/json")

  if @client.has_auth_token?
    request.add_field("Authorization", "Token #{@client.get_auth_token}")
  end

  request.body = {
    :query => @query_str,
    :database => @client.get_database,
    :format => "json"
  }.to_json

  http = Net::HTTP.new(@client.get_host, @client.get_port)
  response = http.request(request)

  response_json = JSON.parse(response.body) rescue nil
  if response_json && response_json.has_key?("error")
    raise response_json["error"]
  end

  if response_json.nil? || response.code.to_i != 200
    raise "HTTP ERROR (#{response.code}): #{response.body[0..128]}"
  end

  return response_json["results"]
end