analytics-ruby 2.0.13 → 2.4.0

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.
@@ -1,291 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Segment
4
- class Analytics
5
- describe Client do
6
- describe '#initialize' do
7
- it 'should error if no write_key is supplied' do
8
- expect { Client.new }.to raise_error(ArgumentError)
9
- end
10
-
11
- it 'should not error if a write_key is supplied' do
12
- Client.new :write_key => WRITE_KEY
13
- end
14
-
15
- it 'should not error if a write_key is supplied as a string' do
16
- Client.new 'write_key' => WRITE_KEY
17
- end
18
- end
19
-
20
- describe '#track' do
21
- before(:all) do
22
- @client = Client.new :write_key => WRITE_KEY
23
- @queue = @client.instance_variable_get :@queue
24
- end
25
-
26
- it 'should error without an event' do
27
- expect { @client.track(:user_id => 'user') }.to raise_error(ArgumentError)
28
- end
29
-
30
- it 'should error without a user_id' do
31
- expect { @client.track(:event => 'Event') }.to raise_error(ArgumentError)
32
- end
33
-
34
- it 'should error if properties is not a hash' do
35
- expect {
36
- @client.track({
37
- :user_id => 'user',
38
- :event => 'Event',
39
- :properties => [1,2,3]
40
- })
41
- }.to raise_error(ArgumentError)
42
- end
43
-
44
- it 'should use the timestamp given' do
45
- time = Time.parse("1990-07-16 13:30:00.123 UTC")
46
-
47
- @client.track({
48
- :event => 'testing the timestamp',
49
- :user_id => 'joe',
50
- :timestamp => time
51
- })
52
-
53
- msg = @queue.pop
54
-
55
- Time.parse(msg[:timestamp]).should == time
56
- end
57
-
58
- it 'should not error with the required options' do
59
- @client.track Queued::TRACK
60
- @queue.pop
61
- end
62
-
63
- it 'should not error when given string keys' do
64
- @client.track Utils.stringify_keys(Queued::TRACK)
65
- @queue.pop
66
- end
67
-
68
- it 'should convert time and date traits into iso8601 format' do
69
- @client.track({
70
- :user_id => 'user',
71
- :event => 'Event',
72
- :properties => {
73
- :time => Time.utc(2013),
74
- :time_with_zone => Time.zone.parse('2013-01-01'),
75
- :date_time => DateTime.new(2013,1,1),
76
- :date => Date.new(2013,1,1),
77
- :nottime => 'x'
78
- }
79
- })
80
- message = @queue.pop
81
- message[:properties][:time].should == '2013-01-01T00:00:00.000Z'
82
- message[:properties][:time_with_zone].should == '2013-01-01T00:00:00.000Z'
83
- message[:properties][:date_time].should == '2013-01-01T00:00:00.000Z'
84
- message[:properties][:date].should == '2013-01-01'
85
- message[:properties][:nottime].should == 'x'
86
- end
87
- end
88
-
89
-
90
- describe '#identify' do
91
- before(:all) do
92
- @client = Client.new :write_key => WRITE_KEY
93
- @queue = @client.instance_variable_get :@queue
94
- end
95
-
96
- it 'should error without any user id' do
97
- expect { @client.identify({}) }.to raise_error(ArgumentError)
98
- end
99
-
100
- it 'should not error with the required options' do
101
- @client.identify Queued::IDENTIFY
102
- @queue.pop
103
- end
104
-
105
- it 'should not error with the required options as strings' do
106
- @client.identify Utils.stringify_keys(Queued::IDENTIFY)
107
- @queue.pop
108
- end
109
-
110
- it 'should convert time and date traits into iso8601 format' do
111
- @client.identify({
112
- :user_id => 'user',
113
- :traits => {
114
- :time => Time.utc(2013),
115
- :time_with_zone => Time.zone.parse('2013-01-01'),
116
- :date_time => DateTime.new(2013,1,1),
117
- :date => Date.new(2013,1,1),
118
- :nottime => 'x'
119
- }
120
- })
121
- message = @queue.pop
122
- message[:traits][:time].should == '2013-01-01T00:00:00.000Z'
123
- message[:traits][:time_with_zone].should == '2013-01-01T00:00:00.000Z'
124
- message[:traits][:date_time].should == '2013-01-01T00:00:00.000Z'
125
- message[:traits][:date].should == '2013-01-01'
126
- message[:traits][:nottime].should == 'x'
127
- end
128
- end
129
-
130
- describe '#alias' do
131
- before :all do
132
- @client = Client.new :write_key => WRITE_KEY
133
- end
134
-
135
- it 'should error without from' do
136
- expect { @client.alias :user_id => 1234 }.to raise_error(ArgumentError)
137
- end
138
-
139
- it 'should error without to' do
140
- expect { @client.alias :previous_id => 1234 }.to raise_error(ArgumentError)
141
- end
142
-
143
- it 'should not error with the required options' do
144
- @client.alias ALIAS
145
- end
146
-
147
- it 'should not error with the required options as strings' do
148
- @client.alias Utils.stringify_keys(ALIAS)
149
- end
150
- end
151
-
152
- describe '#group' do
153
- before :all do
154
- @client = Client.new :write_key => WRITE_KEY
155
- @queue = @client.instance_variable_get :@queue
156
- end
157
-
158
- after :each do
159
- @client.flush
160
- end
161
-
162
- it 'should error without group_id' do
163
- expect { @client.group :user_id => 'foo' }.to raise_error(ArgumentError)
164
- end
165
-
166
- it 'should error without user_id' do
167
- expect { @client.group :group_id => 'foo' }.to raise_error(ArgumentError)
168
- end
169
-
170
- it 'should not error with the required options' do
171
- @client.group Queued::GROUP
172
- end
173
-
174
- it 'should not error with the required options as strings' do
175
- @client.group Utils.stringify_keys(Queued::GROUP)
176
- end
177
-
178
- it 'should convert time and date traits into iso8601 format' do
179
- @client.identify({
180
- :user_id => 'user',
181
- :group_id => 'group',
182
- :traits => {
183
- :time => Time.utc(2013),
184
- :time_with_zone => Time.zone.parse('2013-01-01'),
185
- :date_time => DateTime.new(2013,1,1),
186
- :date => Date.new(2013,1,1),
187
- :nottime => 'x'
188
- }
189
- })
190
- message = @queue.pop
191
- message[:traits][:time].should == '2013-01-01T00:00:00.000Z'
192
- message[:traits][:time_with_zone].should == '2013-01-01T00:00:00.000Z'
193
- message[:traits][:date_time].should == '2013-01-01T00:00:00.000Z'
194
- message[:traits][:date].should == '2013-01-01'
195
- message[:traits][:nottime].should == 'x'
196
- end
197
- end
198
-
199
- describe '#page' do
200
- before :all do
201
- @client = Client.new :write_key => WRITE_KEY
202
- end
203
-
204
- it 'should error without user_id' do
205
- expect { @client.page :name => 'foo' }.to raise_error(ArgumentError)
206
- end
207
-
208
- it 'should not error with the required options' do
209
- @client.page Queued::PAGE
210
- end
211
-
212
- it 'should not error with the required options as strings' do
213
- @client.page Utils.stringify_keys(Queued::PAGE)
214
- end
215
- end
216
-
217
- describe '#screen' do
218
- before :all do
219
- @client = Client.new :write_key => WRITE_KEY
220
- end
221
-
222
- it 'should error without user_id' do
223
- expect { @client.screen :name => 'foo' }.to raise_error(ArgumentError)
224
- end
225
-
226
- it 'should not error with the required options' do
227
- @client.screen Queued::SCREEN
228
- end
229
-
230
- it 'should not error with the required options as strings' do
231
- @client.screen Utils.stringify_keys(Queued::SCREEN)
232
- end
233
- end
234
-
235
- describe '#flush' do
236
- before(:all) do
237
- @client = Client.new :write_key => WRITE_KEY
238
- end
239
-
240
- it 'should wait for the queue to finish on a flush' do
241
- @client.identify Queued::IDENTIFY
242
- @client.track Queued::TRACK
243
- @client.flush
244
- @client.queued_messages.should == 0
245
- end
246
-
247
- it 'should complete when the process forks' do
248
- @client.identify Queued::IDENTIFY
249
-
250
- Process.fork do
251
- @client.track Queued::TRACK
252
- @client.flush
253
- @client.queued_messages.should == 0
254
- end
255
-
256
- Process.wait
257
- end unless defined? JRUBY_VERSION
258
- end
259
-
260
- context 'common' do
261
- check_property = proc { |msg, k, v| msg[k] && msg[k].should == v }
262
-
263
- before(:all) do
264
- @client = Client.new :write_key => WRITE_KEY
265
- @queue = @client.instance_variable_get :@queue
266
- end
267
-
268
-
269
- it 'should not convert ids given as fixnums to strings' do
270
- [:track, :screen, :page, :group, :identify, :alias].each do |s|
271
- @client.send s, :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :event => "coco barked", :name => "coco"
272
- message = @queue.pop(true)
273
- check_property.call(message, :userId, 1)
274
- check_property.call(message, :groupId, 2)
275
- check_property.call(message, :previousId, 3)
276
- check_property.call(message, :anonymousId, 4)
277
- end
278
- end
279
-
280
- it 'should send integrations' do
281
- [:track, :screen, :page, :group, :identify, :alias].each do |s|
282
- @client.send s, :integrations => { :All => true, :Salesforce => false }, :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :event => "coco barked", :name => "coco"
283
- message = @queue.pop(true)
284
- message[:integrations][:All].should be_true
285
- message[:integrations][:Salesforce].should be_false
286
- end
287
- end
288
- end
289
- end
290
- end
291
- end
@@ -1,96 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Segment
4
- class Analytics
5
- describe Worker do
6
- describe "#init" do
7
- it 'accepts string keys' do
8
- queue = Queue.new
9
- worker = Segment::Analytics::Worker.new(queue, 'secret', 'batch_size' => 100)
10
- worker.instance_variable_get(:@batch_size).should == 100
11
- end
12
- end
13
-
14
- describe '#flush' do
15
- before :all do
16
- Segment::Analytics::Defaults::Request::BACKOFF = 0.1
17
- end
18
-
19
- after :all do
20
- Segment::Analytics::Defaults::Request::BACKOFF = 30.0
21
- end
22
-
23
- it 'should not error if the endpoint is unreachable' do
24
- Net::HTTP.any_instance.stub(:post).and_raise(Exception)
25
-
26
- queue = Queue.new
27
- queue << {}
28
- worker = Segment::Analytics::Worker.new(queue, 'secret')
29
- worker.run
30
-
31
- queue.should be_empty
32
-
33
- Net::HTTP.any_instance.unstub(:post)
34
- end
35
-
36
- it 'should execute the error handler if the request is invalid' do
37
- Segment::Analytics::Request.any_instance.stub(:post).and_return(Segment::Analytics::Response.new(400, "Some error"))
38
-
39
- on_error = Proc.new do |status, error|
40
- puts "#{status}, #{error}"
41
- end
42
-
43
- on_error.should_receive(:call).once
44
-
45
- queue = Queue.new
46
- queue << {}
47
- worker = Segment::Analytics::Worker.new queue, 'secret', :on_error => on_error
48
- worker.run
49
-
50
- Segment::Analytics::Request::any_instance.unstub(:post)
51
-
52
- queue.should be_empty
53
- end
54
-
55
- it 'should not call on_error if the request is good' do
56
-
57
- on_error = Proc.new do |status, error|
58
- puts "#{status}, #{error}"
59
- end
60
-
61
- on_error.should_receive(:call).at_most(0).times
62
-
63
- queue = Queue.new
64
- queue << Requested::TRACK
65
- worker = Segment::Analytics::Worker.new queue, 'testsecret', :on_error => on_error
66
- worker.run
67
-
68
- queue.should be_empty
69
- end
70
- end
71
-
72
- describe '#is_requesting?' do
73
- it 'should not return true if there isn\'t a current batch' do
74
-
75
- queue = Queue.new
76
- worker = Segment::Analytics::Worker.new(queue, 'testsecret')
77
-
78
- worker.is_requesting?.should == false
79
- end
80
-
81
- it 'should return true if there is a current batch' do
82
- queue = Queue.new
83
- queue << Requested::TRACK
84
- worker = Segment::Analytics::Worker.new(queue, 'testsecret')
85
-
86
- Thread.new do
87
- worker.run
88
- worker.is_requesting?.should == false
89
- end
90
-
91
- eventually { worker.is_requesting?.should be_true }
92
- end
93
- end
94
- end
95
- end
96
- end
@@ -1,95 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Segment
4
- class Analytics
5
- describe Analytics do
6
- let(:analytics) { Segment::Analytics.new :write_key => WRITE_KEY, :stub => true }
7
-
8
- describe '#track' do
9
- it 'should error without an event' do
10
- expect { analytics.track(:user_id => 'user') }.to raise_error(ArgumentError)
11
- end
12
-
13
- it 'should error without a user_id' do
14
- expect { analytics.track(:event => 'Event') }.to raise_error(ArgumentError)
15
- end
16
-
17
- it 'should not error with the required options' do
18
- analytics.track Queued::TRACK
19
- sleep(1)
20
- end
21
- end
22
-
23
-
24
- describe '#identify' do
25
- it 'should error without a user_id' do
26
- expect { analytics.identify :traits => {} }.to raise_error(ArgumentError)
27
- end
28
-
29
- it 'should not error with the required options' do
30
- analytics.identify Queued::IDENTIFY
31
- sleep(1)
32
- end
33
- end
34
-
35
- describe '#alias' do
36
- it 'should error without from' do
37
- expect { analytics.alias :user_id => 1234 }.to raise_error(ArgumentError)
38
- end
39
-
40
- it 'should error without to' do
41
- expect { analytics.alias :previous_id => 1234 }.to raise_error(ArgumentError)
42
- end
43
-
44
- it 'should not error with the required options' do
45
- analytics.alias ALIAS
46
- sleep(1)
47
- end
48
- end
49
-
50
- describe '#group' do
51
- it 'should error without group_id' do
52
- expect { analytics.group :user_id => 'foo' }.to raise_error(ArgumentError)
53
- end
54
-
55
- it 'should error without user_id or anonymous_id' do
56
- expect { analytics.group :group_id => 'foo' }.to raise_error(ArgumentError)
57
- end
58
-
59
- it 'should not error with the required options' do
60
- analytics.group Queued::GROUP
61
- sleep(1)
62
- end
63
- end
64
-
65
- describe '#page' do
66
- it 'should error without user_id or anonymous_id' do
67
- expect { analytics.page :name => 'foo' }.to raise_error(ArgumentError)
68
- end
69
-
70
- it 'should not error with the required options' do
71
- analytics.page Queued::PAGE
72
- sleep(1)
73
- end
74
- end
75
-
76
- describe '#screen' do
77
- it 'should error without user_id or anonymous_id' do
78
- expect { analytics.screen :name => 'foo' }.to raise_error(ArgumentError)
79
- end
80
-
81
- it 'should not error with the required options' do
82
- analytics.screen Queued::SCREEN
83
- sleep(1)
84
- end
85
- end
86
-
87
- describe '#flush' do
88
- it 'should flush without error' do
89
- analytics.identify Queued::IDENTIFY
90
- analytics.flush
91
- end
92
- end
93
- end
94
- end
95
- end
data/spec/spec_helper.rb DELETED
@@ -1,81 +0,0 @@
1
- require 'segment/analytics'
2
- require 'wrong'
3
- require 'active_support/time'
4
-
5
- include Wrong
6
-
7
- # Setting timezone for ActiveSupport::TimeWithZone to UTC
8
- Time.zone = 'UTC'
9
-
10
- module Segment
11
- class Analytics
12
- WRITE_KEY = 'testsecret'
13
-
14
- TRACK = {
15
- :event => 'Ruby Library test event',
16
- :properties => {
17
- :type => 'Chocolate',
18
- :is_a_lie => true,
19
- :layers => 20,
20
- :created => Time.new
21
- }
22
- }
23
-
24
- IDENTIFY = {
25
- :traits => {
26
- :likes_animals => true,
27
- :instrument => 'Guitar',
28
- :age => 25
29
- }
30
- }
31
-
32
- ALIAS = {
33
- :previous_id => 1234,
34
- :user_id => 'abcd'
35
- }
36
-
37
- GROUP = {}
38
-
39
- PAGE = {
40
- :name => 'home'
41
- }
42
-
43
- SCREEN = {
44
- :name => 'main'
45
- }
46
-
47
- USER_ID = 1234
48
- GROUP_ID = 1234
49
-
50
- # Hashes sent to the client, snake_case
51
- module Queued
52
- TRACK = TRACK.merge :user_id => USER_ID
53
- IDENTIFY = IDENTIFY.merge :user_id => USER_ID
54
- GROUP = GROUP.merge :group_id => GROUP_ID, :user_id => USER_ID
55
- PAGE = PAGE.merge :user_id => USER_ID
56
- SCREEN = SCREEN.merge :user_id => USER_ID
57
- end
58
-
59
- # Hashes which are sent from the worker, camel_cased
60
- module Requested
61
- TRACK = TRACK.merge({
62
- :userId => USER_ID,
63
- :type => 'track'
64
- })
65
-
66
- IDENTIFY = IDENTIFY.merge({
67
- :userId => USER_ID,
68
- :type => 'identify'
69
- })
70
-
71
- GROUP = GROUP.merge({
72
- :groupId => GROUP_ID,
73
- :userId => USER_ID,
74
- :type => 'group'
75
- })
76
-
77
- PAGE = PAGE.merge :userId => USER_ID
78
- SCREEN = SCREEN.merge :userId => USER_ID
79
- end
80
- end
81
- end