km_resque 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .rspec
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in km_resque.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Luke Melia
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,51 @@
1
+ # KmResque
2
+
3
+ An interface for interacting with the KISSmetrics API via Resque. Keeps all direct interactions with the KISSMetrics API out of your requests.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/lukemelia/km_resque.png)](http://travis-ci.org/lukemelia/km_resque)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'km_resque'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install km_resque
20
+
21
+ Configure your API key:
22
+
23
+ KmResque.configure do |config|
24
+ config.key = '<YOUR-KISSMETRICS-API-KEY>'
25
+ end
26
+
27
+ ## Usage
28
+
29
+ KmResque.alias(anonymous_id, user.id)
30
+ KmResque.record(user.id, 'signed_up', { :source => 'contest' })
31
+ KmResque.set(user.id, { :gender => 'F' })
32
+
33
+ ## Running specs
34
+
35
+ $ bundle exec rspec spec/
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Added some feature'`); don't forget the specs!
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
44
+
45
+ ## Credits
46
+
47
+ Written by Luke Melia. Thanks to Yapp for open sourcing KMResque. Inspiration from delayed_kiss and km-delay.
48
+
49
+ ## License
50
+
51
+ km_resque is available under the terms of the MIT License http://www.opensource.org/licenses/mit-license.php
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/km-resque.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/km_resque/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Luke Melia"]
6
+ gem.email = ["luke@lukemelia.com"]
7
+ gem.description = %q{Interact with the KISSMetrics API via Resque}
8
+ gem.summary = %q{Interact with the KISSMetrics API via Resque}
9
+ gem.homepage = "https://github.com/lukemelia/km_resque"
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 = "km_resque"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = KmResque::VERSION
17
+
18
+ gem.add_dependency 'resque', '>= 1.1.0'
19
+ gem.add_development_dependency 'resque_spec'
20
+ gem.add_development_dependency 'webmock'
21
+ end
@@ -0,0 +1,11 @@
1
+ require 'km_resque/api_client'
2
+
3
+ class KmResque
4
+ class AliasJob
5
+ @queue = :km
6
+
7
+ def self.perform(identifier1, identifier2, timestamp)
8
+ ApiClient.new.alias(identifier1, identifier2, timestamp)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,66 @@
1
+ require 'km_resque'
2
+
3
+ class KmResque
4
+ class ApiClient
5
+ def alias(identifier1, identifier2, timestamp)
6
+ hit('a', { '_n' => identifier2,
7
+ '_p' => identifier1,
8
+ '_t' => timestamp
9
+ })
10
+ end
11
+
12
+ def record(identifier, event_name, properties, timestamp)
13
+ hit('e', { '_p' => identifier,
14
+ '_n' => event_name,
15
+ '_t' => timestamp
16
+ }.merge(properties || {}))
17
+ end
18
+
19
+ def set(identifier, properties, timestamp)
20
+ hit('s', { '_p' => identifier,
21
+ '_t' => timestamp
22
+ }.merge(properties))
23
+ end
24
+
25
+ def api_key
26
+ @api_key ||= KmResque.configuration.key
27
+ end
28
+
29
+ def host
30
+ @host ||= KmResque.configuration.host
31
+ end
32
+
33
+ def port
34
+ @port ||= KmResque.configuration.port
35
+ end
36
+
37
+ private
38
+
39
+ def hit(type, data)
40
+ unless data['_p']
41
+ raise Error.new("Can't hit the API without an identity")
42
+ end
43
+
44
+ data['_k'] = api_key
45
+ unless data['_k']
46
+ raise Error.new("Can't hit the API without an API Key")
47
+ end
48
+
49
+ data['_d'] = 1 if data['_t']
50
+ data['_t'] ||= Time.now.to_i
51
+
52
+ unsafe = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}]", false, 'N')
53
+
54
+ query_parts = []
55
+ query = ''
56
+ data.inject(query) do |query, key_val|
57
+ query_parts << key_val.collect { |i| URI.escape(i.to_s, unsafe) }.join('=')
58
+ end
59
+ query = '/' + type + '?' + query_parts.join('&')
60
+ proxy = URI.parse(ENV['http_proxy'] || ENV['HTTP_PROXY'] || '')
61
+ res = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).start(host, port) do |http|
62
+ http.get(query)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,15 @@
1
+ class KmResque
2
+ class Configuration
3
+ attr_accessor :key
4
+
5
+ attr_writer :host
6
+ def host
7
+ @host ||= 'trk.kissmetrics.com'
8
+ end
9
+
10
+ attr_writer :port
11
+ def port
12
+ @port ||= 80
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'km_resque/api_client'
2
+
3
+ class KmResque
4
+ class RecordJob
5
+ @queue = :km
6
+
7
+ def self.perform(identifier, event_name, properties, timestamp)
8
+ ApiClient.new.record(identifier, event_name, properties, timestamp)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'km_resque/api_client'
2
+
3
+ class KmResque
4
+ class SetJob
5
+ @queue = :km
6
+
7
+ def self.perform(identifier, properties, timestamp)
8
+ ApiClient.new.set(identifier, properties, timestamp)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ class KmResque
2
+ VERSION = "1.0.0"
3
+ end
data/lib/km_resque.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "km_resque/version"
2
+ require "km_resque/configuration"
3
+ require "km_resque/alias_job"
4
+ require "km_resque/set_job"
5
+ require "km_resque/record_job"
6
+
7
+ class KmResque
8
+ class Error < RuntimeError; end
9
+ def self.configure(&block)
10
+ yield configuration
11
+ end
12
+ def self.configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+ def self.alias(identifier1, identifier2)
16
+ ::Resque.enqueue(AliasJob, identifier1, identifier2, Time.now.to_i)
17
+ end
18
+ def self.set(identifier, properties)
19
+ ::Resque.enqueue(SetJob, identifier, properties, Time.now.to_i)
20
+ end
21
+ def self.record(identifier, eventName, properties)
22
+ ::Resque.enqueue(RecordJob, identifier, eventName, properties, Time.now.to_i)
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ $:.push File.expand_path('../../lib', __FILE__)
2
+ require 'webmock/rspec'
3
+ require 'km_resque/alias_job'
4
+
5
+ describe "KmResque::AliasJob" do
6
+ before(:each) do
7
+ stub_request(:any, %r{http://trk.kissmetrics.com.*})
8
+ KmResque.configure do |config|
9
+ config.key = "abc123"
10
+ end
11
+ end
12
+ describe "perform" do
13
+ it "should hit the KM API" do
14
+ timestamp = Time.now.to_i
15
+ KmResque::AliasJob.perform("identifier1", "identifier2", timestamp)
16
+ expected_api_hit = "http://trk.kissmetrics.com/a?_d=1&_k=abc123&_n=identifier2&_p=identifier1&_t=#{timestamp}"
17
+ WebMock.should have_requested(:get, expected_api_hit)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ $:.push File.expand_path('../../lib', __FILE__)
2
+ require 'webmock/rspec'
3
+ require 'km_resque/record_job'
4
+
5
+ describe "KmResque::RecordJob" do
6
+ before(:each) do
7
+ stub_request(:any, %r{http://trk.kissmetrics.com.*})
8
+ KmResque.configure do |config|
9
+ config.key = "abc123"
10
+ end
11
+ end
12
+ describe "perform" do
13
+ it "should hit the KM API" do
14
+ timestamp = Time.now.to_i
15
+ KmResque::RecordJob.perform("identifier", "eventName", { :foo => 'bar', :baz => 'bay'}, timestamp)
16
+ expected_api_hit = "http://trk.kissmetrics.com/e?_d=1&_k=abc123&_n=eventName&_p=identifier&_t=#{timestamp}&baz=bay&foo=bar"
17
+ WebMock.should have_requested(:get, expected_api_hit)
18
+ end
19
+ it "should succceed with no properties" do
20
+ timestamp = Time.now.to_i
21
+ KmResque::RecordJob.perform("identifier", "eventName", nil, timestamp)
22
+ expected_api_hit = "http://trk.kissmetrics.com/e?_d=1&_k=abc123&_n=eventName&_p=identifier&_t=#{timestamp}"
23
+ WebMock.should have_requested(:get, expected_api_hit)
24
+ end
25
+ it "should raise an error when no identity is provided" do
26
+ timestamp = Time.now.to_i
27
+ lambda {
28
+ KmResque::RecordJob.perform(nil, "eventName", { :foo => 'bar', :baz => 'bay'}, timestamp)
29
+ }.should raise_error(KmResque::Error)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'km_resque'
3
+ require 'resque_spec'
4
+
5
+ describe "KmResque" do
6
+ before(:each) do
7
+ ResqueSpec.reset!
8
+ Time.stub!(:now => Time.now)
9
+ @timestamp = Time.now.to_i
10
+ end
11
+ describe "configuring" do
12
+ it "should capture the API key" do
13
+ KmResque.configure do |config|
14
+ config.key = "foo"
15
+ end
16
+ KmResque.configuration.key.should == "foo"
17
+ end
18
+ end
19
+ describe "alias" do
20
+ it "should queue an AliasJob" do
21
+ KmResque.alias("identifier1", "identifier2")
22
+ KmResque::AliasJob.should have_queued("identifier1",
23
+ "identifier2",
24
+ @timestamp
25
+ ).in(:km)
26
+ end
27
+ end
28
+ describe "set" do
29
+ it "should queue an SetJob" do
30
+ KmResque.set("identifier", {:some_prop => 'some_val'})
31
+ KmResque::SetJob.should have_queued("identifier",
32
+ {:some_prop => 'some_val'},
33
+ @timestamp
34
+ ).in(:km)
35
+ end
36
+ end
37
+ describe "record" do
38
+ it "should queue an RecordJob" do
39
+ KmResque.record("identifier", "myEventName", {:some_prop => 'some_val'})
40
+ KmResque::RecordJob.should have_queued("identifier",
41
+ "myEventName",
42
+ {:some_prop => 'some_val'},
43
+ @timestamp
44
+ ).in(:km)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,20 @@
1
+ $:.push File.expand_path('../../lib', __FILE__)
2
+ require 'webmock/rspec'
3
+ require 'km_resque/set_job'
4
+
5
+ describe "KmResque::SetJob" do
6
+ before(:each) do
7
+ stub_request(:any, %r{http://trk.kissmetrics.com.*})
8
+ KmResque.configure do |config|
9
+ config.key = "abc123"
10
+ end
11
+ end
12
+ describe "perform" do
13
+ it "should hit the KM API" do
14
+ timestamp = Time.now.to_i
15
+ KmResque::SetJob.perform("identifier", { :foo => 'bar', :baz => 'bay'}, timestamp)
16
+ expected_api_hit = "http://trk.kissmetrics.com/s?_d=1&_k=abc123&_p=identifier&_t=#{timestamp}&baz=bay&foo=bar"
17
+ WebMock.should have_requested(:get, expected_api_hit)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,47 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'km_resque'
3
+ require 'resque_spec'
4
+
5
+ describe "KmResque" do
6
+ before(:each) do
7
+ ResqueSpec.reset!
8
+ Time.stub!(:now => Time.now)
9
+ @timestamp = Time.now.to_i
10
+ end
11
+ describe "configuring" do
12
+ it "should capture the API key" do
13
+ KmResque.configure do |config|
14
+ config.key = "foo"
15
+ end
16
+ KmResque.configuration.key.should == "foo"
17
+ end
18
+ end
19
+ describe "alias" do
20
+ it "should queue an AliasJob" do
21
+ KmResque.alias("identifier1", "identifier2")
22
+ KmResque::AliasJob.should have_queued("identifier1",
23
+ "identifier2",
24
+ @timestamp
25
+ ).in(:km)
26
+ end
27
+ end
28
+ describe "set" do
29
+ it "should queue an SetJob" do
30
+ KmResque.set("identifier", {:some_prop => 'some_val'})
31
+ KmResque::SetJob.should have_queued("identifier",
32
+ {:some_prop => 'some_val'},
33
+ @timestamp
34
+ ).in(:km)
35
+ end
36
+ end
37
+ describe "record" do
38
+ it "should queue an RecordJob" do
39
+ KmResque.record("identifier", "myEventName", {:some_prop => 'some_val'})
40
+ KmResque::RecordJob.should have_queued("identifier",
41
+ "myEventName",
42
+ {:some_prop => 'some_val'},
43
+ @timestamp
44
+ ).in(:km)
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: km_resque
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Luke Melia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-09-20 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: resque
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.0
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: resque_spec
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: webmock
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id003
48
+ description: Interact with the KISSMetrics API via Resque
49
+ email:
50
+ - luke@lukemelia.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - .travis.yml
60
+ - Gemfile
61
+ - LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - km-resque.gemspec
65
+ - lib/km_resque.rb
66
+ - lib/km_resque/alias_job.rb
67
+ - lib/km_resque/api_client.rb
68
+ - lib/km_resque/configuration.rb
69
+ - lib/km_resque/record_job.rb
70
+ - lib/km_resque/set_job.rb
71
+ - lib/km_resque/version.rb
72
+ - spec/lib/km_resque/alias_job_spec.rb
73
+ - spec/lib/km_resque/record_job_spec.rb
74
+ - spec/lib/km_resque/resque_spec.rb
75
+ - spec/lib/km_resque/set_job_spec.rb
76
+ - spec/lib/km_resque_spec.rb
77
+ homepage: https://github.com/lukemelia/km_resque
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.10
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Interact with the KISSMetrics API via Resque
104
+ test_files:
105
+ - spec/lib/km_resque/alias_job_spec.rb
106
+ - spec/lib/km_resque/record_job_spec.rb
107
+ - spec/lib/km_resque/resque_spec.rb
108
+ - spec/lib/km_resque/set_job_spec.rb
109
+ - spec/lib/km_resque_spec.rb