metric 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/metric.rb +6 -4
- data/lib/metric/configuration.rb +1 -1
- data/lib/metric/track.rb +9 -9
- data/lib/metric/version.rb +1 -1
- data/spec/metric/configuration_spec.rb +3 -3
- data/spec/metric/receive_spec.rb +19 -17
- data/spec/metric/track_spec.rb +23 -12
- metadata +85 -54
data/lib/metric.rb
CHANGED
@@ -18,16 +18,18 @@ module Metric
|
|
18
18
|
|
19
19
|
# Tracks metrics
|
20
20
|
#
|
21
|
-
# @param [String
|
22
|
-
# @
|
21
|
+
# @param [String] metric Metric identifier
|
22
|
+
# @param [Hash] options Options
|
23
|
+
# @return [Hash] API Response
|
23
24
|
def track(metric, options = {})
|
24
25
|
Metric::Track.track(metric, options)
|
25
26
|
end
|
26
27
|
|
27
28
|
# Fetches data from the API
|
28
29
|
#
|
29
|
-
# @param [String
|
30
|
-
# @
|
30
|
+
# @param [String] metric Metric identifier
|
31
|
+
# @param [String] range Range identifier, either total, today, week or month
|
32
|
+
# @return [Hash] API Response
|
31
33
|
def receive(metric, range)
|
32
34
|
Metric::Receive.receive(metric, range)
|
33
35
|
end
|
data/lib/metric/configuration.rb
CHANGED
data/lib/metric/track.rb
CHANGED
@@ -8,17 +8,23 @@ module Metric
|
|
8
8
|
# @param [String] metric Metric identifier
|
9
9
|
# @param [Hash] options Options
|
10
10
|
# @option options [Symbol] :amount Amount to track
|
11
|
+
# @option options [Symbol] :date Override the default date (today)
|
12
|
+
# @option options [Symbol] :meta Pass in custom meta data about the metric
|
11
13
|
# @option options [Symbol] :trigger Flag for email notification
|
12
14
|
# @return [String]
|
13
15
|
def self.compose(metric, options = {})
|
14
16
|
amount = options[:amount]
|
15
17
|
trigger = options[:trigger]
|
18
|
+
date = options[:date]
|
19
|
+
meta = options[:meta]
|
16
20
|
|
17
21
|
key = "?api_key=" + Metric.configuration.api_key
|
18
22
|
url = Metric.configuration.metric_host + '/track'
|
19
23
|
url << key
|
20
|
-
url <<
|
24
|
+
url << "&metric=#{CGI.escape(metric)}"
|
21
25
|
url << "&amount=#{amount}" if amount
|
26
|
+
url << "&date=#{date}" if date
|
27
|
+
url << "&meta=#{CGI.escape(meta)}" if meta
|
22
28
|
url << "&trigger=1" if trigger
|
23
29
|
url
|
24
30
|
end
|
@@ -36,6 +42,8 @@ module Metric
|
|
36
42
|
end
|
37
43
|
end
|
38
44
|
|
45
|
+
private
|
46
|
+
|
39
47
|
# Check if Rails or Rack env is production, or amount is 0
|
40
48
|
def self.quit_early?(options)
|
41
49
|
return true if defined?(Rails) && !Rails.env.production?
|
@@ -43,13 +51,5 @@ module Metric
|
|
43
51
|
return true if options[:amount] && options[:amount] == 0
|
44
52
|
false
|
45
53
|
end
|
46
|
-
|
47
|
-
# CGI escape the metric name so spaces and characters are allowed
|
48
|
-
#
|
49
|
-
# @param [String]
|
50
|
-
# @return [String]
|
51
|
-
def self.parse_metric(metric)
|
52
|
-
"&metric=#{CGI.escape(metric)}"
|
53
|
-
end
|
54
54
|
end
|
55
55
|
end
|
data/lib/metric/version.rb
CHANGED
@@ -9,14 +9,14 @@ describe Metric::Configuration do
|
|
9
9
|
Metric.configure do |config|
|
10
10
|
config.api_key = "test"
|
11
11
|
end
|
12
|
-
Metric.configuration.metric_host.should == "
|
12
|
+
Metric.configuration.metric_host.should == "https://api.metric.io"
|
13
13
|
end
|
14
14
|
|
15
15
|
it "configures metric host" do
|
16
16
|
Metric.configure do |config|
|
17
|
-
config.metric_host = "
|
17
|
+
config.metric_host = "https://localhost:5000"
|
18
18
|
end
|
19
|
-
Metric.configuration.metric_host.should == "
|
19
|
+
Metric.configuration.metric_host.should == "https://localhost:5000"
|
20
20
|
end
|
21
21
|
|
22
22
|
it "configures api_key" do
|
data/spec/metric/receive_spec.rb
CHANGED
@@ -5,30 +5,32 @@ describe Metric::Receive do
|
|
5
5
|
Metric::Receive.generate_token("hits").should == "c6daa87bcf8bf7cb4d1c74d872793e5e"
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
context "generating correct url" do
|
9
|
+
it "total" do
|
10
|
+
request = "https://api.metric.io/receive?api_key=spec&token=c6daa87bcf8bf7cb4d1c74d872793e5e&metric=hits&range=total"
|
11
|
+
Metric::Receive.compose("hits", "total").should == request
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
it "today" do
|
15
|
+
request = "https://api.metric.io/receive?api_key=spec&token=c6daa87bcf8bf7cb4d1c74d872793e5e&metric=hits&range=today"
|
16
|
+
Metric::Receive.compose("hits", "today").should == request
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
it "week" do
|
20
|
+
request = "https://api.metric.io/receive?api_key=spec&token=c6daa87bcf8bf7cb4d1c74d872793e5e&metric=hits&range=week"
|
21
|
+
Metric::Receive.compose("hits", "week").should == request
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
it "month" do
|
25
|
+
request = "https://api.metric.io/receive?api_key=spec&token=c6daa87bcf8bf7cb4d1c74d872793e5e&metric=hits&range=month"
|
26
|
+
Metric::Receive.compose("hits", "month").should == request
|
27
|
+
end
|
26
28
|
end
|
27
29
|
|
28
30
|
it "grabs actual data" do
|
29
|
-
stub_request(:get, "
|
31
|
+
stub_request(:get, "https://api.metric.io/receive?api_key=spec&metric=hits&range=total&token=c6daa87bcf8bf7cb4d1c74d872793e5e").
|
30
32
|
to_return(:status => 200, :body => "{\"total\":\"1\"}", :headers => {})
|
31
|
-
Metric::Receive.receive("hits", "
|
33
|
+
Metric::Receive.receive("hits", "total").should == {"total" => "1"}
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
data/spec/metric/track_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Metric::Track do
|
4
4
|
it "composes the request url" do
|
5
|
-
Metric::Track.compose("hits").should == "
|
5
|
+
Metric::Track.compose("hits").should == "https://api.metric.io/track?api_key=spec&metric=hits"
|
6
6
|
end
|
7
7
|
|
8
8
|
it "gets correct url when tracking" do
|
@@ -11,23 +11,34 @@ describe Metric::Track do
|
|
11
11
|
Metric::Track.track("hits")
|
12
12
|
end
|
13
13
|
|
14
|
-
it "encodes the
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
it "sends trigger param" do
|
19
|
-
url = "http://api.metric.io/track?api_key=spec&metric=hits&trigger=1"
|
20
|
-
Metric::Track.compose("hits", {:trigger => true}).should == url
|
14
|
+
it "encodes the input" do
|
15
|
+
url = "https://api.metric.io/track?api_key=spec&metric=hits+and+spaces"
|
16
|
+
Metric::Track.compose("hits and spaces").should == url
|
21
17
|
end
|
22
18
|
|
23
19
|
it "sends custom amount" do
|
24
|
-
url = "
|
25
|
-
Metric::Track.compose("hits",
|
20
|
+
url = "https://api.metric.io/track?api_key=spec&metric=hits&amount=42"
|
21
|
+
Metric::Track.compose("hits", :amount => 42).should == url
|
26
22
|
end
|
27
23
|
|
28
24
|
it "does nothing if amount is 0" do
|
29
|
-
Metric::Track.
|
25
|
+
Metric::Track.should_not_receive(:compose)
|
26
|
+
Metric::Track.track("hits", :amount => 0)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "passes in custom date" do
|
30
|
+
url = "https://api.metric.io/track?api_key=spec&metric=hits&date=20120101"
|
31
|
+
Metric::Track.compose("hits", :date => "20120101").should == url
|
30
32
|
end
|
31
|
-
end
|
32
33
|
|
34
|
+
it "passes in meta information" do
|
35
|
+
url = "https://api.metric.io/track?api_key=spec&metric=payment&meta=userid%3A+1"
|
36
|
+
Metric::Track.compose("payment", :meta => "userid: 1").should == url
|
37
|
+
end
|
38
|
+
|
39
|
+
it "sends trigger param" do
|
40
|
+
url = "https://api.metric.io/track?api_key=spec&metric=hits&trigger=1"
|
41
|
+
Metric::Track.compose("hits", :trigger => true).should == url
|
42
|
+
end
|
43
|
+
end
|
33
44
|
|
metadata
CHANGED
@@ -1,72 +1,93 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: metric
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 8
|
10
|
+
version: 0.0.8
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Mark Mulder
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2012-02-22 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
15
22
|
name: faraday
|
16
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
17
25
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
22
33
|
type: :runtime
|
23
|
-
|
24
|
-
|
25
|
-
- !ruby/object:Gem::Dependency
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
26
36
|
name: multi_json
|
27
|
-
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
|
-
requirements:
|
30
|
-
- -
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
33
47
|
type: :runtime
|
34
|
-
|
35
|
-
|
36
|
-
- !ruby/object:Gem::Dependency
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
37
50
|
name: rspec
|
38
|
-
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
39
53
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
44
61
|
type: :development
|
45
|
-
|
46
|
-
|
47
|
-
- !ruby/object:Gem::Dependency
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
48
64
|
name: webmock
|
49
|
-
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
50
67
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
55
75
|
type: :development
|
56
|
-
|
57
|
-
version_requirements: *2161465040
|
76
|
+
version_requirements: *id004
|
58
77
|
description: Track metrics via metric.io
|
59
|
-
email:
|
78
|
+
email:
|
60
79
|
- markmulder@gmail.com
|
61
80
|
executables: []
|
81
|
+
|
62
82
|
extensions: []
|
83
|
+
|
63
84
|
extra_rdoc_files: []
|
64
|
-
|
85
|
+
|
86
|
+
files:
|
65
87
|
- .gitignore
|
66
88
|
- .rspec
|
67
89
|
- .travis.yml
|
68
90
|
- Gemfile
|
69
|
-
- Gemfile.lock
|
70
91
|
- README.markdown
|
71
92
|
- Rakefile
|
72
93
|
- lib/metric.rb
|
@@ -80,31 +101,41 @@ files:
|
|
80
101
|
- spec/metric/track_spec.rb
|
81
102
|
- spec/metric_spec.rb
|
82
103
|
- spec/spec_helper.rb
|
104
|
+
has_rdoc: true
|
83
105
|
homepage: http://github.com/bittersweet/metric
|
84
106
|
licenses: []
|
107
|
+
|
85
108
|
post_install_message:
|
86
109
|
rdoc_options: []
|
87
|
-
|
110
|
+
|
111
|
+
require_paths:
|
88
112
|
- lib
|
89
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
114
|
none: false
|
91
|
-
requirements:
|
92
|
-
- -
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
|
95
|
-
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
hash: 3
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
version: "0"
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
123
|
none: false
|
97
|
-
requirements:
|
98
|
-
- -
|
99
|
-
- !ruby/object:Gem::Version
|
100
|
-
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
128
|
+
segments:
|
129
|
+
- 0
|
130
|
+
version: "0"
|
101
131
|
requirements: []
|
132
|
+
|
102
133
|
rubyforge_project:
|
103
|
-
rubygems_version: 1.
|
134
|
+
rubygems_version: 1.4.2
|
104
135
|
signing_key:
|
105
136
|
specification_version: 3
|
106
137
|
summary: Companion gem to the metric.io site to track metrics
|
107
|
-
test_files:
|
138
|
+
test_files:
|
108
139
|
- spec/metric/configuration_spec.rb
|
109
140
|
- spec/metric/receive_spec.rb
|
110
141
|
- spec/metric/track_spec.rb
|