Class: Metric::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/metric/track.rb

Overview

Used to track metrics

Class Method Summary (collapse)

Class Method Details

+ (String) compose(metric, options = {})

Generate the url with query strings

Parameters:

  • metric (String)

    Metric identifier

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :amount (Symbol)

    Amount to track

  • :trigger (Symbol)

    Flag for email notification

Returns:

  • (String)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metric/track.rb', line 13

def self.compose(metric, options = {})
  amount = options[:amount]
  trigger = options[:trigger]

  key = "?api_key=" + Metric.configuration.api_key
  url = Metric.configuration.metric_host + '/track'
  url << key
  url << parse_metric(metric)
  url << "&amount=#{amount}" if amount
  url << "&trigger=1" if trigger
  url
end

+ (String) parse_metric(metric)

CGI escape the metric name so spaces and characters are allowed

Parameters:

  • (String)

Returns:

  • (String)


45
46
47
# File 'lib/metric/track.rb', line 45

def self.parse_metric(metric)
  "&metric=#{CGI.escape(metric)}"
end

+ (nil) track(metric, options = {})

Note:

If this gem is used with Rails it will only track data in the production environment

Uses a thread to perform the request

Parameters:

  • (String, Hash)

Returns:

  • (nil)


31
32
33
34
35
36
37
38
39
# File 'lib/metric/track.rb', line 31

def self.track(metric, options = {})
  return if defined?(Rails) && !Rails.env.production?
  return if options[:amount] && options[:amount] == 0

  url = compose(metric, options)
  Thread.new do
    `curl "#{url}" 2>&1 ; `
  end
end