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 collapse

VERSION =
"1.3.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.keyObject (readonly)

Returns the value of attribute key.



23
24
25
# File 'lib/km.rb', line 23

def key
  @key
end

Class Method Details

.alias(name, alias_to) ⇒ Object



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

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

.hostObject



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

def host
  @host
end

.identify(id) ⇒ Object



56
57
58
# File 'lib/km.rb', line 56

def identify(id)
  @id = id
end

.init(key, options = {}) ⇒ Object



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

def init(key, options={})
  default = {
    :host      => @host,
    :log_dir   => @log_dir,
    :to_stderr => @to_stderr,
    :use_cron  => @use_cron,
    :dryrun    => @dryrun,
    :env       => set_env,
  }
  options = default.merge(options)

  begin
    @key       = key
    @host      = options[:host]
    @log_dir   = options[:log_dir]
    @use_cron  = options[:use_cron]
    @to_stderr = options[:to_stderr]
    @dryrun    = options[:dryrun]
    @env       = options[:env]
    log_dir_writable?
  rescue Exception => e
    log_error(e)
  end
end

.log_dirObject



115
116
117
# File 'lib/km.rb', line 115

def log_dir
  @log_dir
end

.record(action, props = {}) ⇒ Object



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

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

.send_logged_queriesObject

:nodoc:



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

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

.set(data) ⇒ Object



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

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

.set_envObject



50
51
52
53
54
# File 'lib/km.rb', line 50

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