greenfinch-ruby 0.1.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.
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'time'
3
+
4
+ require 'mixpanel-ruby/events.rb'
5
+ require 'mixpanel-ruby/version.rb'
6
+
7
+ describe Mixpanel::Events do
8
+ before(:each) do
9
+ @time_now = Time.parse('Jun 6 1972, 16:23:04')
10
+ allow(Time).to receive(:now).and_return(@time_now)
11
+
12
+ @log = []
13
+ @events = Mixpanel::Events.new('TEST TOKEN') do |type, message|
14
+ @log << [type, JSON.load(message)]
15
+ end
16
+ end
17
+
18
+ it 'should send a well formed track/ message' do
19
+ @events.track('TEST ID', 'Test Event', {
20
+ 'Circumstances' => 'During a test'
21
+ })
22
+ expect(@log).to eq([[:event, 'data' => {
23
+ 'event' => 'Test Event',
24
+ 'properties' => {
25
+ 'Circumstances' => 'During a test',
26
+ 'distinct_id' => 'TEST ID',
27
+ 'mp_lib' => 'ruby',
28
+ '$lib_version' => Mixpanel::VERSION,
29
+ 'token' => 'TEST TOKEN',
30
+ 'time' => @time_now.to_i
31
+ }
32
+ }]])
33
+ end
34
+
35
+ it 'should send a well formed import/ message' do
36
+ @events.import('API_KEY', 'TEST ID', 'Test Event', {
37
+ 'Circumstances' => 'During a test'
38
+ })
39
+ expect(@log).to eq([[:import, {
40
+ 'api_key' => 'API_KEY',
41
+ 'data' => {
42
+ 'event' => 'Test Event',
43
+ 'properties' => {
44
+ 'Circumstances' => 'During a test',
45
+ 'distinct_id' => 'TEST ID',
46
+ 'mp_lib' => 'ruby',
47
+ '$lib_version' => Mixpanel::VERSION,
48
+ 'token' => 'TEST TOKEN',
49
+ 'time' => @time_now.to_i
50
+ }
51
+ }
52
+ } ]])
53
+ end
54
+
55
+ it 'should allow users to pass timestamp for import' do
56
+ older_time = Time.parse('Jun 6 1971, 16:23:04')
57
+ @events.import('API_KEY', 'TEST ID', 'Test Event', {
58
+ 'Circumstances' => 'During a test',
59
+ 'time' => older_time.to_i,
60
+ })
61
+ expect(@log).to eq([[:import, {
62
+ 'api_key' => 'API_KEY',
63
+ 'data' => {
64
+ 'event' => 'Test Event',
65
+ 'properties' => {
66
+ 'Circumstances' => 'During a test',
67
+ 'distinct_id' => 'TEST ID',
68
+ 'mp_lib' => 'ruby',
69
+ '$lib_version' => Mixpanel::VERSION,
70
+ 'token' => 'TEST TOKEN',
71
+ 'time' => older_time.to_i,
72
+ }
73
+ }
74
+ } ]])
75
+ end
76
+ end
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+ require 'active_support/time'
3
+
4
+ require 'mixpanel-ruby/groups'
5
+
6
+ describe Mixpanel::Groups do
7
+ before(:each) do
8
+ @time_now = Time.parse('Jun 6 1972, 16:23:04')
9
+ allow(Time).to receive(:now).and_return(@time_now)
10
+
11
+ @log = []
12
+ @groups = Mixpanel::Groups.new('TEST TOKEN') do |type, message|
13
+ @log << [type, JSON.load(message)]
14
+ end
15
+ end
16
+
17
+ it 'should send a well formed groups/set message' do
18
+ @groups.set("TEST GROUP KEY", "TEST GROUP ID", {
19
+ '$groupname' => 'Mixpanel',
20
+ '$grouprevenue' => 200
21
+ })
22
+ expect(@log).to eq([[:group_update, 'data' => {
23
+ '$token' => 'TEST TOKEN',
24
+ '$group_key' => 'TEST GROUP KEY',
25
+ '$group_id' => 'TEST GROUP ID',
26
+ '$time' => @time_now.to_i * 1000,
27
+ '$set' => {
28
+ '$groupname' => 'Mixpanel',
29
+ '$grouprevenue' => 200
30
+ }
31
+ }]])
32
+ end
33
+
34
+ it 'should properly cast dates' do
35
+ @groups.set("TEST GROUP KEY", "TEST GROUP ID", {
36
+ 'created_at' => DateTime.new(2013, 1, 2, 3, 4, 5)
37
+ })
38
+ expect(@log).to eq([[:group_update, 'data' => {
39
+ '$token' => 'TEST TOKEN',
40
+ '$group_key' => 'TEST GROUP KEY',
41
+ '$group_id' => 'TEST GROUP ID',
42
+ '$time' => @time_now.to_i * 1000,
43
+ '$set' => {
44
+ 'created_at' => '2013-01-02T03:04:05'
45
+ }
46
+ }]])
47
+ end
48
+
49
+ it 'should convert offset datetimes to UTC' do
50
+ @groups.set("TEST GROUP KEY", "TEST GROUP ID", {
51
+ 'created_at' => DateTime.new(2013, 1, 1, 18, 4, 5, '-8')
52
+ })
53
+ expect(@log).to eq([[:group_update, 'data' => {
54
+ '$token' => 'TEST TOKEN',
55
+ '$group_key' => 'TEST GROUP KEY',
56
+ '$group_id' => 'TEST GROUP ID',
57
+ '$time' => @time_now.to_i * 1000,
58
+ '$set' => {
59
+ 'created_at' => '2013-01-02T02:04:05'
60
+ }
61
+ }]])
62
+ end
63
+
64
+ it 'should convert offset ActiveSupport::TimeWithZone objects to UTC' do
65
+ Time.zone = 'Pacific Time (US & Canada)'
66
+ @groups.set("TEST GROUP KEY", "TEST GROUP ID", {
67
+ 'created_at' => Time.zone.local(2013, 1, 1, 18, 4, 5)
68
+ })
69
+ expect(@log).to eq([[:group_update, 'data' => {
70
+ '$token' => 'TEST TOKEN',
71
+ '$group_key' => 'TEST GROUP KEY',
72
+ '$group_id' => 'TEST GROUP ID',
73
+ '$time' => @time_now.to_i * 1000,
74
+ '$set' => {
75
+ 'created_at' => '2013-01-02T02:04:05'
76
+ }
77
+ }]])
78
+ end
79
+
80
+ it 'should send a well formed groups/set_once message' do
81
+ @groups.set_once("TEST GROUP KEY", "TEST GROUP ID", {
82
+ '$groupname' => 'Mixpanel',
83
+ '$grouprevenue' => 200
84
+ })
85
+ expect(@log).to eq([[:group_update, 'data' => {
86
+ '$token' => 'TEST TOKEN',
87
+ '$group_key' => 'TEST GROUP KEY',
88
+ '$group_id' => 'TEST GROUP ID',
89
+ '$time' => @time_now.to_i * 1000,
90
+ '$set_once' => {
91
+ '$groupname' => 'Mixpanel',
92
+ '$grouprevenue' => 200
93
+ }
94
+ }]])
95
+ end
96
+
97
+ it 'should send a well formed groups/remove message' do
98
+ @groups.remove("TEST GROUP KEY", "TEST GROUP ID", {
99
+ 'Albums' => 'Diamond Dogs'
100
+ })
101
+ expect(@log).to eq([[:group_update, 'data' => {
102
+ '$token' => 'TEST TOKEN',
103
+ '$group_key' => 'TEST GROUP KEY',
104
+ '$group_id' => 'TEST GROUP ID',
105
+ '$time' => @time_now.to_i * 1000,
106
+ '$remove' => {
107
+ 'Albums' => 'Diamond Dogs'
108
+ }
109
+ }]])
110
+ end
111
+
112
+ it 'should send a well formed groups/union message' do
113
+ @groups.union("TEST GROUP KEY", "TEST GROUP ID", {
114
+ 'Albums' => ['Diamond Dogs']
115
+ })
116
+ expect(@log).to eq([[:group_update, 'data' => {
117
+ '$token' => 'TEST TOKEN',
118
+ '$group_key' => 'TEST GROUP KEY',
119
+ '$group_id' => 'TEST GROUP ID',
120
+ '$time' => @time_now.to_i * 1000,
121
+ '$union' => {
122
+ 'Albums' => ['Diamond Dogs']
123
+ }
124
+ }]])
125
+ end
126
+
127
+ it 'should send a well formed unset message' do
128
+ @groups.unset("TEST GROUP KEY", "TEST GROUP ID", 'Albums')
129
+ expect(@log).to eq([[:group_update, 'data' => {
130
+ '$token' => 'TEST TOKEN',
131
+ '$group_key' => 'TEST GROUP KEY',
132
+ '$group_id' => 'TEST GROUP ID',
133
+ '$time' => @time_now.to_i * 1000,
134
+ '$unset' => ['Albums']
135
+ }]])
136
+ end
137
+
138
+ it 'should send a well formed unset message with multiple properties' do
139
+ @groups.unset("TEST GROUP KEY", "TEST GROUP ID", ['Albums', 'Vinyls'])
140
+ expect(@log).to eq([[:group_update, 'data' => {
141
+ '$token' => 'TEST TOKEN',
142
+ '$group_key' => 'TEST GROUP KEY',
143
+ '$group_id' => 'TEST GROUP ID',
144
+ '$time' => @time_now.to_i * 1000,
145
+ '$unset' => ['Albums', 'Vinyls']
146
+ }]])
147
+ end
148
+
149
+ it 'should send a well formed groups/delete message' do
150
+ @groups.delete_group("TEST GROUP KEY", "TEST GROUP ID")
151
+ expect(@log).to eq([[:group_update, 'data' => {
152
+ '$token' => 'TEST TOKEN',
153
+ '$group_key' => 'TEST GROUP KEY',
154
+ '$group_id' => 'TEST GROUP ID',
155
+ '$time' => @time_now.to_i * 1000,
156
+ '$delete' => ''
157
+ }]])
158
+ end
159
+ end
@@ -0,0 +1,220 @@
1
+ require 'spec_helper'
2
+ require 'active_support/time'
3
+
4
+ require 'mixpanel-ruby/people'
5
+
6
+ describe Mixpanel::People do
7
+ before(:each) do
8
+ @time_now = Time.parse('Jun 6 1972, 16:23:04')
9
+ allow(Time).to receive(:now).and_return(@time_now)
10
+
11
+ @log = []
12
+ @people = Mixpanel::People.new('TEST TOKEN') do |type, message|
13
+ @log << [type, JSON.load(message)]
14
+ end
15
+ end
16
+
17
+ it 'should send a well formed engage/set message' do
18
+ @people.set("TEST ID", {
19
+ '$firstname' => 'David',
20
+ '$lastname' => 'Bowie',
21
+ })
22
+ expect(@log).to eq([[:profile_update, 'data' => {
23
+ '$token' => 'TEST TOKEN',
24
+ '$distinct_id' => 'TEST ID',
25
+ '$time' => @time_now.to_i * 1000,
26
+ '$set' => {
27
+ '$firstname' => 'David',
28
+ '$lastname' => 'Bowie'
29
+ }
30
+ }]])
31
+ end
32
+
33
+ it 'should properly cast dates' do
34
+ @people.set("TEST ID", {
35
+ 'created_at' => DateTime.new(2013, 1, 2, 3, 4, 5)
36
+ })
37
+ expect(@log).to eq([[:profile_update, 'data' => {
38
+ '$token' => 'TEST TOKEN',
39
+ '$distinct_id' => 'TEST ID',
40
+ '$time' => @time_now.to_i * 1000,
41
+ '$set' => {
42
+ 'created_at' => '2013-01-02T03:04:05'
43
+ }
44
+ }]])
45
+ end
46
+
47
+ it 'should convert offset datetimes to UTC' do
48
+ @people.set("TEST ID", {
49
+ 'created_at' => DateTime.new(2013, 1, 1, 18, 4, 5, '-8')
50
+ })
51
+ expect(@log).to eq([[:profile_update, 'data' => {
52
+ '$token' => 'TEST TOKEN',
53
+ '$distinct_id' => 'TEST ID',
54
+ '$time' => @time_now.to_i * 1000,
55
+ '$set' => {
56
+ 'created_at' => '2013-01-02T02:04:05'
57
+ }
58
+ }]])
59
+ end
60
+
61
+ it 'should convert offset ActiveSupport::TimeWithZone objects to UTC' do
62
+ Time.zone = 'Pacific Time (US & Canada)'
63
+ @people.set("TEST ID", {
64
+ 'created_at' => Time.zone.local(2013, 1, 1, 18, 4, 5)
65
+ })
66
+ expect(@log).to eq([[:profile_update, 'data' => {
67
+ '$token' => 'TEST TOKEN',
68
+ '$distinct_id' => 'TEST ID',
69
+ '$time' => @time_now.to_i * 1000,
70
+ '$set' => {
71
+ 'created_at' => '2013-01-02T02:04:05'
72
+ }
73
+ }]])
74
+ end
75
+
76
+ it 'should send a well formed engage/set_once message' do
77
+ @people.set_once("TEST ID", {
78
+ '$firstname' => 'David',
79
+ '$lastname' => 'Bowie',
80
+ })
81
+ expect(@log).to eq([[:profile_update, 'data' => {
82
+ '$token' => 'TEST TOKEN',
83
+ '$distinct_id' => 'TEST ID',
84
+ '$time' => @time_now.to_i * 1000,
85
+ '$set_once' => {
86
+ '$firstname' => 'David',
87
+ '$lastname' => 'Bowie'
88
+ }
89
+ }]])
90
+ end
91
+
92
+ it 'should send a well formed engage/add message' do
93
+ @people.increment("TEST ID", {'Albums Released' => 10})
94
+ expect(@log).to eq([[:profile_update, 'data' => {
95
+ '$token' => 'TEST TOKEN',
96
+ '$distinct_id' => 'TEST ID',
97
+ '$time' => @time_now.to_i * 1000,
98
+ '$add' => {
99
+ 'Albums Released' => 10
100
+ }
101
+ }]])
102
+ end
103
+
104
+ it 'should send an engage/add message with a value of 1' do
105
+ @people.plus_one("TEST ID", 'Albums Released')
106
+ expect(@log).to eq([[:profile_update, 'data' => {
107
+ '$token' => 'TEST TOKEN',
108
+ '$distinct_id' => 'TEST ID',
109
+ '$time' => @time_now.to_i * 1000,
110
+ '$add' => {
111
+ 'Albums Released' => 1
112
+ }
113
+ }]])
114
+ end
115
+
116
+ it 'should send a well formed engage/append message' do
117
+ @people.append("TEST ID", {'Albums' => 'Diamond Dogs'})
118
+ expect(@log).to eq([[:profile_update, 'data' => {
119
+ '$token' => 'TEST TOKEN',
120
+ '$distinct_id' => 'TEST ID',
121
+ '$time' => @time_now.to_i * 1000,
122
+ '$append' => {
123
+ 'Albums' => 'Diamond Dogs'
124
+ }
125
+ }]])
126
+ end
127
+
128
+ it 'should send a well formed engage/union message' do
129
+ @people.union("TEST ID", {'Albums' => ['Diamond Dogs']})
130
+ expect(@log).to eq([[:profile_update, 'data' => {
131
+ '$token' => 'TEST TOKEN',
132
+ '$distinct_id' => 'TEST ID',
133
+ '$time' => @time_now.to_i * 1000,
134
+ '$union' => {
135
+ 'Albums' => ['Diamond Dogs']
136
+ }
137
+ }]])
138
+ end
139
+
140
+ it 'should send a well formed unset message' do
141
+ @people.unset('TEST ID', 'Albums')
142
+ expect(@log).to eq([[:profile_update, 'data' => {
143
+ '$token' => 'TEST TOKEN',
144
+ '$distinct_id' => 'TEST ID',
145
+ '$time' => @time_now.to_i * 1000,
146
+ '$unset' => ['Albums']
147
+ }]])
148
+ end
149
+
150
+ it 'should send a well formed unset message with multiple properties' do
151
+ @people.unset('TEST ID', ['Albums', 'Vinyls'])
152
+ expect(@log).to eq([[:profile_update, 'data' => {
153
+ '$token' => 'TEST TOKEN',
154
+ '$distinct_id' => 'TEST ID',
155
+ '$time' => @time_now.to_i * 1000,
156
+ '$unset' => ['Albums', 'Vinyls']
157
+ }]])
158
+ end
159
+
160
+ it 'should send an engage/append with the right $transaction stuff' do
161
+ @people.track_charge("TEST ID", 25.42, {
162
+ '$time' => DateTime.new(1999,12,24,14, 02, 53),
163
+ 'SKU' => '1234567'
164
+ })
165
+ expect(@log).to eq([[:profile_update, 'data' => {
166
+ '$token' => 'TEST TOKEN',
167
+ '$distinct_id' => 'TEST ID',
168
+ '$time' => @time_now.to_i * 1000,
169
+ '$append' => {
170
+ '$transactions' => {
171
+ '$time' => '1999-12-24T14:02:53',
172
+ 'SKU' => '1234567',
173
+ '$amount' => 25.42
174
+ }
175
+ }
176
+ }]])
177
+ end
178
+
179
+ it 'should send a well formed engage/unset message for $transaction' do
180
+ @people.clear_charges("TEST ID")
181
+ expect(@log).to eq([[:profile_update, 'data' => {
182
+ '$token' => 'TEST TOKEN',
183
+ '$distinct_id' => 'TEST ID',
184
+ '$time' => @time_now.to_i * 1000,
185
+ '$unset' => ['$transactions']
186
+ }]])
187
+ end
188
+
189
+ it 'should send a well formed engage/delete message' do
190
+ @people.delete_user("TEST ID")
191
+ expect(@log).to eq([[:profile_update, 'data' => {
192
+ '$token' => 'TEST TOKEN',
193
+ '$distinct_id' => 'TEST ID',
194
+ '$time' => @time_now.to_i * 1000,
195
+ '$delete' => ''
196
+ }]])
197
+ end
198
+
199
+ it 'should send a well formed engage/delete message with blank optional_params' do
200
+ @people.delete_user("TEST ID", {})
201
+ expect(@log).to eq([[:profile_update, 'data' => {
202
+ '$token' => 'TEST TOKEN',
203
+ '$distinct_id' => 'TEST ID',
204
+ '$time' => @time_now.to_i * 1000,
205
+ '$delete' => ''
206
+ }]])
207
+ end
208
+
209
+ it 'should send a well formed engage/delete message with ignore_alias true' do
210
+ @people.delete_user("TEST ID", {"$ignore_alias"=>true})
211
+ expect(@log).to eq([[:profile_update, 'data' => {
212
+ '$token' => 'TEST TOKEN',
213
+ '$distinct_id' => 'TEST ID',
214
+ '$time' => @time_now.to_i * 1000,
215
+ '$delete' => '',
216
+ "$ignore_alias"=>true
217
+ }]])
218
+ end
219
+
220
+ end