delayed_kiss 0.0.1
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 +4 -0
- data/.rspec +2 -0
- data/Gemfile +6 -0
- data/README.textile +44 -0
- data/Rakefile +7 -0
- data/delayed_kiss.gemspec +25 -0
- data/lib/delayed_kiss/version.rb +3 -0
- data/lib/delayed_kiss.rb +52 -0
- data/spec/delayed_kiss_spec.rb +110 -0
- data/spec/spec_helper.rb +37 -0
- metadata +103 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.textile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
h1. Delayed Kiss
|
2
|
+
|
3
|
+
A simple interface for the KISSmetrics API that plays nicely with Delayed Job and Heroku.
|
4
|
+
|
5
|
+
h2. Installation
|
6
|
+
|
7
|
+
Include the gem in your gemfile:
|
8
|
+
|
9
|
+
<pre>
|
10
|
+
gem 'delayed_kiss'
|
11
|
+
</pre>
|
12
|
+
|
13
|
+
Configure your API key:
|
14
|
+
<pre>
|
15
|
+
#in config/kiss_metrics.yml
|
16
|
+
development:
|
17
|
+
key: <yourkey>
|
18
|
+
test:
|
19
|
+
key: <yourkey>
|
20
|
+
production:
|
21
|
+
key: <yourkey>
|
22
|
+
</pre>
|
23
|
+
Initialize the API key
|
24
|
+
<pre>
|
25
|
+
#in config/initializers
|
26
|
+
KM_CONFIG = YAML::load(ERB.new(IO.read(File.join(Rails.root, "config/kissmetrics.yml"))).result)[Rails.env]
|
27
|
+
if !KM_CONFIG.blank?
|
28
|
+
DelayedKiss.configure do |config|
|
29
|
+
config.key = KM_CONFIG['key']
|
30
|
+
end
|
31
|
+
end
|
32
|
+
</pre>
|
33
|
+
|
34
|
+
h2. Usage
|
35
|
+
|
36
|
+
Start recording events anywhere in your application:
|
37
|
+
<pre>
|
38
|
+
DelayedKiss.alias(anonymous_user, user.km_id)
|
39
|
+
DelayedKiss.record(user.km_id, 'signed up')
|
40
|
+
</pre>
|
41
|
+
|
42
|
+
h2. Thanks
|
43
|
+
|
44
|
+
DelayedKiss is derived and gemified from https://github.com/vbrendel/delayed_km
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "delayed_kiss/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "delayed_kiss"
|
7
|
+
s.version = DelayedKiss::VERSION
|
8
|
+
s.authors = ["Dustin DeYoung"]
|
9
|
+
s.email = ["ddeyoung@authorsolutions.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{KissMetrics API with Delayed Job}
|
12
|
+
s.description = %q{A simple wrapper for the KissMetrics API using Delayed Job}
|
13
|
+
|
14
|
+
s.rubyforge_project = "delayed_kiss"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency("activesupport")
|
22
|
+
s.add_dependency("i18n")
|
23
|
+
s.add_dependency("httparty", ">= 0.8.1")
|
24
|
+
s.add_dependency("delayed_job", ">= 2.1.4")
|
25
|
+
end
|
data/lib/delayed_kiss.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "delayed_kiss/version"
|
2
|
+
|
3
|
+
module DelayedKiss
|
4
|
+
mattr_accessor :key
|
5
|
+
@@key = nil
|
6
|
+
|
7
|
+
def self.configure
|
8
|
+
yield self
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.record(id, event, query_params={})
|
12
|
+
raise DelayedKiss::ConfigurationError if @@key.blank?
|
13
|
+
raise ArgumentError.new("id can't be blank") if id.blank?
|
14
|
+
raise ArgumentError.new("event can't be blank") if event.blank?
|
15
|
+
|
16
|
+
query_params.merge!({
|
17
|
+
'_n' => event,
|
18
|
+
'_p' => id,
|
19
|
+
'_t' => Time.now.to_i.to_s,
|
20
|
+
'_k' => @@key
|
21
|
+
})
|
22
|
+
HTTParty.delay.get('https://trk.kissmetrics.com/e?' + query_params.to_param)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.alias(alias_from, alias_to)
|
26
|
+
raise DelayedKiss::ConfigurationError if @@key.blank?
|
27
|
+
raise ArgumentError.new("you must specify both a from a to value") if alias_from.blank? || alias_to.blank?
|
28
|
+
|
29
|
+
query_params = {
|
30
|
+
'_n' => alias_to,
|
31
|
+
'_p' => alias_from,
|
32
|
+
'_t' => Time.now.to_i.to_s,
|
33
|
+
'_k' => @@key
|
34
|
+
}
|
35
|
+
HTTParty.delay.get('https://trk.kissmetrics.com/a?' + query_params.to_param)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.set(id, query_params)
|
39
|
+
raise DelayedKiss::ConfigurationError if @@key.blank?
|
40
|
+
raise ArgumentError.new("id can't be blank") if !id || id.blank?
|
41
|
+
return if query_params.blank? # don't do anything if we're not setting any values on the identity
|
42
|
+
|
43
|
+
query_params.merge!({
|
44
|
+
'_p' => id,
|
45
|
+
'_t' => Time.now.to_i.to_s,
|
46
|
+
'_k' => @@key
|
47
|
+
})
|
48
|
+
HTTParty.delay.get('https://trk.kissmetrics.com/s?' + query_params.to_param)
|
49
|
+
end
|
50
|
+
|
51
|
+
class ConfigurationError < StandardError; end
|
52
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DelayedKiss do
|
4
|
+
describe :configure do
|
5
|
+
before(:each) do
|
6
|
+
DelayedKiss.cache_config
|
7
|
+
end
|
8
|
+
|
9
|
+
after(:each) do
|
10
|
+
DelayedKiss.reset_config
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should configure the key" do
|
14
|
+
test_key = "youraikeyvalue"
|
15
|
+
DelayedKiss.configure do |config|
|
16
|
+
config.key = test_key
|
17
|
+
end
|
18
|
+
|
19
|
+
DelayedKiss.key.should == test_key
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe :record do
|
24
|
+
it "should send a request to the KISSmetrics API when valid parameters are supplied" do
|
25
|
+
HTTParty.expects(:get).once.returns(true)
|
26
|
+
DelayedKiss.record("identity", "event")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise an error without an identity" do
|
30
|
+
HTTParty.expects(:get).never
|
31
|
+
expect { DelayedKiss.record(nil, "event") }.to raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should rais an error without an event" do
|
35
|
+
expect { DelayedKiss.record("identity", nil) }.to raise_error(ArgumentError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should send a request to the KISSmetrics API with custom properties in the URL's query string" do
|
39
|
+
HTTParty.expects(:get).with(regexp_matches(/type=crazy/)).returns(true)
|
40
|
+
DelayedKiss.record("identity", "event", {:type => "crazy"})
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should raise an error if the key is not configured" do
|
44
|
+
DelayedKiss.cache_config
|
45
|
+
DelayedKiss.key = nil
|
46
|
+
expect { DelayedKiss.record("identity", "event") }.to raise_error(DelayedKiss::ConfigurationError)
|
47
|
+
DelayedKiss.reset_config
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe :alias do
|
52
|
+
it "should send a request to the KISSmetrics API when valid parameters are supplied" do
|
53
|
+
HTTParty.expects(:get).once.returns(true)
|
54
|
+
DelayedKiss.alias("oldname", "newname")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should include the from alias in the query string" do
|
58
|
+
HTTParty.expects(:get).with(regexp_matches(/_p=oldname/)).returns(true)
|
59
|
+
DelayedKiss.alias("oldname", "newname")
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should include the to alias in the query string" do
|
63
|
+
HTTParty.expects(:get).with(regexp_matches(/_n=newname/)).returns(true)
|
64
|
+
DelayedKiss.alias("oldname", "newname")
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should raise an error without a from alias" do
|
68
|
+
expect { DelayedKiss.alias(nil, "newname") }.to raise_error(ArgumentError)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should raise an error without a to alias" do
|
72
|
+
expect { DelayedKiss.alias("oldname", nil) }.to raise_error(ArgumentError)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should raise an error if the key is not configured" do
|
76
|
+
DelayedKiss.cache_config
|
77
|
+
DelayedKiss.key = nil
|
78
|
+
expect { DelayedKiss.record("identity", "event") }.to raise_error(DelayedKiss::ConfigurationError)
|
79
|
+
DelayedKiss.reset_config
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe :set do
|
84
|
+
it "should send a request to the KISSmetrics API when valid parameters are supplied" do
|
85
|
+
HTTParty.expects(:get).once.returns(true)
|
86
|
+
DelayedKiss.set("identity", {:gender => :male})
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should include the set properties in the query string" do
|
90
|
+
HTTParty.expects(:get).with(regexp_matches(/age=27/)).returns(true)
|
91
|
+
DelayedKiss.set("identity", {:age => 27})
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should not send a reuqest to the KISSmetrics API when the set paramters are blank" do
|
95
|
+
HTTParty.expects(:get).never
|
96
|
+
DelayedKiss.set("identity", {})
|
97
|
+
end
|
98
|
+
|
99
|
+
it "raises an error without an identity" do
|
100
|
+
expect { DelayedKiss.set(nil, {:gener => :female}) }.to raise_error(ArgumentError)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should raise an error if the key is not configured" do
|
104
|
+
DelayedKiss.cache_config
|
105
|
+
DelayedKiss.key = nil
|
106
|
+
expect { DelayedKiss.record("identity", "event") }.to raise_error(DelayedKiss::ConfigurationError)
|
107
|
+
DelayedKiss.reset_config
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
|
5
|
+
require 'active_support'
|
6
|
+
require 'active_support/core_ext'
|
7
|
+
require 'httparty'
|
8
|
+
require 'delayed_job'
|
9
|
+
require 'delayed_kiss'
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.mock_with :mocha
|
13
|
+
end
|
14
|
+
|
15
|
+
DelayedKiss.configure do |config|
|
16
|
+
config.key = "dlf23jkd9sl32nfj99kl2s8635h3jk33f"
|
17
|
+
end
|
18
|
+
|
19
|
+
# provide methods for clearing and restoring the configurations
|
20
|
+
module DelayedKiss
|
21
|
+
@@cached_key = nil
|
22
|
+
|
23
|
+
def self.cache_config
|
24
|
+
@@cached_key = @@key
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.reset_config
|
28
|
+
@@key = @@cached_key if @@cached_key
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# ignore delaying jobs
|
33
|
+
module Delayed::MessageSending
|
34
|
+
def delay
|
35
|
+
return self
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delayed_kiss
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dustin DeYoung
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-07 00:00:00.000000000 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
requirement: &2152316100 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2152316100
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: i18n
|
28
|
+
requirement: &2152315680 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2152315680
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: httparty
|
39
|
+
requirement: &2152315180 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 0.8.1
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *2152315180
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: delayed_job
|
50
|
+
requirement: &2152314680 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.1.4
|
56
|
+
type: :runtime
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *2152314680
|
59
|
+
description: A simple wrapper for the KissMetrics API using Delayed Job
|
60
|
+
email:
|
61
|
+
- ddeyoung@authorsolutions.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- .gitignore
|
67
|
+
- .rspec
|
68
|
+
- Gemfile
|
69
|
+
- README.textile
|
70
|
+
- Rakefile
|
71
|
+
- delayed_kiss.gemspec
|
72
|
+
- lib/delayed_kiss.rb
|
73
|
+
- lib/delayed_kiss/version.rb
|
74
|
+
- spec/delayed_kiss_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
has_rdoc: true
|
77
|
+
homepage: ''
|
78
|
+
licenses: []
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project: delayed_kiss
|
97
|
+
rubygems_version: 1.6.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: KissMetrics API with Delayed Job
|
101
|
+
test_files:
|
102
|
+
- spec/delayed_kiss_spec.rb
|
103
|
+
- spec/spec_helper.rb
|