Sha256: 97cb1e0435c8b78f8c24af1964bfce515ac9f359b9a70317e3afe4fe90f3a8a6

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module Metric

  # Used to track metrics

  class Track
    # Generate the url with query strings
    #
    # @param [String] metric Metric identifier
    # @param [Hash] options Options
    # @option options [Symbol] :amount Amount to track
    # @option options [Symbol] :trigger Flag for email notification
    # @return [String]
    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

    # Track the metric
    # @note curl is called in a seperate thread so this won't block execution
    #
    # @param [String, Hash]
    # @return [nil]
    def self.track(metric, options = {})
      return if quit_early?(options)
      url = compose(metric, options)
      Thread.new do
        `curl "#{url}" 2>&1 ; `
      end
    end

    # Check if Rails or Rack env is production, or amount is 0
    def self.quit_early?(options)
      return true if defined?(Rails) && !Rails.env.production?
      return true if ENV['RACK_ENV'] && ENV['RACK_ENV'] != "production"
      return true if options[:amount] && options[:amount] == 0
      false
    end

    # CGI escape the metric name so spaces and characters are allowed
    #
    # @param [String]
    # @return [String]
    def self.parse_metric(metric)
      "&metric=#{CGI.escape(metric)}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metric-0.0.7 lib/metric/track.rb