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,87 @@
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 '.initialize' do
12
+
13
+ context "when an API token is provided" do
14
+
15
+ it "creates a new Trakio instance" do
16
+ expect(Trakio.new "my_api_token").to be_a Trakio
17
+ end
18
+
19
+ it "sets the API token for this instance" do
20
+ trakio = Trakio.new 'my_api_token'
21
+ expect(trakio.api_token).to eql 'my_api_token'
22
+ end
23
+
24
+
25
+ context "when a channel is provided" do
26
+ it "sets channel for this instance" do
27
+ trakio = Trakio.new "my_api_token", channel: 'my-channel'
28
+ expect(trakio.channel).to eql 'my-channel'
29
+ end
30
+ end
31
+
32
+ context "when a distinct_id is provided" do
33
+ it "sets that for this instance" do
34
+ trakio = Trakio.new "my_api_token", distinct_id: 'user@example.com'
35
+ expect(trakio.distinct_id).to eql 'user@example.com'
36
+ end
37
+ end
38
+
39
+ context "when a https option is provided" do
40
+ it "sets https option" do
41
+ trakio = Trakio.new 'my_api_token', https: false
42
+ expect(trakio.https).to be_false
43
+ end
44
+ end
45
+
46
+ context "when a https option isn't provided" do
47
+ it "defaults to true" do
48
+ trakio = Trakio.new 'my_api_token'
49
+ expect(trakio.https).to be_true
50
+ end
51
+ end
52
+
53
+ context "when a host is provided" do
54
+ it "sets host option" do
55
+ trakio = Trakio.new 'my_api_token', host: 'lvh.me:3007'
56
+ expect(trakio.host).to eql 'lvh.me:3007'
57
+ end
58
+ end
59
+
60
+ context "when a host isn't provided" do
61
+ it "defaults to api.trak.io/v1" do
62
+ trakio = Trakio.new 'my_api_token'
63
+ expect(trakio.host).to eql 'api.trak.io/v1'
64
+ end
65
+ end
66
+
67
+ end
68
+
69
+ context "when an API token isn't provided" do
70
+
71
+ it "gets it from the default instance" do
72
+ Trakio.init "my_api_token"
73
+ trakio = Trakio.new
74
+ expect(trakio.api_token).to eql 'my_api_token'
75
+ end
76
+
77
+ context "when there is no default instance" do
78
+ it "raises an exception" do
79
+ expect{ Trakio.new }.to raise_error Trakio::Exceptions::Uninitiated
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ end
86
+
87
+ end
@@ -0,0 +1,110 @@
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 '.distinct_id=' do
12
+
13
+ it "sets the distinct_id to be used by this Interface" do
14
+ trakio = Trakio.new 'api_token'
15
+ trakio.distinct_id = 'user@example.com'
16
+ expect(trakio.instance_variable_get('@distinct_id')).to eql 'user@example.com'
17
+ end
18
+
19
+ context "when this is the default Interface" do
20
+
21
+ it "raises an exception" do
22
+ Trakio.init 'my_api_token'
23
+ expect{ Trakio.distinct_id = 'user@example.com' }.to raise_error Trakio::Exceptions::NoDistinctIdForDefaultInstance
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ describe '.distinct_id' do
31
+
32
+ it "returns the current value" do
33
+ trakio = Trakio.new 'api_token'
34
+ trakio.instance_variable_set('@distinct_id','user@example.com')
35
+ expect(trakio.distinct_id).to eql 'user@example.com'
36
+ end
37
+
38
+ end
39
+
40
+ describe '.channel=' do
41
+
42
+ it "sets the channel to be used by this Interface" do
43
+ trakio = Trakio.new 'api_token'
44
+ trakio.channel = 'my-channel'
45
+ expect(trakio.instance_variable_get('@channel')).to eql 'my-channel'
46
+ end
47
+
48
+ end
49
+
50
+ describe '.channel' do
51
+
52
+ it "returns the current value" do
53
+ trakio = Trakio.new 'api_token'
54
+ trakio.instance_variable_set('@channel','my-channel')
55
+ expect(trakio.channel).to eql 'my-channel'
56
+ end
57
+
58
+ end
59
+
60
+ describe '.https=' do
61
+
62
+ it "sets whether https is to be used by this Interface" do
63
+ trakio = Trakio.new 'api_token'
64
+ trakio.https = false
65
+ expect(trakio.instance_variable_get('@https')).to be_false
66
+ end
67
+
68
+ end
69
+
70
+ describe '.https' do
71
+
72
+ it "returns the current value" do
73
+ trakio = Trakio.new 'api_token'
74
+ trakio.instance_variable_set('@https',false)
75
+ expect(trakio.https).to be_false
76
+ end
77
+
78
+ it "defaults to true" do
79
+ trakio = Trakio.new 'api_token'
80
+ expect(trakio.https).to be_true
81
+ end
82
+
83
+ end
84
+
85
+ describe '.host=' do
86
+
87
+ it "sets the host to be used by this Interface" do
88
+ trakio = Trakio.new 'api_token'
89
+ trakio.host = "lvh.me:3000"
90
+ expect(trakio.instance_variable_get('@host')).to eql "lvh.me:3000"
91
+ end
92
+
93
+ end
94
+
95
+ describe '.host' do
96
+
97
+ it "returns the current value" do
98
+ trakio = Trakio.new 'api_token'
99
+ trakio.instance_variable_set('@host',"lvh.me:3000")
100
+ expect(trakio.host).to eql "lvh.me:3000"
101
+ end
102
+
103
+ it "defaults to api.trak.io/v1" do
104
+ trakio = Trakio.new 'api_token'
105
+ expect(trakio.host).to eql "api.trak.io/v1"
106
+ end
107
+
108
+ end
109
+
110
+ end
@@ -0,0 +1,180 @@
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 '.track' do
12
+
13
+ context "when a distinct_id is provided" do
14
+
15
+ context "when an event is provided" do
16
+
17
+ it "sends a track request to api.trak.io" do
18
+ stub = stub_request(:post, "https://api.trak.io/v1/track").
19
+ with(:body => {
20
+ token: 'my_api_token',
21
+ data: {
22
+ distinct_id: 'user@example.com',
23
+ event: 'my-event'
24
+ }
25
+ }).to_return(:body => {
26
+ status: 'success',
27
+ trak_id: '1234567890'
28
+ }.to_json)
29
+
30
+ trakio = Trakio.new 'my_api_token'
31
+ resp = trakio.track distinct_id: 'user@example.com', event: 'my-event'
32
+
33
+ expect(resp[:status]).to eql 'success'
34
+ expect(resp[:trak_id]).to eql '1234567890'
35
+
36
+ stub.should have_been_requested
37
+ end
38
+
39
+ context "when a channel is provided" do
40
+
41
+ it "sends a track request to api.trak.io" do
42
+ stub = stub_request(:post, "https://api.trak.io/v1/track").
43
+ with(:body => {
44
+ token: 'my_api_token',
45
+ data: {
46
+ distinct_id: 'user@example.com',
47
+ event: 'my-event',
48
+ channel: 'my-channel'
49
+ }
50
+ }).to_return(:body => {
51
+ status: 'success',
52
+ trak_id: '1234567890'
53
+ }.to_json)
54
+
55
+ trakio = Trakio.new 'my_api_token'
56
+ resp = trakio.track distinct_id: 'user@example.com', event: 'my-event',
57
+ channel: 'my-channel'
58
+
59
+ expect(resp[:status]).to eql 'success'
60
+ expect(resp[:trak_id]).to eql '1234567890'
61
+
62
+ stub.should have_been_requested
63
+ end
64
+
65
+ end
66
+
67
+ context "when a channel isn't provided and there is one on the instance" do
68
+
69
+ it "sends a track request to api.trak.io" do
70
+ stub = stub_request(:post, "https://api.trak.io/v1/track").
71
+ with(:body => {
72
+ token: 'my_api_token',
73
+ data: {
74
+ distinct_id: 'user@example.com',
75
+ event: 'my-event',
76
+ channel: 'my-channel'
77
+ }
78
+ }).to_return(:body => {
79
+ status: 'success',
80
+ trak_id: '1234567890'
81
+ }.to_json)
82
+
83
+ trakio = Trakio.new "my_api_token", channel: 'my-channel'
84
+ resp = trakio.track distinct_id: 'user@example.com', event: 'my-event'
85
+
86
+ expect(resp[:status]).to eql 'success'
87
+ expect(resp[:trak_id]).to eql '1234567890'
88
+
89
+ stub.should have_been_requested
90
+ end
91
+
92
+ end
93
+
94
+ context "when properties are provided" do
95
+
96
+ it "sends a track request to api.trak.io" do
97
+ stub = stub_request(:post, "https://api.trak.io/v1/track").
98
+ with(:body => {
99
+ token: 'my_api_token',
100
+ data: {
101
+ distinct_id: 'user@example.com',
102
+ event: 'my-event',
103
+ channel: 'my-channel',
104
+ properties: {
105
+ foo: 'bar'
106
+ }
107
+ }
108
+ }).to_return(:body => {
109
+ status: 'success',
110
+ trak_id: '1234567890'
111
+ }.to_json)
112
+
113
+ trakio = Trakio.new "my_api_token"
114
+ resp = trakio.track distinct_id: 'user@example.com', event: 'my-event',
115
+ channel: 'my-channel', properties: { foo: 'bar' }
116
+
117
+ expect(resp[:status]).to eql 'success'
118
+ expect(resp[:trak_id]).to eql '1234567890'
119
+
120
+ stub.should have_been_requested
121
+ end
122
+
123
+ end
124
+
125
+ end
126
+
127
+ context "when an event isn't provided" do
128
+
129
+ it "raises an exception" do
130
+ trakio = Trakio.new 'my_api_token'
131
+ expect { trakio.track distinct_id: 'user@example.com' }.to raise_error RuntimeError
132
+ end
133
+
134
+ end
135
+
136
+ end
137
+
138
+ context "when a distinct_id isn't provided" do
139
+
140
+ context "when an event is provided" do
141
+
142
+ it "raises an error" do
143
+ trakio = Trakio.new 'my_api_token'
144
+ expect { trakio.track event: 'my-event' }.to raise_error RuntimeError
145
+ end
146
+
147
+ end
148
+
149
+ end
150
+
151
+ context "when a distinct_id isn't provided but its set on the instance" do
152
+
153
+ context "when an event is provided" do
154
+
155
+ it "sends a track request to api.trak.io" do
156
+ stub = stub_request(:post, "https://api.trak.io/v1/track").
157
+ with(:body => {
158
+ token: 'my_api_token',
159
+ data: {
160
+ distinct_id: 'user@example.com',
161
+ event: 'my-event',
162
+ }
163
+ }).to_return(:body => {
164
+ status: 'success',
165
+ trak_id: '1234567890'
166
+ }.to_json)
167
+
168
+ trakio = Trakio.new 'my_api_token', distinct_id: 'user@example.com'
169
+ trakio.track event: 'my-event'
170
+
171
+ stub.should have_been_requested
172
+ end
173
+
174
+ end
175
+
176
+ end
177
+
178
+ end
179
+
180
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'trakio/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "trakio-ruby"
8
+ spec.version = Trakio::VERSION
9
+ spec.authors = ["Matthew Spence", "Tobie Warburton"]
10
+ spec.email = ["matt@trak.io", "tobie.warburton@gmail.com"]
11
+ spec.description = "Official trak.io ruby library for Ruby"
12
+ spec.summary = "Official trak.io ruby library for Ruby"
13
+ spec.homepage = "https://github.com/trakio/trakio-ruby"
14
+ spec.license = "Apache 2.0"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'webmock'
25
+ spec.add_development_dependency 'fuubar'
26
+
27
+ spec.add_dependency 'rest_client'
28
+ spec.add_dependency 'pry'
29
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trakio-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Spence
8
+ - Tobie Warburton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: webmock
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: fuubar
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rest_client
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: pry
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :runtime
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ description: Official trak.io ruby library for Ruby
113
+ email:
114
+ - matt@trak.io
115
+ - tobie.warburton@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - .gitignore
121
+ - Gemfile
122
+ - LICENSE
123
+ - README.md
124
+ - Rakefile
125
+ - lib/trakio.rb
126
+ - lib/trakio/version.rb
127
+ - spec/spec_helper.rb
128
+ - spec/trakio/alias_spec.rb
129
+ - spec/trakio/annotate_spec.rb
130
+ - spec/trakio/class_methods_spec.rb
131
+ - spec/trakio/indentify_spec.rb
132
+ - spec/trakio/initialize_spec.rb
133
+ - spec/trakio/instance_methods_spec.rb
134
+ - spec/trakio/track_spec.rb
135
+ - trakio-ruby.gemspec
136
+ homepage: https://github.com/trakio/trakio-ruby
137
+ licenses:
138
+ - Apache 2.0
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.0.6
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Official trak.io ruby library for Ruby
160
+ test_files:
161
+ - spec/spec_helper.rb
162
+ - spec/trakio/alias_spec.rb
163
+ - spec/trakio/annotate_spec.rb
164
+ - spec/trakio/class_methods_spec.rb
165
+ - spec/trakio/indentify_spec.rb
166
+ - spec/trakio/initialize_spec.rb
167
+ - spec/trakio/instance_methods_spec.rb
168
+ - spec/trakio/track_spec.rb