class Keen::Client

Constants

CONFIG

Attributes

api_url[RW]
log_queries[RW]
master_key[RW]
open_timeout[RW]
project_id[RW]
proxy_type[RW]
proxy_url[RW]
read_key[RW]
read_timeout[RW]
write_key[RW]

Public Class Methods

new(*args) click to toggle source
# File lib/keen/client.rb, line 37
def initialize(*args)
  options = args[0]
  unless options.is_a?(Hash)
    # deprecated, pass a hash of options instead
    options = {
      :project_id => args[0],
      :write_key => args[1],
      :read_key => args[2],
    }.merge(args[3] || {})
  end

  self.project_id, self.write_key, self.read_key, self.master_key = options.values_at(
    :project_id, :write_key, :read_key, :master_key)

  self.api_url = options[:api_url] || CONFIG[:api_url]

  self.proxy_url, self.proxy_type = options.values_at(:proxy_url, :proxy_type)

  self.read_timeout = options[:read_timeout].to_f unless options[:read_timeout].nil?

  self.open_timeout = options[:open_timeout].to_f unless options[:open_timeout].nil?
end

Public Instance Methods

access_keys() click to toggle source
# File lib/keen/client.rb, line 64
def access_keys
  @access_keys ||= AccessKeys.new(self)
end
ensure_master_key!() click to toggle source
# File lib/keen/client.rb, line 98
def ensure_master_key!
  raise ConfigurationError, "Master Key must be set for this operation" unless self.master_key
end
ensure_project_id!() click to toggle source
# File lib/keen/client.rb, line 90
def ensure_project_id!
  raise ConfigurationError, "Project ID must be set" unless self.project_id
end
ensure_read_key!() click to toggle source
# File lib/keen/client.rb, line 102
def ensure_read_key!
  raise ConfigurationError, "Read Key must be set for this operation" unless self.read_key
end
ensure_write_key!() click to toggle source
# File lib/keen/client.rb, line 94
def ensure_write_key!
  raise ConfigurationError, "Write Key must be set for this operation" unless self.write_key
end
process_response(status_code, response_body) click to toggle source
# File lib/keen/client.rb, line 68
def process_response(status_code, response_body)
  case status_code.to_i
  when 200..201
    begin
      return MultiJson.decode(response_body)
    rescue
      Keen.logger.warn("Invalid JSON for response code #{status_code}: #{response_body}")
      return {}
    end
  when 204
    return true
  when 400
    raise BadRequestError.new(response_body)
  when 401
    raise AuthenticationError.new(response_body)
  when 404
    raise NotFoundError.new(response_body)
  else
    raise HttpError.new(response_body)
  end
end
saved_queries() click to toggle source
# File lib/keen/client.rb, line 60
def saved_queries
  @saved_queries ||= SavedQueries.new(self)
end

Private Instance Methods

api_event_collection_resource_path(event_collection) click to toggle source
# File lib/keen/client.rb, line 108
def api_event_collection_resource_path(event_collection)
  encoded_collection_name = Addressable::URI.encode_component(event_collection.to_s)
  encoded_collection_name.gsub! '/', '%2F'
  "/#{api_version}/projects/#{project_id}/events/#{encoded_collection_name}"
end
method_missing(_method, *args, &block) click to toggle source
Calls superclass method
# File lib/keen/client.rb, line 171
def method_missing(_method, *args, &block)
  if config = CONFIG[_method.to_sym]
    if config.is_a?(Proc)
      config.call(*args)
    else
      config
    end
  else
    super
  end
end
preprocess_encodables(params) click to toggle source
# File lib/keen/client.rb, line 127
def preprocess_encodables(params)
  [:filters, :steps, :analyses].each do |key|
    if params.key?(key)
      params[key] = MultiJson.encode(params[key])
    end
  end
end
preprocess_group_by(params) click to toggle source
# File lib/keen/client.rb, line 142
def preprocess_group_by(params)
  group_by = params[:group_by]
  if group_by.is_a?(Array)
    params[:group_by] = MultiJson.encode(group_by)
  end
end
preprocess_max_age(params) click to toggle source
# File lib/keen/client.rb, line 149
def preprocess_max_age(params)
  max_age = params[:max_age]
  if max_age.is_a? Numeric
    params[:max_age] = params[:max_age].to_s
  else
    params.delete(:max_age)
  end
end
preprocess_params(params) click to toggle source
# File lib/keen/client.rb, line 114
def preprocess_params(params)
  params = params.delete_if {|key, val| val.nil?}

  preprocess_encodables(params)
  preprocess_timeframe(params)
  preprocess_max_age(params)
  preprocess_group_by(params)
  preprocess_percentile(params)
  preprocess_property_names(params)

  params.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join('&')
end
preprocess_percentile(params) click to toggle source
# File lib/keen/client.rb, line 158
def preprocess_percentile(params)
  if params.key?(:percentile)
    params[:percentile] = params[:percentile].to_s
  end
end
preprocess_property_names(params) click to toggle source
# File lib/keen/client.rb, line 164
def preprocess_property_names(params)
  property_names = params[:property_names]
  if property_names.is_a?(Array)
    params[:property_names] = MultiJson.encode(property_names)
  end
end
preprocess_timeframe(params) click to toggle source
# File lib/keen/client.rb, line 135
def preprocess_timeframe(params)
  timeframe = params[:timeframe]
  if timeframe.is_a?(Hash)
    params[:timeframe] = MultiJson.encode(timeframe)
  end
end