trakio-ruby 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.travis.yml +8 -0
- data/README.md +56 -4
- data/Rakefile +5 -1
- data/lib/trakio/version.rb +1 -1
- data/lib/trakio.rb +53 -4
- data/spec/trakio/annotate_spec.rb +3 -3
- data/spec/trakio/exception_spec.rb +65 -0
- data/trakio-ruby.gemspec +0 -1
- metadata +5 -16
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MGY0ZDI2ZDY3MWFiYWQ3OTEwODk3NjM4YTEyMTNiMzcwMGViYjE2ZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NzViZGQ0NTVkZjVmN2Y3NjFjM2VmNWJiZGMxYmFlNGJjY2U0MmYxNw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MmQzNTAyZjg2NDIzZmFlYmM5ZGVjNWUxMzI2ZWJiYjgwZWEzMGFkZTE5M2I3
|
10
|
+
YzUwZTg5ZmQyYjA2MmZkY2UyNGU3NjdmMDI2NzA0YTVkNjA5YzFiM2NiY2I2
|
11
|
+
NjdkYzYzM2M3MDk3MzM4NzBkMTBlMzZlOThiZTFhYzg4ODE0NTY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Njc2MWU5YWNiMjlkMDU1NzA1OWFlNjFkODVjMDVjMTYxNDA1NjZlMmM5N2Fh
|
14
|
+
OTcyMWVmMTIzY2QxYjM5Y2MzN2ExOGVkOTE3OWY2OWM3MGE0MjFhYTNiOTIx
|
15
|
+
YTkzNTZhNTNjNjkwMmNlZWFiOTBhNjg3ZjI3MzBhYjBhOTY4ZjM=
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# Official trak.io Ruby Library
|
2
2
|
|
3
|
-
# WARNING: Work in progress, this library is incomplete, it has been published but is subject to change.
|
4
|
-
|
5
3
|
## Installation
|
6
4
|
|
7
5
|
Add this line to your application's Gemfile:
|
@@ -16,9 +14,63 @@ Or install it yourself as:
|
|
16
14
|
|
17
15
|
$ gem install trakio-ruby
|
18
16
|
|
19
|
-
##
|
17
|
+
## Examples
|
18
|
+
|
19
|
+
For more indepth documentation see: http://docs.trak.io/ruby.html
|
20
|
+
|
21
|
+
### Creating an instance, and then tracking an event.
|
22
|
+
```ruby
|
23
|
+
# create the instance
|
24
|
+
trakio = Trakio.new 'my_api_token'
|
25
|
+
# track my-event
|
26
|
+
resp = trakio.track distinct_id: 'user@example.com', event: 'my-event'
|
27
|
+
# resp will look like { 'status': 'success', 'trak_id': '12345' }
|
28
|
+
```
|
29
|
+
|
30
|
+
### Creating a default instance, and then tracking an event.
|
31
|
+
```ruby
|
32
|
+
# set token on default instance
|
33
|
+
Trakio.init 'my_api_token'
|
34
|
+
# track our event
|
35
|
+
resp = Trakio.track distinct_id: 'user@example.com', event: 'my-event'
|
36
|
+
# resp will look like { 'status': 'success', 'trak_id': '12345' }
|
37
|
+
```
|
38
|
+
|
39
|
+
### Creating an instance and aliasing an entry
|
40
|
+
```ruby
|
41
|
+
# set token on default instance
|
42
|
+
Trakio.init 'my_api_token'
|
43
|
+
|
44
|
+
resp = Trakio.alias distinct_id: 'u1@example.com', alias: ['u2@example.com']
|
45
|
+
# resp will look like { 'status': 'success', 'trak_id': '12345', 'distinct_ids': ['u1@example.com', 'u2@example.com'] }
|
46
|
+
|
47
|
+
# an equivilent is shown below
|
48
|
+
|
49
|
+
resp = Trakio.alias distinct_id: 'u1@example.com', alias: 'u2@example.com'
|
50
|
+
# resp will look like { 'status': 'success', 'trak_id': '12345', 'distinct_ids': ['u1@example.com', 'u2@example.com'] }
|
51
|
+
```
|
52
|
+
|
53
|
+
### Creating an instance and using identify
|
54
|
+
```ruby
|
55
|
+
# set token on default instance
|
56
|
+
Trakio.init 'my_api_token'
|
57
|
+
|
58
|
+
resp = Trakio.identify distinct_id: 'user@example.com', properties: { name: 'Tobie' }
|
59
|
+
# resp will look like { 'status': 'success', 'trak_id': '12345', 'distinct_ids': ['user@example.com'] }
|
60
|
+
```
|
61
|
+
|
62
|
+
### Creating an instance and using annotate
|
63
|
+
```ruby
|
64
|
+
# set token on default instance
|
65
|
+
Trakio.init 'my_api_token'
|
66
|
+
|
67
|
+
resp = Trakio.annotate event: 'event', channel: 'channel'
|
68
|
+
# resp will look like { 'status': 'success', 'trak_id': '12345', 'properties': {} }
|
69
|
+
```
|
20
70
|
|
21
|
-
|
71
|
+
## Creating and Running Tests
|
72
|
+
* Tests can be run by running the following commands `bundle exec rspec`
|
73
|
+
* Tests can be added by either adding into an existing spec file, or creating a new one.
|
22
74
|
|
23
75
|
## Contributing
|
24
76
|
|
data/Rakefile
CHANGED
data/lib/trakio/version.rb
CHANGED
data/lib/trakio.rb
CHANGED
@@ -6,9 +6,18 @@ require "json"
|
|
6
6
|
class Trakio
|
7
7
|
|
8
8
|
class Exceptions
|
9
|
-
class Uninitiated <
|
10
|
-
class
|
11
|
-
class
|
9
|
+
class Uninitiated < StandardError; end
|
10
|
+
class NoDistinctIdForDefaultInstance < StandardError; end
|
11
|
+
class DataObjectInvalidJson < StandardError; end
|
12
|
+
class DataObjectInvalidBase64 < StandardError; end
|
13
|
+
class DataObjectInvalidType < StandardError; end
|
14
|
+
class InvalidToken < StandardError; end
|
15
|
+
class MissingParameter < StandardError; end
|
16
|
+
class RouteNotFound < StandardError; end
|
17
|
+
class PropertiesObjectInvalid < StandardError; end
|
18
|
+
class RequestInvalidJson < StandardError; end
|
19
|
+
class RevenuePropertyInvalid < StandardError; end
|
20
|
+
class InternalServiceError < StandardError; end
|
12
21
|
end
|
13
22
|
|
14
23
|
class << self
|
@@ -157,7 +166,47 @@ class Trakio
|
|
157
166
|
url = "#{protocol}://#{@host}/#{endpoint}"
|
158
167
|
data = { token: @api_token, data: params }.to_json
|
159
168
|
resp = RestClient.post url, data, :content_type => :json, :accept => :json
|
160
|
-
JSON.parse(resp.body, :symbolize_names => true)
|
169
|
+
result = JSON.parse(resp.body, :symbolize_names => true)
|
170
|
+
return result if result[:status] == 'success'
|
171
|
+
|
172
|
+
# status must be error
|
173
|
+
# here we will raise the required exception as in the API
|
174
|
+
exception = constantize(result[:exception].sub! 'TrakioAPI::', 'Trakio::') # name of the class
|
175
|
+
message = result[:message] # extra information for the exception
|
176
|
+
raise exception.new(message)
|
177
|
+
end
|
178
|
+
|
179
|
+
protected
|
180
|
+
|
181
|
+
def constantize(camel_cased_word) # Taken from ActiveSupport
|
182
|
+
names = camel_cased_word.split('::')
|
183
|
+
|
184
|
+
# Trigger a builtin NameError exception including the ill-formed constant in the message.
|
185
|
+
Object.const_get(camel_cased_word) if names.empty?
|
186
|
+
|
187
|
+
# Remove the first blank element in case of '::ClassName' notation.
|
188
|
+
names.shift if names.size > 1 && names.first.empty?
|
189
|
+
|
190
|
+
names.inject(Object) do |constant, name|
|
191
|
+
if constant == Object
|
192
|
+
constant.const_get(name)
|
193
|
+
else
|
194
|
+
candidate = constant.const_get(name)
|
195
|
+
next candidate if constant.const_defined?(name, false)
|
196
|
+
next candidate unless Object.const_defined?(name)
|
197
|
+
|
198
|
+
# Go down the ancestors to check it it's owned
|
199
|
+
# directly before we reach Object or the end of ancestors.
|
200
|
+
constant = constant.ancestors.inject do |const, ancestor|
|
201
|
+
break const if ancestor == Object
|
202
|
+
break ancestor if ancestor.const_defined?(name, false)
|
203
|
+
const
|
204
|
+
end
|
205
|
+
|
206
|
+
# owner is in Object, so raise
|
207
|
+
constant.const_get(name, false)
|
208
|
+
end
|
209
|
+
end
|
161
210
|
end
|
162
211
|
|
163
212
|
end
|
@@ -0,0 +1,65 @@
|
|
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 an error is returned by the API" do
|
14
|
+
it "raises an exception" do
|
15
|
+
stub = stub_request(:post, "https://api.trak.io/v1/track").
|
16
|
+
with(:body => {
|
17
|
+
token: 'my_api_token',
|
18
|
+
data: {
|
19
|
+
distinct_id: 'user@example.com',
|
20
|
+
event: 'my-event'
|
21
|
+
}
|
22
|
+
}).to_return(:body => {
|
23
|
+
status: 'error',
|
24
|
+
code: 401,
|
25
|
+
exception: "TrakioAPI::Exceptions::InvalidToken",
|
26
|
+
message: "Missing or invalid API token.",
|
27
|
+
details: "You must provide a valid API token, see http://docs.trak.io/authentication.html."
|
28
|
+
}.to_json)
|
29
|
+
|
30
|
+
trakio = Trakio.new 'my_api_token'
|
31
|
+
expect {
|
32
|
+
trakio.track distinct_id: 'user@example.com', event: 'my-event'
|
33
|
+
}.to raise_error Trakio::Exceptions::InvalidToken
|
34
|
+
|
35
|
+
stub.should have_been_requested
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when no error is returned by the API" do
|
41
|
+
it "returns the result" 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
|
+
}
|
49
|
+
}).to_return(:body => {
|
50
|
+
status: 'success',
|
51
|
+
trak_id: '1234567890'
|
52
|
+
}.to_json)
|
53
|
+
|
54
|
+
trakio = Trakio.new 'my_api_token'
|
55
|
+
resp = trakio.track distinct_id: 'user@example.com', event: 'my-event'
|
56
|
+
|
57
|
+
expect(resp[:status]).to eql 'success'
|
58
|
+
expect(resp[:trak_id]).to eql '1234567890'
|
59
|
+
|
60
|
+
stub.should have_been_requested
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end # end .track
|
65
|
+
end
|
data/trakio-ruby.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trakio-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Spence
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -95,20 +95,6 @@ dependencies:
|
|
95
95
|
- - ! '>='
|
96
96
|
- !ruby/object:Gem::Version
|
97
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
98
|
description: Official trak.io ruby library for Ruby
|
113
99
|
email:
|
114
100
|
- matt@trak.io
|
@@ -118,6 +104,7 @@ extensions: []
|
|
118
104
|
extra_rdoc_files: []
|
119
105
|
files:
|
120
106
|
- .gitignore
|
107
|
+
- .travis.yml
|
121
108
|
- Gemfile
|
122
109
|
- LICENSE
|
123
110
|
- README.md
|
@@ -128,6 +115,7 @@ files:
|
|
128
115
|
- spec/trakio/alias_spec.rb
|
129
116
|
- spec/trakio/annotate_spec.rb
|
130
117
|
- spec/trakio/class_methods_spec.rb
|
118
|
+
- spec/trakio/exception_spec.rb
|
131
119
|
- spec/trakio/indentify_spec.rb
|
132
120
|
- spec/trakio/initialize_spec.rb
|
133
121
|
- spec/trakio/instance_methods_spec.rb
|
@@ -162,6 +150,7 @@ test_files:
|
|
162
150
|
- spec/trakio/alias_spec.rb
|
163
151
|
- spec/trakio/annotate_spec.rb
|
164
152
|
- spec/trakio/class_methods_spec.rb
|
153
|
+
- spec/trakio/exception_spec.rb
|
165
154
|
- spec/trakio/indentify_spec.rb
|
166
155
|
- spec/trakio/initialize_spec.rb
|
167
156
|
- spec/trakio/instance_methods_spec.rb
|