km-resque 0.9.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/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,49 @@
1
+ # KM-Resque
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
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'km-resque'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install km-resque
18
+
19
+ Configure your API key:
20
+
21
+ KM::Resque.configure do |config|
22
+ config.key = '<YOUR-KISSMETRICS-API-KEY>'
23
+ end
24
+
25
+ ## Usage
26
+
27
+ KM::Resque.alias(anonymous_id, user.id)
28
+ KM::Resque.record(user.id, 'signed_up', { :source => 'contest' })
29
+ KM::Resque.set(user.id, { :gender => 'F' })
30
+
31
+ ## Running specs
32
+
33
+ $ bundle exec rspec spec/
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Added some feature'`); don't forget the specs!
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
42
+
43
+ ## Credits
44
+
45
+ Written by Luke Melia. Thanks to Yapp for open sourcing KMResque. Inspiration from delayed_kiss and km-delay.
46
+
47
+ ## License
48
+
49
+ 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 = KM::Resque::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,13 @@
1
+ require 'km/resque/api_client'
2
+
3
+ module KM
4
+ class Resque
5
+ class AliasJob
6
+ @queue = :km
7
+
8
+ def self.perform(identifier1, identifier2, timestamp)
9
+ ApiClient.new.alias(identifier1, identifier2, timestamp)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,68 @@
1
+ require 'km/resque'
2
+
3
+ module KM
4
+ class Resque
5
+ class ApiClient
6
+ def alias(identifier1, identifier2, timestamp)
7
+ hit('a', { '_n' => identifier2,
8
+ '_p' => identifier1,
9
+ '_t' => timestamp
10
+ })
11
+ end
12
+
13
+ def record(identifier, event_name, properties, timestamp)
14
+ hit('e', { '_p' => identifier,
15
+ '_n' => event_name,
16
+ '_t' => timestamp
17
+ }.merge(properties || {}))
18
+ end
19
+
20
+ def set(identifier, properties, timestamp)
21
+ hit('s', { '_p' => identifier,
22
+ '_t' => timestamp
23
+ }.merge(properties))
24
+ end
25
+
26
+ def api_key
27
+ @api_key ||= KM::Resque.configuration.key
28
+ end
29
+
30
+ def host
31
+ @host ||= KM::Resque.configuration.host
32
+ end
33
+
34
+ def port
35
+ @port ||= KM::Resque.configuration.port
36
+ end
37
+
38
+ private
39
+
40
+ def hit(type, data)
41
+ unless data['_p']
42
+ raise Error.new("Can't hit the API without an identity")
43
+ end
44
+
45
+ data['_k'] = api_key
46
+ unless data['_k']
47
+ raise Error.new("Can't hit the API without an API Key")
48
+ end
49
+
50
+ data['_d'] = 1 if data['_t']
51
+ data['_t'] ||= Time.now.to_i
52
+
53
+ unsafe = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}]", false, 'N')
54
+
55
+ query_parts = []
56
+ query = ''
57
+ data.inject(query) do |query, key_val|
58
+ query_parts << key_val.collect { |i| URI.escape(i.to_s, unsafe) }.join('=')
59
+ end
60
+ query = '/' + type + '?' + query_parts.join('&')
61
+ proxy = URI.parse(ENV['http_proxy'] || ENV['HTTP_PROXY'] || '')
62
+ res = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).start(host, port) do |http|
63
+ http.get(query)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,17 @@
1
+ module KM
2
+ class Resque
3
+ class Configuration
4
+ attr_accessor :key
5
+
6
+ attr_writer :host
7
+ def host
8
+ @host ||= 'trk.kissmetrics.com'
9
+ end
10
+
11
+ attr_writer :port
12
+ def port
13
+ @port ||= 80
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ require 'km/resque/api_client'
2
+
3
+ module KM
4
+ class Resque
5
+ class RecordJob
6
+ @queue = :km
7
+
8
+ def self.perform(identifier, event_name, properties, timestamp)
9
+ ApiClient.new.record(identifier, event_name, properties, timestamp)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'km/resque/api_client'
2
+
3
+ module KM
4
+ class Resque
5
+ class SetJob
6
+ @queue = :km
7
+
8
+ def self.perform(identifier, properties, timestamp)
9
+ ApiClient.new.set(identifier, properties, timestamp)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module KM
2
+ class Resque
3
+ VERSION = "0.9.0"
4
+ end
5
+ end
data/lib/km/resque.rb ADDED
@@ -0,0 +1,25 @@
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
+ module KM
8
+ class Resque
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
25
+ 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 "KM::Resque::AliasJob" do
6
+ before(:each) do
7
+ stub_request(:any, %r{http://trk.kissmetrics.com.*})
8
+ KM::Resque.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
+ KM::Resque::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,26 @@
1
+ $:.push File.expand_path('../../lib', __FILE__)
2
+ require 'webmock/rspec'
3
+ require 'km/resque/record_job'
4
+
5
+ describe "KM::Resque::RecordJob" do
6
+ before(:each) do
7
+ stub_request(:any, %r{http://trk.kissmetrics.com.*})
8
+ KM::Resque.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
+ KM::Resque::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
+ KM::Resque::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
+ end
26
+ 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 "KM::Resque::SetJob" do
6
+ before(:each) do
7
+ stub_request(:any, %r{http://trk.kissmetrics.com.*})
8
+ KM::Resque.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
+ KM::Resque::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 "KM::Resque" 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
+ KM::Resque.configure do |config|
14
+ config.key = "foo"
15
+ end
16
+ KM::Resque.configuration.key.should == "foo"
17
+ end
18
+ end
19
+ describe "alias" do
20
+ it "should queue an AliasJob" do
21
+ KM::Resque.alias("identifier1", "identifier2")
22
+ KM::Resque::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
+ KM::Resque.set("identifier", {:some_prop => 'some_val'})
31
+ KM::Resque::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
+ KM::Resque.record("identifier", "myEventName", {:some_prop => 'some_val'})
40
+ KM::Resque::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,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: km-resque
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.9.0
6
+ platform: ruby
7
+ authors:
8
+ - Luke Melia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-09-12 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
+ - Gemfile
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - km-resque.gemspec
64
+ - lib/km/resque.rb
65
+ - lib/km/resque/alias_job.rb
66
+ - lib/km/resque/api_client.rb
67
+ - lib/km/resque/configuration.rb
68
+ - lib/km/resque/record_job.rb
69
+ - lib/km/resque/set_job.rb
70
+ - lib/km/resque/version.rb
71
+ - spec/lib/km/resque/alias_job_spec.rb
72
+ - spec/lib/km/resque/record_job_spec.rb
73
+ - spec/lib/km/resque/set_job_spec.rb
74
+ - spec/lib/km/resque_spec.rb
75
+ homepage: https://github.com/lukemelia/km-resque
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
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
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.10
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Interact with the KISSMetrics API via Resque
102
+ test_files:
103
+ - spec/lib/km/resque/alias_job_spec.rb
104
+ - spec/lib/km/resque/record_job_spec.rb
105
+ - spec/lib/km/resque/set_job_spec.rb
106
+ - spec/lib/km/resque_spec.rb