metric 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
1
  require 'metric/configuration'
2
+ require 'metric/util'
2
3
  require 'metric/track'
3
4
  require 'metric/receive'
5
+ require 'metric/rails'
4
6
  require 'cgi'
5
7
 
6
8
  module Metric
@@ -0,0 +1,13 @@
1
+ require 'metric/rails/controller_methods'
2
+
3
+ module Metric
4
+ module Rails
5
+ def self.initialize
6
+ if defined?(ActionController::Base)
7
+ ActionController::Base.send(:include, Metric::Rails::ControllerMethods)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ Metric::Rails.initialize
@@ -0,0 +1,18 @@
1
+ module Metric
2
+ module Rails
3
+ module ControllerMethods
4
+ def metric_current_user
5
+ user = current_user
6
+ return {} if current_user.nil?
7
+
8
+ parameters = {}
9
+ [:id, :name, :username, :email].each do |attribute|
10
+ parameters[attribute.to_s] = user.send(attribute) if user.respond_to?(attribute)
11
+ end
12
+ parameters
13
+ rescue NoMethodError, NameError
14
+ {}
15
+ end
16
+ end
17
+ end
18
+ end
@@ -21,12 +21,13 @@ module Metric
21
21
  # @param [String] range Range identifier, either total, today, week or month
22
22
  # @return [String]
23
23
  def self.compose(metric, range)
24
+ token = generate_token(metric, range)
25
+ parameters = {"metric" => metric, "range" => range, "token" => token}
24
26
  api_key = Metric.configuration.api_key
25
27
  url = Metric.configuration.protocol + "://" + Metric.configuration.host
26
- url << "/v1/sites/#{api_key}/statistics"
27
- url << parse_metric(metric)
28
- url << "&range=" + range
29
- url << "&token=" + generate_token(metric, range)
28
+ url << "/v1/sites/#{api_key}/statistics?"
29
+ url << Metric::Util.build_query_string(parameters)
30
+ url
30
31
  end
31
32
 
32
33
  # Returns and memoizes a Faraday connection
@@ -48,14 +49,6 @@ module Metric
48
49
  response = connection.get(url)
49
50
  MultiJson.decode(response.body)[range]
50
51
  end
51
-
52
- # CGI escape the metric name so spaces and characters are allowed
53
- #
54
- # @param [String] metric Metric identifier
55
- # @return [String]
56
- def self.parse_metric(metric)
57
- "?metric=#{CGI.escape(metric)}"
58
- end
59
52
  end
60
53
  end
61
54
 
@@ -10,22 +10,20 @@ module Metric
10
10
  # @option options [Symbol] :amount Amount to track
11
11
  # @option options [Symbol] :date Override the default date (today)
12
12
  # @option options [Symbol] :meta Pass in custom meta data about the metric
13
- # @option options [Symbol] :trigger Flag for email notification
14
13
  # @return [String]
15
14
  def self.compose(metric, options = {})
16
15
  amount = options[:amount]
17
- trigger = options[:trigger]
18
16
  date = options[:date]
19
17
  meta = options[:meta]
18
+ customer = options[:customer]
19
+
20
+ parameters = {"metric" => metric, "amount" => amount, "date" => date,
21
+ "meta" => meta, "customer" => customer}
20
22
 
21
23
  api_key = Metric.configuration.api_key
22
24
  url = Metric.configuration.protocol + "://" + Metric.configuration.host
23
- url << "/v1/sites/#{api_key}/track"
24
- url << "?metric=#{CGI.escape(metric)}"
25
- url << "&amount=#{amount}" if amount
26
- url << "&date=#{date}" if date
27
- url << "&meta=#{CGI.escape(meta)}" if meta
28
- url << "&trigger=1" if trigger
25
+ url << "/v1/sites/#{api_key}/track?"
26
+ url << Metric::Util.build_query_string(parameters)
29
27
  url
30
28
  end
31
29
 
