trakio-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trakio do
4
+
5
+ subject { Trakio }
6
+
7
+ after {
8
+ Trakio.default_instance = nil
9
+ }
10
+
11
+ describe '.alias' do
12
+
13
+ context "when an array alias is provided" do
14
+ it "sends an array" do
15
+ stub = stub_request(:post, "https://api.trak.io/v1/alias").
16
+ with(:body => {
17
+ token: 'my_api_token',
18
+ data: {
19
+ distinct_id: 'user@example.com',
20
+ alias: [
21
+ 'alias1@example.com',
22
+ ],
23
+ }
24
+ }).to_return(:body => {
25
+ status: 'success',
26
+ trak_id: '1234567890',
27
+ distinct_ids: ['user@example.com', 'alias1@example.com'],
28
+ }.to_json)
29
+
30
+ trakio = Trakio.new 'my_api_token'
31
+ trakio.alias distinct_id: 'user@example.com', alias: ['alias1@example.com']
32
+
33
+ stub.should have_been_requested
34
+ end
35
+ end
36
+
37
+ context "when a string alias is provided" do
38
+ it "sends a string" do
39
+ stub = stub_request(:post, "https://api.trak.io/v1/alias").
40
+ with(:body => {
41
+ token: 'my_api_token',
42
+ data: {
43
+ distinct_id: 'user@example.com',
44
+ alias: 'alias1@example.com',
45
+ }
46
+ }).to_return(:body => {
47
+ status: 'success',
48
+ trak_id: '1234567890',
49
+ distinct_ids: ['user@example.com', 'alias1@example.com'],
50
+ }.to_json)
51
+
52
+ trakio = Trakio.new 'my_api_token'
53
+ trakio.alias distinct_id: 'user@example.com', alias: 'alias1@example.com'
54
+
55
+ stub.should have_been_requested
56
+ end
57
+ end
58
+
59
+ context "when no alias is provided" do
60
+ it "raises an error" do
61
+ trakio = Trakio.new 'my_api_token'
62
+ expect { trakio.alias distinct_id: 'user@example.com' }.to raise_error RuntimeError
63
+ end
64
+ end
65
+
66
+ context "when no distinct_id is provided" do
67
+
68
+ context "when there is one set on the instance" do
69
+ it "sends a request" do
70
+ stub = stub_request(:post, "https://api.trak.io/v1/alias").
71
+ with(:body => {
72
+ token: 'my_api_token',
73
+ data: {
74
+ distinct_id: 'user@example.com',
75
+ alias: 'alias1@example.com',
76
+ }
77
+ }).to_return(:body => {
78
+ status: 'success',
79
+ trak_id: '1234567890',
80
+ distinct_ids: ['user@example.com', 'alias1@example.com'],
81
+ }.to_json)
82
+
83
+ trakio = Trakio.new 'my_api_token', distinct_id: 'user@example.com'
84
+ trakio.alias alias: 'alias1@example.com'
85
+
86
+ stub.should have_been_requested
87
+ end
88
+ end
89
+
90
+ context "when there is not one set on the instance" do
91
+ it "raises an error" do
92
+ trakio = Trakio.new 'my_api_token'
93
+ expect { trakio.alias alias: 'alias1@example.com' }.to raise_error RuntimeError
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trakio do
4
+
5
+ subject { Trakio }
6
+
7
+ after {
8
+ Trakio.default_instance = nil
9
+ }
10
+
11
+ describe '.annotate' do
12
+
13
+ context "when an event is provided" do
14
+
15
+ context "when a channel is provided" do
16
+ it "should send a request with the channel" do
17
+ stub = stub_request(:post, "https://api.trak.io/v1/annotate").
18
+ with(:body => {
19
+ token: 'my_api_token',
20
+ data: {
21
+ event: 'event',
22
+ channel: 'channel',
23
+ properties: {},
24
+ }
25
+ }).to_return(:body => {
26
+ status: 'success',
27
+ trak_id: '1234567890',
28
+ }.to_json)
29
+
30
+ trakio = Trakio.new 'my_api_token'
31
+ trakio.annotate event: 'event', channel: 'channel'
32
+
33
+ stub.should have_been_requested
34
+ end
35
+ end
36
+
37
+ context "when a channel isnt provided" do
38
+ context "when there is a channel set on the instance" do
39
+ it "should send a request with the channel" do
40
+ stub = stub_request(:post, "https://api.trak.io/v1/annotate").
41
+ with(:body => {
42
+ token: 'my_api_token',
43
+ data: {
44
+ event: 'event',
45
+ channel: 'channel',
46
+ properties: {},
47
+ }
48
+ }).to_return(:body => {
49
+ status: 'success',
50
+ trak_id: '1234567890',
51
+ }.to_json)
52
+
53
+ trakio = Trakio.new 'my_api_token', channel: 'channel'
54
+ trakio.annotate event: 'event'
55
+
56
+ stub.should have_been_requested
57
+ end
58
+ end
59
+ end
60
+
61
+ context "when properties are provided" do
62
+ it "should send a request with the properties" do
63
+ stub = stub_request(:post, "https://api.trak.io/v1/annotate").
64
+ with(:body => {
65
+ token: 'my_api_token',
66
+ data: {
67
+ event: 'event',
68
+ properties: {
69
+ details: 'details',
70
+ },
71
+ }
72
+ }).to_return(:body => {
73
+ status: 'success',
74
+ trak_id: '1234567890',
75
+ }.to_json)
76
+
77
+ trakio = Trakio.new 'my_api_token'
78
+ trakio.annotate event: 'event', properties: { details: 'details' }
79
+
80
+ stub.should have_been_requested
81
+ end
82
+ end
83
+
84
+ context "when properties are not provided" do
85
+ it "should send a request with empty properties" do
86
+ stub = stub_request(:post, "https://api.trak.io/v1/annotate").
87
+ with(:body => {
88
+ token: 'my_api_token',
89
+ data: {
90
+ event: 'event',
91
+ properties: {},
92
+ }
93
+ }).to_return(:body => {
94
+ status: 'success',
95
+ trak_id: '1234567890',
96
+ }.to_json)
97
+
98
+ trakio = Trakio.new 'my_api_token'
99
+ trakio.annotate event: 'event'
100
+
101
+ stub.should have_been_requested
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ context "when an event is not provided" do
108
+
109
+ context "when a channel is provided" do
110
+ it "should raise an error" do
111
+ trakio = Trakio.new 'my_api_token'
112
+ expect { trakio.annotate channel: 'channel' }.to raise_error RuntimeError
113
+ end
114
+ end
115
+
116
+ context "when properties are provided" do
117
+ it "should raise an error" do
118
+ trakio = Trakio.new 'my_api_token'
119
+ expect { trakio.annotate properties: { name: 'tobie' } }.to raise_error RuntimeError
120
+ end
121
+ end
122
+
123
+ context "when no arguments are provided" do
124
+ it "should raise an error" do
125
+ trakio = Trakio.new 'my_api_token'
126
+ expect { trakio.annotate }.to raise_error ArgumentError
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
134
+ end
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trakio do
4
+
5
+ subject { Trakio }
6
+
7
+ after {
8
+ Trakio.default_instance = nil
9
+ }
10
+
11
+ describe '#default_instance' do
12
+
13
+ context "when a default instance hasn't been created" do
14
+ it "raises an exception" do
15
+ expect{ Trakio.default_instance }.to raise_error Trakio::Exceptions::Uninitiated
16
+ end
17
+ end
18
+
19
+ context "when a default instance has already been created" do
20
+ it "returns that" do
21
+ Trakio.init 'my_api_token'
22
+ expect(Trakio.default_instance).to be_a Trakio
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ describe '#init' do
29
+
30
+ context "when an API token is provided" do
31
+
32
+ it "creates a default Trakio::Interface" do
33
+ Trakio.init 'my_api_token'
34
+ end
35
+
36
+ context "when a distinct_id is provided" do
37
+ it "raises an exception" do
38
+ expect{
39
+ Trakio.init 'my_api_token', distinct_id: 'user@example.com'
40
+ }.to raise_error Trakio::Exceptions::NoDistinctIdForDefaultInstance
41
+ end
42
+ end
43
+
44
+ context "when a channel is provided" do
45
+ it "sets the channel option" do
46
+ Trakio.init 'my_api_token', channel: 'my-channel'
47
+ expect(Trakio.channel).to eql 'my-channel'
48
+ end
49
+ end
50
+
51
+ context "when a https is provided" do
52
+ it "sets https option" do
53
+ Trakio.init 'my_api_token', https: false
54
+ expect(Trakio.https).to be_false
55
+ end
56
+ end
57
+
58
+ context "when a https isn't provided" do
59
+ it "defaults to true" do
60
+ Trakio.init 'my_api_token'
61
+ expect(Trakio.https).to be_true
62
+ end
63
+ end
64
+
65
+ context "when a host is provided" do
66
+ it "sets host option" do
67
+ Trakio.init 'my_api_token', host: 'lvh.me:3007'
68
+ expect(Trakio.host).to eql 'lvh.me:3007'
69
+ end
70
+ end
71
+
72
+ context "when a host isn't provided" do
73
+ it "defaults to api.trak.io/v1" do
74
+ Trakio.init 'my_api_token'
75
+ expect(Trakio.host).to eql 'api.trak.io/v1'
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ context "when an API token isn't provided" do
82
+ it "raises an exception" do
83
+ expect{ Trakio.init }.to raise_error Trakio::Exceptions::InvalidToken
84
+ end
85
+ end
86
+
87
+ end
88
+
89
+ describe '#track' do
90
+ it "calls track on the default Trakio instance" do
91
+ default_instance = double(Trakio)
92
+
93
+ Trakio.default_instance = default_instance
94
+ expect(Trakio.default_instance).to receive(:track)
95
+
96
+ Trakio.track distinct_id: 'tobie.warburton@gmail.com', event: 'test-event'
97
+ end
98
+ end
99
+
100
+ describe '#identify' do
101
+ it "calls identify on the default Trakio instance" do
102
+ default_instance = double(Trakio)
103
+
104
+ Trakio.default_instance = default_instance
105
+ expect(Trakio.default_instance).to receive(:identify)
106
+
107
+ Trakio.identify
108
+ end
109
+ end
110
+
111
+ describe '#alias' do
112
+ it "calls alias on the default Trakio instance" do
113
+ default_instance = double(Trakio)
114
+
115
+ Trakio.default_instance = default_instance
116
+ expect(Trakio.default_instance).to receive(:alias)
117
+
118
+ Trakio.alias
119
+ end
120
+ end
121
+
122
+ describe '#annotate' do
123
+
124
+ it "calls annotate on the default Trakio instance" do
125
+ default_instance = double(Trakio)
126
+
127
+ Trakio.default_instance = default_instance
128
+ expect(Trakio.default_instance).to receive(:annotate)
129
+
130
+ Trakio.annotate
131
+ end
132
+
133
+ end
134
+
135
+ describe '#page_view' do
136
+
137
+ it "calls page_view on the default Trakio instance" do
138
+ default_instance = double(Trakio)
139
+
140
+ Trakio.default_instance = default_instance
141
+ expect(Trakio.default_instance).to receive(:page_view)
142
+
143
+ Trakio.page_view
144
+ end
145
+
146
+ end
147
+
148
+
149
+ describe '#distinct_id' do
150
+ it "raise an error" do
151
+ expect{ Trakio.distinct_id }.to raise_error Trakio::Exceptions::NoDistinctIdForDefaultInstance
152
+ end
153
+ end
154
+
155
+ describe '#channel' do
156
+
157
+ it "calls channel on the default Trakio instance" do
158
+ default_instance = double(Trakio)
159
+
160
+ Trakio.default_instance = default_instance
161
+ expect(Trakio.default_instance).to receive(:channel)
162
+
163
+ Trakio.channel
164
+ end
165
+
166
+ end
167
+
168
+ end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trakio do
4
+
5
+ subject { Trakio }
6
+
7
+ after {
8
+ Trakio.default_instance = nil
9
+ }
10
+
11
+ describe '.identify' do
12
+
13
+ context "when a distinct_id is provided" do
14
+ context "when properties are provided" do
15
+ it "sends an identify request" do
16
+ stub = stub_request(:post, "https://api.trak.io/v1/identify").
17
+ with(:body => {
18
+ token: 'my_api_token',
19
+ data: {
20
+ distinct_id: 'user@example.com',
21
+ properties: {
22
+ name: 'Tobie',
23
+ },
24
+ }
25
+ }).to_return(:body => {
26
+ status: 'success',
27
+ trak_id: '1234567890',
28
+ distinct_ids: ['user@example.com'],
29
+ }.to_json)
30
+
31
+ trakio = Trakio.new 'my_api_token'
32
+ trakio.identify distinct_id: 'user@example.com', properties: { name: 'Tobie' }
33
+
34
+ stub.should have_been_requested
35
+ end
36
+ end
37
+ end
38
+
39
+ context "when a distinct_id isn't provided but is set on instance" do
40
+ context "when properties are provided" do
41
+ it "sends an identify request" do
42
+ stub = stub_request(:post, "https://api.trak.io/v1/identify").
43
+ with(:body => {
44
+ token: 'my_api_token',
45
+ data: {
46
+ distinct_id: 'user@example.com',
47
+ properties: {
48
+ name: 'Tobie',
49
+ },
50
+ }
51
+ }).to_return(:body => {
52
+ status: 'success',
53
+ trak_id: '1234567890',
54
+ distinct_ids: ['user@example.com'],
55
+ }.to_json)
56
+
57
+ trakio = Trakio.new 'my_api_token', distinct_id: 'user@example.com'
58
+ trakio.identify properties: { name: 'Tobie' }
59
+
60
+ stub.should have_been_requested
61
+ end
62
+ end
63
+ end
64
+
65
+ context "when a distinct_id isn't provided" do
66
+
67
+ context "when properties are provided" do
68
+ it "raises an error" do
69
+ trakio = Trakio.new 'my_api_token'
70
+ expect { trakio.identify properties: { name: 'Tobie' } }.to raise_error RuntimeError
71
+ end
72
+ end
73
+
74
+ context "when properties aren't provided" do
75
+ it "raises an error" do
76
+ trakio = Trakio.new 'my_api_token', distinct_id: 'user@example.com'
77
+ expect { trakio.identify }.to raise_error ArgumentError
78
+ end
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ end