analytics-ruby 2.0.5 → 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,299 +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 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:00Z'
82
- message[:properties][:time_with_zone].should == '2013-01-01T00:00:00Z'
83
- message[:properties][:date_time].should == '2013-01-01T00:00:00Z'
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:00Z'
123
- message[:traits][:time_with_zone].should == '2013-01-01T00:00:00Z'
124
- message[:traits][:date_time].should == '2013-01-01T00:00:00Z'
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:00Z'
192
- message[:traits][:time_with_zone].should == '2013-01-01T00:00:00Z'
193
- message[:traits][:date_time].should == '2013-01-01T00:00:00Z'
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 error without name' do
209
- expect { @client.page :user_id => 1 }.to raise_error(ArgumentError)
210
- end
211
-
212
- it 'should not error with the required options' do
213
- @client.page Queued::PAGE
214
- end
215
-
216
- it 'should not error with the required options as strings' do
217
- @client.page Utils.stringify_keys(Queued::PAGE)
218
- end
219
- end
220
-
221
- describe '#screen' do
222
- before :all do
223
- @client = Client.new :write_key => WRITE_KEY
224
- end
225
-
226
- it 'should error without user_id' do
227
- expect { @client.screen :name => 'foo' }.to raise_error(ArgumentError)
228
- end
229
-
230
- it 'should error without name' do
231
- expect { A@client.screen :user_id => 1 }.to raise_error(ArgumentError)
232
- end
233
-
234
- it 'should not error with the required options' do
235
- @client.screen Queued::SCREEN
236
- end
237
-
238
- it 'should not error with the required options as strings' do
239
- @client.screen Utils.stringify_keys(Queued::SCREEN)
240
- end
241
- end
242
-
243
- describe '#flush' do
244
- before(:all) do
245
- @client = Client.new :write_key => WRITE_KEY
246
- end
247
-
248
- it 'should wait for the queue to finish on a flush' do
249
- @client.identify Queued::IDENTIFY
250
- @client.track Queued::TRACK
251
- @client.flush
252
- @client.queued_messages.should == 0
253
- end
254
-
255
- it 'should complete when the process forks' do
256
- @client.identify Queued::IDENTIFY
257
-
258
- Process.fork do
259
- @client.track Queued::TRACK
260
- @client.flush
261
- @client.queued_messages.should == 0
262
- end
263
-
264
- Process.wait
265
- end unless defined? JRUBY_VERSION
266
- end
267
-
268
- context 'common' do
269
- check_property = proc { |msg, k, v| msg[k] && msg[k].should == v }
270
-
271
- before(:all) do
272
- @client = Client.new :write_key => WRITE_KEY
273
- @queue = @client.instance_variable_get :@queue
274
- end
275
-
276
-
277
- it 'should not convert ids given as fixnums to strings' do
278
- [:track, :screen, :page, :group, :identify, :alias].each do |s|
279
- @client.send s, :user_id => 1, :group_id => 2, :previous_id => 3, :anonymous_id => 4, :event => "coco barked", :name => "coco"
280
- message = @queue.pop(true)
281
- check_property.call(message, :userId, 1)
282
- check_property.call(message, :groupId, 2)
283
- check_property.call(message, :previousId, 3)
284
- check_property.call(message, :anonymousId, 4)
285
- end
286
- end
287
-
288
- it 'should send integrations' do
289
- [:track, :screen, :page, :group, :identify, :alias].each do |s|
290
- @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"
291
- message = @queue.pop(true)
292
- message[:integrations][:All].should be_true
293
- message[:integrations][:Salesforce].should be_false
294
- end
295
- end
296
- end
297
- end
298
- end
299
- 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,103 +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 error without name' do
71
- expect { analytics.page :user_id => 1 }.to raise_error(ArgumentError)
72
- end
73
-
74
- it 'should not error with the required options' do
75
- analytics.page Queued::PAGE
76
- sleep(1)
77
- end
78
- end
79
-
80
- describe '#screen' do
81
- it 'should error without user_id or anonymous_id' do
82
- expect { analytics.screen :name => 'foo' }.to raise_error(ArgumentError)
83
- end
84
-
85
- it 'should error without name' do
86
- expect { analytics.screen :user_id => 1 }.to raise_error(ArgumentError)
87
- end
88
-
89
- it 'should not error with the required options' do
90
- analytics.screen Queued::SCREEN
91
- sleep(1)
92
- end
93
- end
94
-
95
- describe '#flush' do
96
- it 'should flush without error' do
97
- analytics.identify Queued::IDENTIFY
98
- analytics.flush
99
- end
100
- end
101
- end
102
- end
103
- 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