class Keen::SavedQueries

Attributes

client[R]

Public Class Methods

new(client) click to toggle source
# File lib/keen/saved_queries.rb, line 6
def initialize(client)
  @client = client
end

Public Instance Methods

all() click to toggle source
# File lib/keen/saved_queries.rb, line 10
def all
  client.ensure_master_key!

  response = saved_query_response(client.master_key)
  client.process_response(response.code.to_i, response.body)
end
create(saved_query_name, saved_query_body) click to toggle source
# File lib/keen/saved_queries.rb, line 33
def create(saved_query_name, saved_query_body)
  client.ensure_master_key!

  response = Keen::HTTP::Sync.new(client.api_url, client.proxy_url, client.read_timeout, client.open_timeout).put(
    path: "#{saved_query_base_url}/#{saved_query_name}",
    headers: api_headers(client.master_key, "sync"),
    body: saved_query_body
  )
  client.process_response(response.code.to_i, response.body)
end
Also aliased as: update
delete(saved_query_name) click to toggle source
# File lib/keen/saved_queries.rb, line 45
def delete(saved_query_name)
  client.ensure_master_key!

  response = Keen::HTTP::Sync.new(client.api_url, client.proxy_url, client.read_timeout, client.open_timeout).delete(
    path: "#{saved_query_base_url}/#{saved_query_name}",
    headers: api_headers(client.master_key, "sync")
  )
  client.process_response(response.code.to_i, response.body)
end
get(saved_query_name, results = false) click to toggle source
# File lib/keen/saved_queries.rb, line 17
def get(saved_query_name, results = false)
  saved_query_path = "/#{saved_query_name}"
  if results
    client.ensure_read_key!
    saved_query_path += "/result"
    # The results path should use the READ KEY
    api_key = client.read_key
  else
    client.ensure_master_key!
    api_key = client.master_key
  end

  response = saved_query_response(api_key, saved_query_path)
  client.process_response(response.code.to_i, response.body)
end
update(saved_query_name, saved_query_body)
Alias for: create

Private Instance Methods

api_headers(authorization, sync_type) click to toggle source
# File lib/keen/saved_queries.rb, line 71
def api_headers(authorization, sync_type)
  user_agent = "keen-gem, v#{Keen::VERSION}, #{sync_type}"
  user_agent += ", #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
  if defined?(RUBY_ENGINE)
    user_agent += ", #{RUBY_ENGINE}"
  end
  { "Content-Type" => "application/json",
    "User-Agent" => user_agent,
    "Authorization" => authorization,
    "Keen-Sdk" => "ruby-#{Keen::VERSION}" }
end
saved_query_base_url() click to toggle source
# File lib/keen/saved_queries.rb, line 66
def saved_query_base_url
  client.ensure_project_id!
  "/#{client.api_version}/projects/#{client.project_id}/queries/saved"
end
saved_query_response(api_key, path = "") click to toggle source
# File lib/keen/saved_queries.rb, line 59
def saved_query_response(api_key, path = "")
  Keen::HTTP::Sync.new(client.api_url, client.proxy_url, client.read_timeout, client.open_timeout).get(
    path: saved_query_base_url + path,
    headers: api_headers(api_key, "sync")
  )
end