Skip to content

Commit

Permalink
Allow passing in meta option
Browse files Browse the repository at this point in the history
This way you can add custom data which will show up in
the event view on metric.io.
  • Loading branch information
bittersweet committed Feb 1, 2012
1 parent 695454a commit 4f0e5e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
13 changes: 8 additions & 5 deletions lib/metric/track.rb
Expand Up @@ -8,20 +8,23 @@ class Track
# @param [String] metric Metric identifier
# @param [Hash] options Options
# @option options [Symbol] :amount Amount to track
# @option options [Symbol] :trigger Flag for email notification
# @option options [Symbol] :date Override the default date (today)
# @option options [Symbol] :meta Pass in custom meta data about the metric
# @option options [Symbol] :trigger Flag for email notification
# @return [String]
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.metric_host + '/track'
url << key
url << parse_metric(metric)
url << "&metric=#{escape(metric)}"
url << "&amount=#{amount}" if amount
url << "&date=#{date}" if date
url << "&meta=#{escape(meta)}" if meta
url << "&trigger=1" if trigger
url
end
Expand All @@ -39,12 +42,12 @@ def self.track(metric, options = {})
end
end

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

private
Expand Down
18 changes: 14 additions & 4 deletions spec/metric/track_spec.rb
Expand Up @@ -11,8 +11,8 @@
Metric::Track.track("hits")
end

it "encodes the request url" do
Metric::Track.parse_metric("hits and spaces").should == "&metric=hits+and+spaces"
it "encodes the input" do
Metric::Track.escape("hits and spaces").should == "hits+and+spaces"
end

it "sends trigger param" do
Expand All @@ -26,13 +26,23 @@
end

it "does nothing if amount is 0" do
Metric::Track.track("hits", {:amount => 0}).should == nil
Metric::Track.should_not_receive(:compose)
Metric::Track.track("hits", {:amount => 0})
end

it "passes in custom date" do
url = "https://api.metric.io/track?api_key=spec&metric=hits&date=20120101"
Metric::Track.compose("hits", {:date => "20120101"}).should == url
end
end

it "passes in meta information" do
url = "https://api.metric.io/track?api_key=spec&metric=payment&meta=userid%3A+1"
Metric::Track.compose("payment", {:meta => "userid: 1"}).should == url
end

it "sends trigger param" do
url = "https://api.metric.io/track?api_key=spec&metric=hits&trigger=1"
Metric::Track.compose("hits", {:trigger => true}).should == url
end
end

0 comments on commit 4f0e5e3

Please sign in to comment.