delayed_km 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 ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in delayed_km.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Marc Beaupre
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ Delayed KM
2
+ ==========
3
+
4
+ # DelayedKm
5
+
6
+ Alternative for the Kissmetrics provided gem but using delayed_job instead of cron. This will suit Heroku users.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'delayed_km'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install delayed_km
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
33
+ gem 'httparty'
34
+ gem 'delayed_job'
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/delayed_km/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Vincent Brendel, Dirk Elmendorf"]
6
+ gem.email = ["jack.quack@gmail.com"]
7
+ gem.description = %q{Alternative for the Kissmetrics provided gem but using delayed_job instead of cron. This will suit Heroku users.}
8
+ gem.summary = %q{Kissmetrics with delayed_job}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "delayed_km"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = DelayedKm::VERSION
17
+
18
+ gem.add_dependency('httparty')
19
+ gem.add_dependency('delayed_job')
20
+ gem.add_dependency('rake')
21
+ gem.add_development_dependency('rspec')
22
+ end
23
+
@@ -0,0 +1,3 @@
1
+ module DelayedKm
2
+ VERSION = "0.0.1"
3
+ end
data/lib/delayed_km.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "delayed_km/version"
2
+
3
+ module DelayedKm
4
+ @id = nil
5
+ @key = nil
6
+
7
+ class << self
8
+ def identity
9
+ @id
10
+ end
11
+
12
+ def api_key
13
+ @key
14
+ end
15
+
16
+ def host
17
+ "trk.kissmetrics.com"
18
+ end
19
+
20
+ def init(key)
21
+ @key = key
22
+ end
23
+
24
+ def identify(id)
25
+ @id = id
26
+ end
27
+
28
+ def record(action, props = {})
29
+ raise ArgumentError, "You must set an API key" if !@key
30
+ raise ArgumentError, "You must identify a user first" if !@id
31
+ props = hash_keys_to_str(props)
32
+ props.update("_n" => action)
33
+ props.update("_p" => @id)
34
+ props.update("_k" => @key)
35
+
36
+ if props["_t"]
37
+ props['_d'] = "1"
38
+ else
39
+ props['_t'] = Time.now.to_i.to_s
40
+ end
41
+
42
+ delay_query("e", props)
43
+ end
44
+
45
+ def alias(name, alias_to)
46
+ raise ArgumentError, "You must set an API key" if !@key
47
+ raise ArgumentError, "You must identify a user first" if !@id
48
+ p = {
49
+ '_n' => alias_to,
50
+ '_p' => name,
51
+ '_k' => @key,
52
+ }
53
+
54
+ delay_query("a", p)
55
+ end
56
+
57
+ def set(props = {})
58
+ raise ArgumentError, "You must set an API key" if !@key
59
+ raise ArgumentError, "You must identify a user first" if !@id
60
+ props = hash_keys_to_str(props)
61
+
62
+ props.update("_p" => @id)
63
+ props.update("_k" => @key)
64
+
65
+ if props["_t"]
66
+ props['_d'] = "1"
67
+ else
68
+ props['_t'] = Time.now.to_i.to_s
69
+ end
70
+ delay_query("s", props)
71
+ end
72
+
73
+ def get(url)
74
+ begin
75
+ return HTTParty.get(url)
76
+ rescue Exception => e
77
+ raise e.exception(e.message)
78
+ end
79
+ end
80
+
81
+ protected
82
+ #This is from the offical km.rb
83
+ def hash_keys_to_str(hash)
84
+ Hash[*hash.map { |k, v| k.class == Symbol ? [k.to_s, v] : [k, v] }.flatten] # convert all keys to strings
85
+ end
86
+
87
+ def delay_query(type, props)
88
+ params = []
89
+ props.each do |k, v|
90
+ params << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
91
+ end
92
+ query = params.join("&")
93
+ url = "http://#{KM.host}/#{type}?#{query}"
94
+ job = KMJob.new(url)
95
+ Rails.logger.debug url
96
+ if Rails.application.config.delayed_km
97
+ Delayed::Job.enqueue job
98
+ elsif Rails.application.config.delayed_km_debug
99
+ job.perform
100
+ end
101
+ end
102
+ end
103
+ end
data/lib/license.txt ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2011 Vincent Brendel, Dirk Elmendorf
3
+
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,186 @@
1
+ require 'spec_helper'
2
+ describe DKM do
3
+ it "should know the host to talk to" do
4
+ DKM.host.should == "trk.kissmetrics.com"
5
+ end
6
+ it "should set and know the api_key" do
7
+ DKM.init("DKM_KEY")
8
+ DKM.api_key.should == "DKM_KEY"
9
+ end
10
+ it "should set and know the identiy know the id" do
11
+ DKM.identify("BOB")
12
+ DKM.identity.should == "BOB"
13
+
14
+ end
15
+ it "should be able to make an http request" do
16
+ HTTParty.should_receive(:get).with("http://www.truckingoffice.com")
17
+ DKM.get("http://www.truckingoffice.com")
18
+
19
+ end
20
+ it "should trigger exception notifier if it is installed"
21
+ it "should raise an error if no key is provided" do
22
+ DKM.init(nil)
23
+ expect { DKM.record("") }.should raise_error(ArgumentError, "You must set an API key")
24
+ expect { DKM.set("") }.should raise_error(ArgumentError, "You must set an API key")
25
+ expect { DKM.alias("", "") }.should raise_error(ArgumentError, "You must set an API key")
26
+ end
27
+
28
+ it "should raise an error if no identity is set" do
29
+ DKM.init("DKM_KEY")
30
+ DKM.identify(nil)
31
+ expect { DKM.record("") }.should raise_error(ArgumentError, "You must identify a user first")
32
+ expect { DKM.set("") }.should raise_error(ArgumentError, "You must identify a user first")
33
+ expect { DKM.alias("", "") }.should raise_error(ArgumentError, "You must identify a user first")
34
+ end
35
+ context "record" do
36
+ before(:each) do
37
+ @now = Time.now
38
+ Time.stub!(:now).and_return(@now)
39
+ DKM.init("DKM_KEY")
40
+ DKM.identify("bob@aol.com")
41
+ @delay = mock
42
+ DKM.stub!(:delay).and_return(@delay)
43
+ end
44
+
45
+ def set_record_mock(action, args = {}, date = nil)
46
+ @delay.should_receive(:get) do |url|
47
+
48
+ expect { URI.parse(url) }.should_not raise_error
49
+ uri = URI.parse(url)
50
+ q = CGI.parse(uri.query)
51
+ uri.host.should == DKM.host
52
+ uri.path.should == "/e"
53
+ q["_k"].first.should == "DKM_KEY"
54
+ q["_p"].first.should == "bob@aol.com"
55
+ q["_n"].first.should == action
56
+ if date
57
+ q["_d"].first.should == "1"
58
+ q["_t"].first.should == date.to_i.to_s
59
+ else
60
+ q["_t"].first.should == @now.to_i.to_s
61
+ end
62
+ args.keys.each do |key|
63
+ q[key.to_s].first.should == args[key].to_s
64
+ end
65
+ end
66
+ end
67
+
68
+ it "should allow you to record an event" do
69
+ set_record_mock("Paid Us")
70
+ DKM.record("Paid Us")
71
+ end
72
+ it "should allow you to record an event for a time in the past" do
73
+ set_record_mock("Paid Us", {}, @now - 1.day)
74
+ DKM.record("Paid Us", {"_t" => (@now - 1.day).to_i.to_s})
75
+ end
76
+ it "should allow you to record an event with additional properties" do
77
+ set_record_mock("Paid Us", {"plan" => "Standard", "amount" => "25.00"})
78
+ DKM.record("Paid Us", {"plan" => "Standard", "amount" => "25.00"})
79
+ end
80
+ it "should allow you to record an event with additional properties that have special characters in the keys" do
81
+ set_record_mock("Paid Us", {"plan" => "Standard", "amount of $" => "25.00"})
82
+ DKM.record("Paid Us", {"plan" => "Standard", "amount of $" => "25.00"})
83
+ end
84
+ it "should allow you to record an event with additional properties for a time in the past" do
85
+ set_record_mock("Paid Us", {"plan" => "Standard", "amount" => "25.00"}, @now - 1.day)
86
+ DKM.record("Paid Us", {"plan" => "Standard", "amount" => "25.00", "_t" => (@now - 1.day).to_i.to_s})
87
+ end
88
+ it "should allow you to use symbols for keys on the additional properties" do
89
+ set_record_mock("Paid Us", {"plan" => "Standard", "amount" => "25.00"}, @now - 1.day)
90
+ DKM.record("Paid Us", {:plan => "Standard", :amount => "25.00", :_t => (@now - 1.day).to_i.to_s})
91
+ end
92
+ end
93
+ context "set" do
94
+ before(:each) do
95
+ @now = Time.now
96
+ Time.stub!(:now).and_return(@now)
97
+ DKM.init("DKM_KEY")
98
+ DKM.identify("bob@aol.com")
99
+ @delay = mock
100
+ DKM.stub!(:delay).and_return(@delay)
101
+ end
102
+
103
+ def set_set_mock(args = {}, date = nil)
104
+ @delay.should_receive(:get) do |url|
105
+ expect { URI.parse(url) }.should_not raise_error
106
+ uri = URI.parse(url)
107
+ q = CGI.parse(uri.query)
108
+ uri.host.should == DKM.host
109
+ uri.path.should == "/s"
110
+ q["_k"].first.should == "DKM_KEY"
111
+ q["_p"].first.should == "bob@aol.com"
112
+
113
+ if date
114
+ q["_d"].first.should == "1"
115
+ q["_t"].first.should == date.to_i.to_s
116
+ else
117
+ q["_t"].first.should == @now.to_i.to_s
118
+ end
119
+ args.keys.each do |key|
120
+ q[key.to_s].first.should == args[key].to_s
121
+ end
122
+ end
123
+ end
124
+
125
+ it "should allow you to set a property on a profile" do
126
+ set_set_mock({"plan" => "Standard"})
127
+ DKM.set({"plan" => "Standard"})
128
+ end
129
+ it "should allow you to set multiple properties on a profile" do
130
+ set_set_mock({"plan" => "Standard", "amount" => "25.00"})
131
+ DKM.set({"plan" => "Standard", "amount" => "25.00"})
132
+ end
133
+ it "should allow you to set properties on a profile with special characters" do
134
+ set_set_mock({"plan" => "Standard", "amount of $" => "25.00"})
135
+ DKM.set({"plan" => "Standard", "amount of $" => "25.00"})
136
+ end
137
+ it "should allow you to set a property on a profile for a time in the past" do
138
+
139
+ set_set_mock({"plan" => "Standard"}, @now - 1.day)
140
+ DKM.set({"plan" => "Standard", "_t" => (@now - 1.day).to_i.to_s})
141
+ end
142
+ it "should allow you to set multiple properties on a profile for a time in the past" do
143
+ set_set_mock({"plan" => "Standard", "amount" => "25.00"}, @now - 1.day)
144
+ DKM.set({"plan" => "Standard", "amount" => "25.00", "_t" => (@now - 1.day).to_i.to_s})
145
+ end
146
+ it "should allow you to use symbols for keys on properties" do
147
+ set_set_mock({"plan" => "Standard", "amount" => "25.00"}, @now - 1.day)
148
+ DKM.set({:plan => "Standard", :amount => "25.00", :_t => (@now - 1.day).to_i.to_s})
149
+ end
150
+ end
151
+
152
+ it "should allow you to alias on identity to another" do
153
+
154
+ end
155
+ context "identify" do
156
+ before(:each) do
157
+ @now = Time.now
158
+ Time.stub!(:now).and_return(@now)
159
+ DKM.init("DKM_KEY")
160
+ DKM.identify("bob@aol.com")
161
+ @delay = mock
162
+ DKM.stub!(:delay).and_return(@delay)
163
+ end
164
+
165
+ def set_identify_mock(old_name, new_name)
166
+
167
+ @delay.should_receive(:get) do |url|
168
+ expect { URI.parse(url) }.should_not raise_error
169
+ uri = URI.parse(url)
170
+
171
+ q = CGI.parse(uri.query)
172
+ uri.host.should == DKM.host
173
+ uri.path.should == "/a"
174
+ q["_k"].first.should == "DKM_KEY"
175
+ q["_p"].first.should == old_name
176
+ q["_n"].first.should == new_name
177
+ end
178
+ end
179
+
180
+ it "should allow you to alias a user" do
181
+ set_identify_mock("sam@aol.com", "will@aol.com")
182
+ DKM.alias("sam@aol.com", "will@aol.com")
183
+ end
184
+ end
185
+
186
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_km
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vincent Brendel, Dirk Elmendorf
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: &70349193944300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70349193944300
25
+ - !ruby/object:Gem::Dependency
26
+ name: delayed_job
27
+ requirement: &70349193943880 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70349193943880
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70349193943460 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70349193943460
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70349193965640 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70349193965640
58
+ description: Alternative for the Kissmetrics provided gem but using delayed_job instead
59
+ of cron. This will suit Heroku users.
60
+ email:
61
+ - jack.quack@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - delayed_km.gemspec
72
+ - lib/delayed_km.rb
73
+ - lib/delayed_km/version.rb
74
+ - lib/license.txt
75
+ - lib/spec/dkm_spec.rb
76
+ homepage: ''
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: -49468628806324911
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ segments:
98
+ - 0
99
+ hash: -49468628806324911
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.17
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Kissmetrics with delayed_job
106
+ test_files: []