em-stathat 0.1.0
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/.gitignore +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +7 -0
- data/README.md +45 -0
- data/em-stathat.gemspec +21 -0
- data/lib/em-stathat.rb +100 -0
- data/lib/em-stathat/version.rb +5 -0
- data/spec/em-stathat_spec.rb +78 -0
- data/spec/spec_helper.rb +7 -0
- metadata +67 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.2
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
## What
|
2
|
+
|
3
|
+
An Eventmachine-compatible wrapper for the stathat api
|
4
|
+
(http://stathat.com), built on top of
|
5
|
+
[em-http-request](https://github.com/igrigorik/em-http-request).
|
6
|
+
|
7
|
+
**NOTE:** Currently, only the EZ api is supported.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
Configure your settings:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
EM::StatHat.config do |c|
|
15
|
+
c.ukey = 'your unique user key'
|
16
|
+
c.email = 'user@example.com' # for sending stats via the EZ api
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
### Send stats using the EZ api
|
21
|
+
|
22
|
+
#### Timers
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
EM::StatHat.new.time('stat name') do
|
26
|
+
# code to profile
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
#### Counts
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
EM::StatHat.new.ez_count('Users Created', 1) # defaults to 1
|
34
|
+
```
|
35
|
+
|
36
|
+
#### Values
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
EM::StatHat.new.ez_value('some metric', 123)
|
40
|
+
```
|
41
|
+
|
42
|
+
## Reference
|
43
|
+
|
44
|
+
* [stathat](https://github.com/patrickxb/stathat)
|
45
|
+
* [em-http-request](https://github.com/igrigorik/em-http-request).
|
data/em-stathat.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "em-stathat/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "em-stathat"
|
7
|
+
s.version = EventMachine::Stathat::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Alex Sharp"]
|
10
|
+
s.email = ["ajsharp@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/ajsharp/em-stathat"
|
12
|
+
s.summary = %q{An EventMachine-compatible async wrapper for the stathat api.}
|
13
|
+
s.description = %q{Essentially a clone of the normal stathat gem, only for use with EventMachine.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'em-http-request', '~> 1.0'
|
21
|
+
end
|
data/lib/em-stathat.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'eventmachine'
|
2
|
+
require 'em-http'
|
3
|
+
|
4
|
+
module EventMachine
|
5
|
+
class StatHat
|
6
|
+
Config = Struct.new(:ukey, :email, :debug)
|
7
|
+
@@config = Config.new
|
8
|
+
|
9
|
+
class << self
|
10
|
+
# Configure EM::StatHat
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# EM::StatHat.config do |c|
|
14
|
+
# c.ukey = 'Qu9sj34ncWXi0e83'
|
15
|
+
# u.email = 'your.email@wherever.com'
|
16
|
+
# end
|
17
|
+
def config(&block)
|
18
|
+
if block_given?
|
19
|
+
@@config.tap(&block)
|
20
|
+
else
|
21
|
+
@@config
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def configured?
|
26
|
+
!!config.ukey && !!config.email
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(base_uri = 'http://api.stathat.com')
|
31
|
+
raise(RuntimeError, "You must configure EM::StatHat before using it!") unless self.class.configured?
|
32
|
+
@base_uri = base_uri
|
33
|
+
end
|
34
|
+
|
35
|
+
def config; self.class.config; end
|
36
|
+
|
37
|
+
# Time a block of code and send the duration as a value
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
# StatHat.new.time('some identifying name') do
|
41
|
+
# # code
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# @param [String] name the name of the stat
|
45
|
+
# @param [Hash] opts a hash of options
|
46
|
+
# @option [Symbol] :ez Send data via the ez api (default: true)
|
47
|
+
def time(name, opts = {})
|
48
|
+
opts[:ez] ||= true
|
49
|
+
|
50
|
+
start = Time.now
|
51
|
+
yield if block_given?
|
52
|
+
|
53
|
+
if opts[:ez] == true
|
54
|
+
ez_value(name, (Time.now - start))
|
55
|
+
else
|
56
|
+
value(name, (Time.now - start))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Record a value via the EZ api
|
61
|
+
def ez_value(stat_name, value)
|
62
|
+
ez(stat_name, {:value => value})
|
63
|
+
end
|
64
|
+
|
65
|
+
# Increment a counter via the EZ api
|
66
|
+
def ez_count(stat_name, count = 1)
|
67
|
+
ez(stat_name, {:count => count})
|
68
|
+
end
|
69
|
+
|
70
|
+
# Record a numeric value
|
71
|
+
#
|
72
|
+
# @param [String] name the stat name
|
73
|
+
# @param [Number] value the value to record
|
74
|
+
# def value(name, value)
|
75
|
+
# request('/v', {:key => name, :value => value})
|
76
|
+
# end
|
77
|
+
|
78
|
+
# Increment a counter
|
79
|
+
#
|
80
|
+
# @param [String] name the stat name
|
81
|
+
# @param [Number] count the amount by which to increment the counter
|
82
|
+
# def count(name, count)
|
83
|
+
# request('/c', {:key => name, :count => count})
|
84
|
+
# end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def ez(stat, value_or_count)
|
89
|
+
request('/ez', {
|
90
|
+
:stat => stat,
|
91
|
+
:email => config.email
|
92
|
+
}.merge(value_or_count))
|
93
|
+
end
|
94
|
+
|
95
|
+
# Return a deferrable object
|
96
|
+
def request(endpoint, opts)
|
97
|
+
EventMachine::HttpRequest.new(@base_uri + endpoint).post(:body => opts)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EM::StatHat, '.config' do
|
4
|
+
before do
|
5
|
+
EM::StatHat.config { } # reset the config
|
6
|
+
end
|
7
|
+
|
8
|
+
it "can set ukey" do
|
9
|
+
EM::StatHat.config do |c|
|
10
|
+
c.ukey = 'thing'
|
11
|
+
end
|
12
|
+
EM::StatHat.config.ukey.should == 'thing'
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns a config instance" do
|
16
|
+
EM::StatHat.config() {}.should be_instance_of EM::StatHat::Config
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can reset stuff" do
|
20
|
+
EM::StatHat.config do |c|
|
21
|
+
c.ukey = 'thing'
|
22
|
+
end
|
23
|
+
|
24
|
+
EM::StatHat.config do |c|
|
25
|
+
c.ukey = 'otherthing'
|
26
|
+
end
|
27
|
+
EM::StatHat.config.ukey.should == 'otherthing'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "can set email" do
|
31
|
+
EM::StatHat.config do |c|
|
32
|
+
c.email = 'user@example.com'
|
33
|
+
end
|
34
|
+
EM::StatHat.config.email.should == 'user@example.com'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe EM::StatHat, 'initializing' do
|
39
|
+
it "raises an error if EM::StatHat has not been configured" do
|
40
|
+
EM::StatHat.config { |c| c.ukey = nil }
|
41
|
+
expect {
|
42
|
+
EM::StatHat.new
|
43
|
+
}.to raise_error(RuntimeError, "You must configure EM::StatHat before using it!")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "does not raise an error if ukey is configured" do
|
47
|
+
EM::StatHat.config { |u| u.ukey = 'key' }
|
48
|
+
expect { EM::StatHat.new }.not_to raise_error
|
49
|
+
end
|
50
|
+
|
51
|
+
it "sets the base url by default" do
|
52
|
+
#EM::StatHat.new.base_url.should ==
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe EM::StatHat, '.configured?' do
|
57
|
+
before { EM::StatHat.config { } }
|
58
|
+
subject { EM::StatHat.configured? }
|
59
|
+
|
60
|
+
it "returns true if both email and ukey are set" do
|
61
|
+
EM::StatHat.config { |c| c.ukey = 'key'; c.email = 'user@example.com' }
|
62
|
+
EM::StatHat.should be_configured
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns false if only email is configured" do
|
66
|
+
EM::StatHat.config { |c| c.ukey = nil; c.email = 'user@example.com' }
|
67
|
+
EM::StatHat.should_not be_configured
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns false if only ukey is configured" do
|
71
|
+
EM::StatHat.config { |c| c.ukey = 'key'; c.email = nil }
|
72
|
+
EM::StatHat.should_not be_configured
|
73
|
+
end
|
74
|
+
|
75
|
+
it "returns false if both are unconfigured" do
|
76
|
+
EM::StatHat.should_not be_configured
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-stathat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Sharp
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-09 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: em-http-request
|
16
|
+
requirement: &2156377520 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156377520
|
25
|
+
description: Essentially a clone of the normal stathat gem, only for use with EventMachine.
|
26
|
+
email:
|
27
|
+
- ajsharp@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rvmrc
|
34
|
+
- Gemfile
|
35
|
+
- README.md
|
36
|
+
- em-stathat.gemspec
|
37
|
+
- lib/em-stathat.rb
|
38
|
+
- lib/em-stathat/version.rb
|
39
|
+
- spec/em-stathat_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
homepage: https://github.com/ajsharp/em-stathat
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.6
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: An EventMachine-compatible async wrapper for the stathat api.
|
65
|
+
test_files:
|
66
|
+
- spec/em-stathat_spec.rb
|
67
|
+
- spec/spec_helper.rb
|