@@ -46,7 +44,7 @@ module Metric
46
44
 
47
45
  # Check if Rails or Rack env is production, or amount is 0
48
46
  def self.quit_early?(options)
49
- return true if defined?(Rails) && !Rails.env.production?
47
+ return true if defined?(::Rails) && !::Rails.env.production?
50
48
  return true if ENV['RACK_ENV'] && ENV['RACK_ENV'] != "production"
51
49
  return true if options[:amount] && options[:amount] == 0
52
50
  false
@@ -0,0 +1,21 @@
1
+ module Metric
2
+ class Util
3
+ def self.escape(string)
4
+ URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
5
+ end
6
+
7
+ def self.build_query_string(object, prefix = nil)
8
+ string = []
9
+ object.each_pair do |key, value|
10
+ k = prefix ? "#{prefix}[#{key}]" : key
11
+ next if value.nil?
12
+ if value.is_a?(Hash)
13
+ string << build_query_string(value, k)
14
+ else
15
+ string << "#{escape(k)}=#{escape(value.to_s)}"
16
+ end
17
+ end
18
+ string.join("&")
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Metric
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -19,7 +19,7 @@ describe Metric::Track do
19
19
  end
20
20
 
21
21
  it "encodes the input" do
22
- result = "https://api.metric.io/v1/sites/spec/track?metric=hits+and+spaces"
22
+ result = "https://api.metric.io/v1/sites/spec/track?metric=hits%20and%20spaces"
23
23
  Metric::Track.compose("hits and spaces").should == result
24
24
  end
25
25
 
@@ -39,13 +39,8 @@ describe Metric::Track do
39
39
  end
40
40
 
41
41
  it "passes in meta information" do
42
- result = "https://api.metric.io/v1/sites/spec/track?metric=payment&meta=userid%3A+1"
42
+ result = "https://api.metric.io/v1/sites/spec/track?metric=payment&meta=userid%3A%201"
43
43
  Metric::Track.compose("payment", :meta => "userid: 1").should == result
44
44
  end
45
-
46
- it "sends trigger param" do
47
- result = "https://api.metric.io/v1/sites/spec/track?metric=hits&trigger=1"
48
- Metric::Track.compose("hits", :trigger => true).should == result
49
- end
50
45
  end
51
46
 
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metric::Util do
4
+ it 'builds a nested query string' do
5
+ object = {"amount" => 1, "customer" => {"id" => "1"}}
6
+ Metric::Util.build_query_string(object).should == "amount=1&customer%5Bid%5D=1"
7
+ end
8
+
9
+ it 'skips nil values' do
10
+ object = {"amount" => 1, "date" => nil}
11
+ Metric::Util.build_query_string(object).should == "amount=1"
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-03 00:00:00.000000000 Z
12
+ date: 2012-12-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -90,13 +90,17 @@ files:
90
90
  - Rakefile
91
91
  - lib/metric.rb
92
92
  - lib/metric/configuration.rb
93
+ - lib/metric/rails.rb
94
+ - lib/metric/rails/controller_methods.rb
93
95
  - lib/metric/receive.rb
94
96
  - lib/metric/track.rb
97
+ - lib/metric/util.rb
95
98
  - lib/metric/version.rb
96
99
  - metric.gemspec
97
100
  - spec/metric/configuration_spec.rb
98
101
  - spec/metric/receive_spec.rb
99
102
  - spec/metric/track_spec.rb
103
+ - spec/metric/util_spec.rb
100
104
  - spec/metric_spec.rb
101
105
  - spec/spec_helper.rb
102
106
  homepage: http://github.com/bittersweet/metric
@@ -127,5 +131,6 @@ test_files:
127
131
  - spec/metric/configuration_spec.rb
128
132
  - spec/metric/receive_spec.rb
129
133
  - spec/metric/track_spec.rb
134
+ - spec/metric/util_spec.rb
130
135
  - spec/metric_spec.rb
131
136
  - spec/spec_helper.rb