metric 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -3
- data/README.markdown +5 -1
- data/lib/metric.rb +17 -22
- data/lib/metric/configuration.rb +11 -1
- data/lib/metric/receive.rb +61 -0
- data/lib/metric/track.rb +49 -0
- data/lib/metric/version.rb +1 -1
- data/metric.gemspec +3 -0
- data/spec/metric/configuration_spec.rb +10 -3
- data/spec/metric/receive_spec.rb +41 -0
- data/spec/metric/track_spec.rb +36 -0
- data/spec/metric_spec.rb +5 -18
- metadata +45 -10
data/Gemfile
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
source 'http://rubygems.org'
|
2
2
|
|
3
|
-
gem
|
4
|
-
gem
|
5
|
-
gem
|
3
|
+
gem 'ruby-debug', :platform => :ruby_18
|
4
|
+
gem 'linecache19', :platform => :ruby_19
|
5
|
+
gem 'ruby-debug19', :platform => :ruby_19
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem 'webmock'
|
9
|
+
end
|
6
10
|
|
7
11
|
gemspec
|
data/README.markdown
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Metric
|
1
|
+
# Metric [![Build Status](http://travis-ci.org/bittersweet/metric.png)](http://travis-ci.org/bittersweet/metric)
|
2
2
|
|
3
3
|
Track simple metrics via [metric.io](metric.io).
|
4
4
|
|
@@ -51,6 +51,10 @@ def register
|
|
51
51
|
end
|
52
52
|
```
|
53
53
|
|
54
|
+
## Documentation
|
55
|
+
|
56
|
+
Although the code is pretty lightweight and self-explanatory, [documentation](http://rdoc.info/github/bittersweet/metric/master/frames) is available via rdoc.info.
|
57
|
+
|
54
58
|
## Thanks
|
55
59
|
|
56
60
|
[jeffkreeftmeijer](https://github.com/jeffkreeftmeijer) for providing me with the awesome domainname!
|
data/lib/metric.rb
CHANGED
@@ -1,40 +1,35 @@
|
|
1
1
|
require 'metric/configuration'
|
2
|
-
require '
|
2
|
+
require 'metric/track'
|
3
|
+
require 'metric/receive'
|
3
4
|
require 'cgi'
|
4
5
|
|
5
6
|
module Metric
|
6
7
|
class << self
|
8
|
+
# Holds the configuration for easy access to settings
|
7
9
|
attr_accessor :configuration
|
8
10
|
|
11
|
+
# Configures gem options
|
12
|
+
#
|
13
|
+
# @param [Block]
|
9
14
|
def configure
|
10
15
|
self.configuration ||= Metric::Configuration.new
|
11
16
|
yield(configuration)
|
12
17
|
end
|
13
18
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
key = "?api_key=" + Metric.configuration.api_key
|
19
|
-
url = Metric.configuration.metric_host + '/track'
|
20
|
-
url << key
|
21
|
-
url << parse_metric(metric)
|
22
|
-
url << "&amount=#{amount}" if amount
|
23
|
-
url << "&trigger=1" if trigger
|
24
|
-
url
|
25
|
-
end
|
26
|
-
|
19
|
+
# Tracks metrics
|
20
|
+
#
|
21
|
+
# @param [String, Hash]
|
22
|
+
# @return [nil]
|
27
23
|
def track(metric, options = {})
|
28
|
-
|
29
|
-
|
30
|
-
url = compose(metric, options)
|
31
|
-
Thread.new do
|
32
|
-
`curl "#{url}" 2>&1 ; `
|
33
|
-
end
|
24
|
+
Metric::Track.track(metric, options)
|
34
25
|
end
|
35
26
|
|
36
|
-
|
37
|
-
|
27
|
+
# Fetches data from the API
|
28
|
+
#
|
29
|
+
# @param [String, String]
|
30
|
+
# @return [Hash]
|
31
|
+
def receive(metric, range)
|
32
|
+
Metric::Receive.receive(metric, range)
|
38
33
|
end
|
39
34
|
end
|
40
35
|
end
|
data/lib/metric/configuration.rb
CHANGED
@@ -1,10 +1,20 @@
|
|
1
1
|
module Metric
|
2
|
+
|
3
|
+
# Used for configuration of the Metric gem. The only required option is
|
4
|
+
# api_key, secret_key is only used if you want to pull data out from the API
|
5
|
+
# and metric_host is used for local debugging purposes.
|
6
|
+
|
2
7
|
class Configuration
|
8
|
+
# Key used to identify the website
|
3
9
|
attr_accessor :api_key
|
4
10
|
|
5
|
-
# Allows setting a different host, used for development purposes
|
11
|
+
# Allows setting a different host to send data to, used for development purposes
|
6
12
|
attr_accessor :metric_host
|
7
13
|
|
14
|
+
# Used to generate a hash for getting data out
|
15
|
+
attr_accessor :secret_key
|
16
|
+
|
17
|
+
# Sets defaults
|
8
18
|
def initialize
|
9
19
|
@metric_host = "http://api.metric.io"
|
10
20
|
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'faraday'
|
3
|
+
require 'multi_json'
|
4
|
+
|
5
|
+
module Metric
|
6
|
+
|
7
|
+
# Fetch data via the metric.io API
|
8
|
+
|
9
|
+
class Receive
|
10
|
+
# Generate a hash of the site's secret_key and metric identifier
|
11
|
+
#
|
12
|
+
# @param [String] metric Metric identifier
|
13
|
+
# @return [String]
|
14
|
+
def self.generate_token(metric)
|
15
|
+
Digest::MD5.hexdigest(Metric.configuration.secret_key + metric)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Generate the url with query strings
|
19
|
+
#
|
20
|
+
# @param [String] metric Metric identifier
|
21
|
+
# @param [String] range Range identifier, either total, today, week or month
|
22
|
+
# @return [String]
|
23
|
+
def self.compose(metric, range)
|
24
|
+
key = "?api_key=" + Metric.configuration.api_key
|
25
|
+
url = Metric.configuration.metric_host + '/receive'
|
26
|
+
url << key
|
27
|
+
url << "&token=" + generate_token(metric)
|
28
|
+
url << parse_metric(metric)
|
29
|
+
url << "&range=" + range
|
30
|
+
end
|
31
|
+
|
32
|
+
# Returns and memoizes a Faraday connection
|
33
|
+
#
|
34
|
+
# @return [Faraday::Connection]
|
35
|
+
def self.connection
|
36
|
+
@connection ||= Faraday.new do |builder|
|
37
|
+
builder.adapter :net_http
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Perform the actual request and parse JSON
|
42
|
+
#
|
43
|
+
# @param [String] metric Metric identifier
|
44
|
+
# @param [String] range Range identifier, either total, today, week or month
|
45
|
+
# @return [Hash] response from the API
|
46
|
+
def self.receive(metric, range)
|
47
|
+
url = compose(metric, range)
|
48
|
+
response = connection.get(url)
|
49
|
+
MultiJson.decode(response.body)
|
50
|
+
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
|
+
end
|
60
|
+
end
|
61
|
+
|
data/lib/metric/track.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Metric
|
2
|
+
|
3
|
+
# Used to track metrics
|
4
|
+
|
5
|
+
class Track
|
6
|
+
# Generate the url with query strings
|
7
|
+
#
|
8
|
+
# @param [String] metric Metric identifier
|
9
|
+
# @param [Hash] options Options
|
10
|
+
# @option options [Symbol] :amount Amount to track
|
11
|
+
# @option options [Symbol] :trigger Flag for email notification
|
12
|
+
# @return [String]
|
13
|
+
def self.compose(metric, options = {})
|
14
|
+
amount = options[:amount]
|
15
|
+
trigger = options[:trigger]
|
16
|
+
|
17
|
+
key = "?api_key=" + Metric.configuration.api_key
|
18
|
+
url = Metric.configuration.metric_host + '/track'
|
19
|
+
url << key
|
20
|
+
url << parse_metric(metric)
|
21
|
+
url << "&amount=#{amount}" if amount
|
22
|
+
url << "&trigger=1" if trigger
|
23
|
+
url
|
24
|
+
end
|
25
|
+
|
26
|
+
# Uses a thread to perform the request
|
27
|
+
# @note If this gem is used with Rails it will only track data in the production environment
|
28
|
+
#
|
29
|
+
# @param [String, Hash]
|
30
|
+
# @return [nil]
|
31
|
+
def self.track(metric, options = {})
|
32
|
+
return if defined?(Rails) && !Rails.env.production?
|
33
|
+
return if options[:amount] && options[:amount] == 0
|
34
|
+
|
35
|
+
url = compose(metric, options)
|
36
|
+
Thread.new do
|
37
|
+
`curl "#{url}" 2>&1 ; `
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# CGI escape the metric name so spaces and characters are allowed
|
42
|
+
#
|
43
|
+
# @param [String]
|
44
|
+
# @return [String]
|
45
|
+
def self.parse_metric(metric)
|
46
|
+
"&metric=#{CGI.escape(metric)}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/metric/version.rb
CHANGED
data/metric.gemspec
CHANGED
@@ -15,6 +15,9 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ['lib']
|
16
16
|
gem.version = Metric::VERSION
|
17
17
|
|
18
|
+
gem.add_runtime_dependency 'faraday'
|
19
|
+
gem.add_runtime_dependency 'multi_json'
|
20
|
+
|
18
21
|
# Tests
|
19
22
|
gem.add_development_dependency 'rspec'
|
20
23
|
gem.add_development_dependency 'webmock'
|
@@ -8,6 +8,13 @@ describe Metric::Configuration do
|
|
8
8
|
Metric.configuration.metric_host.should == "http://api.metric.io"
|
9
9
|
end
|
10
10
|
|
11
|
+
it "configures metric host" do
|
12
|
+
Metric.configure do |config|
|
13
|
+
config.metric_host = "http://localhost:5000"
|
14
|
+
end
|
15
|
+
Metric.configuration.metric_host.should == "http://localhost:5000"
|
16
|
+
end
|
17
|
+
|
11
18
|
it "configures api_key" do
|
12
19
|
Metric.configure do |config|
|
13
20
|
config.api_key = "test"
|
@@ -15,10 +22,10 @@ describe Metric::Configuration do
|
|
15
22
|
Metric.configuration.api_key.should == "test"
|
16
23
|
end
|
17
24
|
|
18
|
-
it "configures
|
25
|
+
it "configures secret key" do
|
19
26
|
Metric.configure do |config|
|
20
|
-
config.
|
27
|
+
config.secret_key = "random_string"
|
21
28
|
end
|
22
|
-
Metric.configuration.
|
29
|
+
Metric.configuration.secret_key.should == "random_string"
|
23
30
|
end
|
24
31
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Metric::Receive do
|
4
|
+
before do
|
5
|
+
reset_config
|
6
|
+
Metric.configure do |config|
|
7
|
+
config.secret_key = "1234"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it "generates correct hash via secret_token" do
|
12
|
+
Metric::Receive.generate_token("hits").should == "c6daa87bcf8bf7cb4d1c74d872793e5e"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "gets total" do
|
16
|
+
request = "http://api.metric.io/receive?api_key=spec&token=c6daa87bcf8bf7cb4d1c74d872793e5e&metric=hits&range=total"
|
17
|
+
Metric::Receive.compose("hits", "total").should == request
|
18
|
+
end
|
19
|
+
|
20
|
+
it "gets today" do
|
21
|
+
request = "http://api.metric.io/receive?api_key=spec&token=c6daa87bcf8bf7cb4d1c74d872793e5e&metric=hits&range=today"
|
22
|
+
Metric::Receive.compose("hits", "today").should == request
|
23
|
+
end
|
24
|
+
|
25
|
+
it "gets week" do
|
26
|
+
request = "http://api.metric.io/receive?api_key=spec&token=c6daa87bcf8bf7cb4d1c74d872793e5e&metric=hits&range=week"
|
27
|
+
Metric::Receive.compose("hits", "week").should == request
|
28
|
+
end
|
29
|
+
|
30
|
+
it "gets month" do
|
31
|
+
request = "http://api.metric.io/receive?api_key=spec&token=c6daa87bcf8bf7cb4d1c74d872793e5e&metric=hits&range=month"
|
32
|
+
Metric::Receive.compose("hits", "month").should == request
|
33
|
+
end
|
34
|
+
|
35
|
+
it "grabs actual data" do
|
36
|
+
stub_request(:get, "http://api.metric.io/receive?api_key=spec&metric=hits&range=today&token=c6daa87bcf8bf7cb4d1c74d872793e5e").
|
37
|
+
to_return(:status => 200, :body => "{\"total\":\"1\"}", :headers => {})
|
38
|
+
Metric::Receive.receive("hits", "today").should == {"total" => "1"}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Metric::Track do
|
4
|
+
before do
|
5
|
+
reset_config
|
6
|
+
end
|
7
|
+
|
8
|
+
it "composes the request url" do
|
9
|
+
Metric::Track.compose("hits").should == "http://api.metric.io/track?api_key=spec&metric=hits"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "gets correct url when tracking" do
|
13
|
+
Metric::Track.should_receive(:compose).with("hits", {})
|
14
|
+
Metric::Track.track("hits")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "encodes the request url" do
|
18
|
+
Metric::Track.parse_metric("hits and spaces").should == "&metric=hits+and+spaces"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "sends trigger param" do
|
22
|
+
url = "http://api.metric.io/track?api_key=spec&metric=hits&trigger=1"
|
23
|
+
Metric::Track.compose("hits", {:trigger => true}).should == url
|
24
|
+
end
|
25
|
+
|
26
|
+
it "sends custom amount" do
|
27
|
+
url = "http://api.metric.io/track?api_key=spec&metric=hits&amount=42"
|
28
|
+
Metric::Track.compose("hits", {:amount => 42}).should == url
|
29
|
+
end
|
30
|
+
|
31
|
+
it "does nothing if amount is 0" do
|
32
|
+
Metric::Track.track("hits", {:amount => 0}).should == nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
data/spec/metric_spec.rb
CHANGED
@@ -5,27 +5,14 @@ describe Metric do
|
|
5
5
|
reset_config
|
6
6
|
end
|
7
7
|
|
8
|
-
it "
|
9
|
-
Metric.
|
10
|
-
end
|
11
|
-
|
12
|
-
it "gets correct url when tracking" do
|
13
|
-
Metric.should_receive(:compose).with("hits", {})
|
8
|
+
it "passes through options to track" do
|
9
|
+
Metric::Track.should_receive(:track).with("hits", {})
|
14
10
|
Metric.track("hits")
|
15
11
|
end
|
16
12
|
|
17
|
-
it "
|
18
|
-
Metric.
|
19
|
-
|
20
|
-
|
21
|
-
it "sends trigger param" do
|
22
|
-
url = "http://api.metric.io/track?api_key=spec&metric=hits&trigger=1"
|
23
|
-
Metric.compose("hits", {:trigger => true}).should == url
|
24
|
-
end
|
25
|
-
|
26
|
-
it "sends custom amount" do
|
27
|
-
url = "http://api.metric.io/track?api_key=spec&metric=hits&amount=42"
|
28
|
-
Metric.compose("hits", {:amount => 42}).should == url
|
13
|
+
it "passes through options to receive" do
|
14
|
+
Metric::Receive.should_receive(:receive).with("hits", "total")
|
15
|
+
Metric.receive("hits", "total")
|
29
16
|
end
|
30
17
|
end
|
31
18
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metric
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mark Mulder
|
@@ -15,11 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-08
|
18
|
+
date: 2011-09-08 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: faraday
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
@@ -30,10 +30,10 @@ dependencies:
|
|
30
30
|
segments:
|
31
31
|
- 0
|
32
32
|
version: "0"
|
33
|
-
type: :
|
33
|
+
type: :runtime
|
34
34
|
version_requirements: *id001
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
|
-
name:
|
36
|
+
name: multi_json
|
37
37
|
prerelease: false
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
@@ -44,8 +44,36 @@ dependencies:
|
|
44
44
|
segments:
|
45
45
|
- 0
|
46
46
|
version: "0"
|
47
|
-
type: :
|
47
|
+
type: :runtime
|
48
48
|
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: webmock
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
type: :development
|
76
|
+
version_requirements: *id004
|
49
77
|
description: Track metrics via metric.io
|
50
78
|
email:
|
51
79
|
- markmulder@gmail.com
|
@@ -60,13 +88,18 @@ files:
|
|
60
88
|
- .rspec
|
61
89
|
- .travis.yml
|
62
90
|
- Gemfile
|
91
|
+
- Gemfile.lock
|
63
92
|
- README.markdown
|
64
93
|
- Rakefile
|
65
94
|
- lib/metric.rb
|
66
95
|
- lib/metric/configuration.rb
|
96
|
+
- lib/metric/receive.rb
|
97
|
+
- lib/metric/track.rb
|
67
98
|
- lib/metric/version.rb
|
68
99
|
- metric.gemspec
|
69
100
|
- spec/metric/configuration_spec.rb
|
101
|
+
- spec/metric/receive_spec.rb
|
102
|
+
- spec/metric/track_spec.rb
|
70
103
|
- spec/metric_spec.rb
|
71
104
|
- spec/spec_helper.rb
|
72
105
|
has_rdoc: true
|
@@ -99,11 +132,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
132
|
requirements: []
|
100
133
|
|
101
134
|
rubyforge_project:
|
102
|
-
rubygems_version: 1.
|
135
|
+
rubygems_version: 1.6.2
|
103
136
|
signing_key:
|
104
137
|
specification_version: 3
|
105
138
|
summary: Companion gem to the metric.io site to track metrics
|
106
139
|
test_files:
|
107
140
|
- spec/metric/configuration_spec.rb
|
141
|
+
- spec/metric/receive_spec.rb
|
142
|
+
- spec/metric/track_spec.rb
|
108
143
|
- spec/metric_spec.rb
|
109
144
|
- spec/spec_helper.rb
|