Class: KM

Inherits:
Object
  • Object
show all
Defined in:
lib/km.rb,
lib/km/saas.rb,
lib/km/version.rb

Defined Under Namespace

Modules: SaaS Classes: IdentError, InitError

Constant Summary

VERSION =
"1.1.0"

Class Method Summary (collapse)

Class Method Details

+ (Object) alias(name, alias_to)



75
76
77
78
79
80
81
82
# File 'lib/km.rb', line 75

def alias(name, alias_to)
  begin
    return unless is_initialized?
    generate_query('a', { '_n' => alias_to, '_p' => name }, false)
  rescue Exception => e
    log_error(e)
  end
end

+ (Object) host



120
121
122
# File 'lib/km.rb', line 120

def host
  @host
end

+ (Object) identify(id)



58
59
60
# File 'lib/km.rb', line 58

def identify(id)
  @id = id
end

+ (Object) init(key, options = {})



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/km.rb', line 30

def init(key, options={})
  default = {
    :host      => @host,
    :log_dir   => @log_dir,
    :to_stderr => @to_stderr,
    :use_cron  => @use_cron,
    :env       => set_env,
  }
  options.reverse_merge!(default)
  begin
    @key       = key
    @host      = options[:host]
    @log_dir   = options[:log_dir]
    @use_cron  = options[:use_cron]
    @to_stderr = options[:to_stderr]
    @env       = options[:env]
    log_dir_writable?
  rescue Exception => e
    log_error(e)
  end
end

+ (Object) log_dir



117
118
119
# File 'lib/km.rb', line 117

def log_dir
  @log_dir
end

+ (Object) record(action, props = {})



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/km.rb', line 62

def record(action,props={})
  props = hash_keys_to_str(props)
  begin
    return unless is_initialized_and_identified?
    return set(action) if action.class == Hash

    props.update('_n' => action)
    generate_query('e', props)
  rescue Exception => e
    log_error(e)
  end
end

+ (Object) send_logged_queries

:nodoc:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/km.rb', line 93

def send_logged_queries # :nodoc:
  line = nil
  begin
    query_log = log_name(:query_old)
    query_log = log_name(:query) unless File.exists?(query_log)
    return unless File.exists?(query_log) # can't find logfile to send
    FileUtils.move(query_log, log_name(:send))
    File.open(log_name(:send)) do |fh|
      while not fh.eof?
        begin
          line = fh.readline.chomp
          send_query(line)
        rescue Exception => e
          log_query(line) if line
          log_error(e)
        end
      end
    end
    FileUtils.rm(log_name(:send))
  rescue Exception => e
    log_error(e)
  end
end

+ (Object) set(data)



84
85
86
87
88
89
90
91
# File 'lib/km.rb', line 84

def set(data)
  begin
    return unless is_initialized_and_identified?
    generate_query('s', data)
  rescue Exception => e
    log_error(e)
  end
end

+ (Object) set_env



52
53
54
55
56
# File 'lib/km.rb', line 52

def set_env
  @env = Rails.env if defined? Rails
  @env ||= ENV['RACK_ENV']
  @env ||= 'production'
end