Class: Analytico::Client

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

Constant Summary collapse

@@connection =
nil

Class Method Summary collapse

Class Method Details

.add_impression(*args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/analytico/client.rb', line 37

def add_impression(*args)
  raise NoConnectionEstablished  if @@connection.nil?
  
  options = extract_options!(args)
  if options[:fqdn].nil? || options[:ip].nil?
    return true
  end
  params = Hash.new
  params[:impression] = options
  params[:impression][:api_key] = @@connection.api_key
  
  response = post(Endpoint.impressions, params)
  HashUtils.recursively_symbolize_keys(response)
end

.add_metric(fqdn, name, value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/analytico/client.rb', line 52

def add_metric(fqdn, name, value)
  raise NoConnectionEstablished  if @@connection.nil?
  
  params = Hash.new
  params[:metric] = Hash.new
  params[:metric][:api_key] = @@connection.api_key
  params[:metric][:fqdn] = fqdn
  params[:metric][:name] = name
  params[:metric][:value] = value
  
  response = post(Endpoint.metrics, params)
  HashUtils.recursively_symbolize_keys(response)
end

.asyncObject



33
34
35
# File 'lib/analytico/client.rb', line 33

def async
  @@async
end

.async=(async_flag) ⇒ Object



28
29
30
31
# File 'lib/analytico/client.rb', line 28

def async=(async_flag)
  @@async = async_flag
  @@connection.async = @@async if @@connection
end

.debugObject



24
25
26
# File 'lib/analytico/client.rb', line 24

def debug
  @@debug
end

.debug=(debug_flag) ⇒ Object



19
20
21
22
# File 'lib/analytico/client.rb', line 19

def debug=(debug_flag)
  @@debug = debug_flag
  @@connection.debug = @@debug if @@connection
end

.post(endpoint, data = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/analytico/client.rb', line 66

def post(endpoint, data=nil)
  unless production_env?
    Rails.logger.warn "%%% Analytico %%% Skipping while not in production. Would have sent (async: #{@@async ? 'on' : 'off' }): #{data.inspect}"
    return nil
  end
  if @@async
    self.send_later :transmit, endpoint, data
  else
    transmit endpoint, data
  end
end

.set_credentials(api_key) ⇒ Object



13
14
15
16
17
# File 'lib/analytico/client.rb', line 13

def set_credentials(api_key)
  @@connection = Connection.new(api_key)
  self.debug = @@debug
  true
end

.transmit(endpoint, data) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/analytico/client.rb', line 78

def transmit(endpoint, data)
  begin
    @@connection.post(endpoint, data)
  rescue
    Rails.logger.warn "%%% Analytico %%% There was an error communicating with Analytico"
  end
end