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

.compose(metric, options = {}) ⇒ String

Generate the url with query strings

Parameters:

  • metric (String)

    Metric identifier

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

    Options

Options Hash (options):

  • :amount (Symbol)

    Amount to track

  • :date (Symbol)

    Override the default date (today)

  • :meta (Symbol)

    Pass in custom meta data about the metric

  • :trigger (Symbol)

    Flag for email notification

Returns:

  • (String)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/metric/track.rb', line 15

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

  key = "?api_key=" + Metric.configuration.api_key
  url = Metric.configuration.protocol + "://" + Metric.configuration.host + '/track'
  url << key
  url << "&metric=#{CGI.escape(metric)}"
  url << "&amount=#{amount}" if amount
  url << "&date=#{date}" if date
  url << "&meta=#{CGI.escape(meta)}" if meta
  url << "&trigger=1" if trigger
  url
end

.track(metric, options = {}) ⇒ nil

Note:

curl is called in a seperate thread so this won’t block execution

Track the metric

Parameters:

  • (String, Hash)

Returns:

  • (nil)


37
38
39
40
41
42
43
# File 'lib/metric/track.rb', line 37

def self.track(metric, options = {})
  return if quit_early?(options)
  url = compose(metric, options)
  Thread.new do
    `curl "#{url}" 2>&1 ; `
  end
end