event_hub_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,24 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ bundler/
24
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in event_hub_client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Cheng-Tao Chu
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,47 @@
1
+ # EventHubClient
2
+
3
+ The EventHubClient gem is a simple wrapper of EventHub HTTP APIs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'event_hub_client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install event_hub_client
18
+
19
+ ## Usage
20
+
21
+ By the nature of the gem is supposed be used from the backend, The gem only supports two apis, track and alias. More details of about EventHub can be found at https://github.com/Codecademy/EventHub/
22
+
23
+ ```ruby
24
+ event_hub_client = EventHubClient::EventHubClient.new(host, port, EventHubClient::Worker.new)
25
+ event_hub_client.track(event_type, user_id, event_properties)
26
+ event_hub_client.alias(from_id, to_id)
27
+ ```
28
+
29
+ If the throughput from EventHubClient::EventHubClient is not good enough, consider using EventHubClient::BatchEventHubClient which batch send the requests. Please notice that you need to explicitly flush the BatchEventHubClient when your service is shutting down or there might be events buffered in the queue and those events might not get sent to the event hub server.
30
+
31
+ ```ruby
32
+ base_event_hub_client = EventHubClient::EventHubClient.new(host, port, EventHubClient::Worker.new)
33
+ queue = []
34
+ batch_size = 10
35
+ batch_event_hub_client = EventHubClient::BatchEventHubClient.new(base_event_hub_client, queue, batch_size)
36
+ event_hub_client.track(event_type, user_id, event_properties)
37
+ event_hub_client.alias(from_id, to_id)
38
+ event_hub_client.flush
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/Codecademy/EventHubClient/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'event_hub_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "event_hub_client"
8
+ spec.version = EventHubClient::VERSION
9
+ spec.authors = ["Cheng-Tao Chu"]
10
+ spec.email = ["chengtao@codecademy.com"]
11
+ spec.summary = %q{Client for EventHub.}
12
+ spec.description = %q{More details of EventHub available at http://github.com/Codecademy/EventHub.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "typhoeus"
25
+ end
@@ -0,0 +1,3 @@
1
+ module EventHubClient
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,78 @@
1
+ require "event_hub_client/version"
2
+ require "net/http"
3
+ require "typhoeus"
4
+ require 'json'
5
+
6
+ module EventHubClient
7
+ class Worker
8
+ def perform(url, body)
9
+ Typhoeus::Request.post(url, body: body)
10
+ end
11
+ end
12
+
13
+ class BatchEventHubClient
14
+ attr_reader :client, :queue, :batch_size
15
+
16
+ def initialize(client, queue, batch_size)
17
+ @client = client
18
+ @queue = queue
19
+ @batch_size = batch_size
20
+ end
21
+
22
+ def track(event_type, user_id, event_properties)
23
+ params = {}.merge(event_properties).merge({
24
+ "event_type" => event_type,
25
+ "external_user_id" => user_id,
26
+ })
27
+ queue.push(params)
28
+ if queue.size % batch_size == 0
29
+ client.send_request("events/batch_track", { "events" => queue.to_json })
30
+ @queue = []
31
+ end
32
+ end
33
+
34
+ def flush
35
+ client.send_request("events/batch_track", { "events" => queue.to_json })
36
+ @queue = []
37
+ end
38
+
39
+ def alias(from_id, to_id)
40
+ client.alias(from_id, to_id)
41
+ end
42
+ end
43
+
44
+ class EventHubClient
45
+ attr_reader :host, :port, :worker
46
+
47
+ def initialize(host, port, worker)
48
+ @host = host
49
+ @port = port
50
+ @worker = worker
51
+ end
52
+
53
+ def track(event_type, user_id, event_properties)
54
+ path = "events/track"
55
+
56
+ params = {}.merge(event_properties).merge({
57
+ "event_type" => event_type,
58
+ "external_user_id" => user_id,
59
+ })
60
+
61
+ send_request(path, params)
62
+ end
63
+
64
+ def alias(from_id, to_id)
65
+ path = "users/alias"
66
+ params = {
67
+ "from_external_user_id" => from_id,
68
+ "to_external_user_id" => to_id
69
+ }
70
+
71
+ send_request(path, params)
72
+ end
73
+
74
+ def send_request(path, body)
75
+ worker.perform("http://#{@host}:#{@port}/#{path}", body)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,112 @@
1
+ require "spec_helper"
2
+
3
+ describe EventHubClient do
4
+ let(:host) { "localhost" }
5
+ let(:port) { 3000 }
6
+
7
+ context "for BatchEventHubClient" do
8
+ let(:queue) { Array.new }
9
+ let(:batch_size) { 2 }
10
+ let(:event_hub_client) {
11
+ EventHubClient::BatchEventHubClient.new(
12
+ EventHubClient::EventHubClient.new(host, port, EventHubClient::Worker.new), queue, batch_size)
13
+ }
14
+
15
+ it "send direct http request when alias" do
16
+ from_id = "hello"
17
+ to_id = "bar"
18
+
19
+ expect(Typhoeus::Request).to receive(:post).with("http://#{host}:#{port}/users/alias", {
20
+ :body => {
21
+ "from_external_user_id" => from_id,
22
+ "to_external_user_id" => to_id
23
+ }}).once
24
+ event_hub_client.alias(from_id, to_id)
25
+ end
26
+
27
+ it "batch track requests" do
28
+ event_types = [ "TYPE1", "TYPE2", "TYPE3", "TYPE4", "TYPE5" ]
29
+ user_ids = [ "foo1", "foo2", "foo3", "foo4", "foo5" ]
30
+ event_properties = [
31
+ { "foo1" => "bar1" },
32
+ { "foo2" => "bar2" },
33
+ { "foo3" => "bar3" },
34
+ { "foo4" => "bar4" },
35
+ { "foo5" => "bar5" },
36
+ ]
37
+
38
+ expect(Typhoeus::Request).to receive(:post).with("http://#{host}:#{port}/events/batch_track", {
39
+ :body => {
40
+ "events" => [
41
+ {
42
+ "foo1" => "bar1",
43
+ "event_type" => event_types[0],
44
+ "external_user_id" => user_ids[0],
45
+ }, {
46
+ "foo2" => "bar2",
47
+ "event_type" => event_types[1],
48
+ "external_user_id" => user_ids[1],
49
+ }].to_json
50
+ }}).once
51
+ expect(Typhoeus::Request).to receive(:post).with("http://#{host}:#{port}/events/batch_track", {
52
+ :body => {
53
+ "events" => [
54
+ {
55
+ "foo3" => "bar3",
56
+ "event_type" => event_types[2],
57
+ "external_user_id" => user_ids[2],
58
+ }, {
59
+ "foo4" => "bar4",
60
+ "event_type" => event_types[3],
61
+ "external_user_id" => user_ids[3],
62
+ }].to_json
63
+ }}).once
64
+ expect(Typhoeus::Request).to receive(:post).with("http://#{host}:#{port}/events/batch_track", {
65
+ :body => {
66
+ "events" => [
67
+ {
68
+ "foo5" => "bar5",
69
+ "event_type" => event_types[4],
70
+ "external_user_id" => user_ids[4],
71
+ }].to_json
72
+ }}).once
73
+
74
+ (0..4).each do |i|
75
+ event_hub_client.track(event_types[i], user_ids[i], event_properties[i])
76
+ end
77
+ event_hub_client.flush
78
+ end
79
+ end
80
+
81
+ context "for EventHubClient" do
82
+ let(:event_hub_client) {
83
+ EventHubClient::EventHubClient.new(host, port, EventHubClient::Worker.new)
84
+ }
85
+
86
+ it "send direct http request when alias" do
87
+ from_id = "hello"
88
+ to_id = "bar"
89
+
90
+ expect(Typhoeus::Request).to receive(:post).with("http://#{host}:#{port}/users/alias", {
91
+ :body => {
92
+ "from_external_user_id" => from_id,
93
+ "to_external_user_id" => to_id
94
+ }}).once
95
+ event_hub_client.alias(from_id, to_id)
96
+ end
97
+
98
+ it "send direct http request when track" do
99
+ event_type = "TYPE"
100
+ user_id = "chengtao@codeecademy.com"
101
+ event_properties = { "foo" => "bar" }
102
+
103
+ expect(Typhoeus::Request).to receive(:post).with("http://#{host}:#{port}/events/track", {
104
+ :body => {
105
+ "event_type" => event_type,
106
+ "external_user_id" => user_id,
107
+ "foo" => "bar"
108
+ }}).once
109
+ event_hub_client.track(event_type, user_id, event_properties)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'event_hub_client'
5
+
6
+ RSpec.configure do |config|
7
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_hub_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cheng-Tao Chu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.6'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.6'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: typhoeus
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: More details of EventHub available at http://github.com/Codecademy/EventHub.
79
+ email:
80
+ - chengtao@codecademy.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - event_hub_client.gemspec
91
+ - lib/event_hub_client.rb
92
+ - lib/event_hub_client/version.rb
93
+ - spec/event_hub_client_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: ''
96
+ licenses:
97
+ - MIT
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ segments:
109
+ - 0
110
+ hash: -3766524776493520849
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ segments:
118
+ - 0
119
+ hash: -3766524776493520849
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.23
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Client for EventHub.
126
+ test_files:
127
+ - spec/event_hub_client_spec.rb
128
+ - spec/spec_helper.rb