trakio-ruby 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trakio.rb +2 -92
- data/lib/trakio/client.rb +7 -0
- data/lib/trakio_client.rb +100 -0
- data/lib/{trakio → trakio_client}/alias.rb +5 -5
- data/lib/{trakio → trakio_client}/annotate.rb +3 -3
- data/lib/{trakio → trakio_client}/company.rb +4 -4
- data/lib/{trakio → trakio_client}/end_point.rb +2 -2
- data/lib/{trakio → trakio_client}/exceptions.rb +1 -1
- data/lib/{trakio → trakio_client}/identify.rb +4 -4
- data/lib/{trakio → trakio_client}/track.rb +5 -5
- data/lib/trakio_client/version.rb +3 -0
- data/spec/trakio/exception_spec.rb +1 -1
- data/trakio-ruby.gemspec +2 -2
- metadata +12 -10
- data/lib/trakio/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dc646d1ad49f9c56f9d126bb1d56aeb3f229b14
|
4
|
+
data.tar.gz: 512212ca3ac321f3da958b8551e22f9d5490580b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23623f51b3df64328df6569c66cbabc6d66cf9305c9d89e21a3d9a8bd58111367c73ad8f8e1119e62c236bfe5432be065bba3cc0d4369a50b379e1bd0269c649
|
7
|
+
data.tar.gz: 01d94782da9dae35c3af49889c762b733b5b5e3c18bcca6ac649e3a27a0b54e2cf31568d6afb9aac2bfe2b9cd19e4a6bc787df3dc9281ced070acb5924247689
|
data/lib/trakio.rb
CHANGED
@@ -1,97 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require "trakio/alias"
|
3
|
-
require "trakio/annotate"
|
4
|
-
require "trakio/company"
|
5
|
-
require "trakio/exceptions"
|
6
|
-
require "trakio/identify"
|
7
|
-
require "trakio/track"
|
8
|
-
require "trakio/version"
|
9
|
-
require "rest_client"
|
10
|
-
require "json"
|
11
|
-
require "date"
|
12
|
-
|
1
|
+
require 'trakio_client'
|
13
2
|
|
14
3
|
class Trakio
|
15
4
|
|
16
|
-
|
17
|
-
attr_accessor :https
|
18
|
-
attr_accessor :host
|
19
|
-
attr_accessor :channel
|
20
|
-
attr_accessor :distinct_id
|
21
|
-
attr_accessor :company_id
|
22
|
-
|
23
|
-
class << self
|
24
|
-
|
25
|
-
def init(*args)
|
26
|
-
api_token, params = args
|
27
|
-
raise Trakio::Exceptions::InvalidToken.new('Missing API Token') unless api_token
|
28
|
-
raise Trakio::Exceptions::NoDistinctIdForDefaultInstance if params and params.has_key?(:distinct_id)
|
29
|
-
raise Trakio::Exceptions::NoCompanyIdForDefaultInstance if params and params.has_key?(:company_id)
|
30
|
-
@default_instance = Trakio.new(*args)
|
31
|
-
end
|
32
|
-
|
33
|
-
def default_instance
|
34
|
-
raise Trakio::Exceptions::Uninitiated unless @default_instance
|
35
|
-
@default_instance
|
36
|
-
end
|
37
|
-
|
38
|
-
def default_instance=(instance)
|
39
|
-
@default_instance = instance
|
40
|
-
end
|
41
|
-
|
42
|
-
def distinct_id value=nil
|
43
|
-
raise Trakio::Exceptions::NoDistinctIdForDefaultInstance
|
44
|
-
end
|
45
|
-
alias :distinct_id= :distinct_id
|
46
|
-
|
47
|
-
def company_id value=nil
|
48
|
-
raise Trakio::Exceptions::NoCompanyIdForDefaultInstance
|
49
|
-
end
|
50
|
-
alias :company_id= :company_id
|
51
|
-
|
52
|
-
def method_missing(method, *args, &block)
|
53
|
-
# passes to the default_instance so that
|
54
|
-
# Trakio.channel returns Trakio.default_instance.channel
|
55
|
-
@default_instance.send(method, *args, &block)
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
def initialize(*args)
|
61
|
-
api_token, params = args
|
62
|
-
api_token = Trakio.default_instance.api_token unless api_token
|
63
|
-
|
64
|
-
@api_token = api_token or raise Trakio::Exceptions::InvalidToken.new('Missing API Token')
|
65
|
-
@https = true
|
66
|
-
@host = 'api.trak.io/v1'
|
67
|
-
|
68
|
-
%w{https host channel distinct_id company_id}.each do |name|
|
69
|
-
instance_variable_set("@#{name}", params[name.to_sym]) if params && params.has_key?(name.to_sym)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
['Alias', 'Annotate', 'Company', 'Identify', 'Track'].each do |method_object|
|
74
|
-
Trakio.class_eval "
|
75
|
-
def #{method_object.downcase} *args
|
76
|
-
@#{method_object.downcase} ||= #{method_object}.new(self)
|
77
|
-
@#{method_object.downcase}.run(*args)
|
78
|
-
end
|
79
|
-
"
|
80
|
-
end
|
81
|
-
|
82
|
-
def page_view *args
|
83
|
-
@track ||= Track.new(self)
|
84
|
-
@track.page_view(*args)
|
85
|
-
end
|
86
|
-
|
87
|
-
protected
|
88
|
-
|
89
|
-
['distinct','company'].each do |x|
|
90
|
-
define_method :"#{x}_id_from_parameters" do |parameters|
|
91
|
-
id = parameters[:"#{x}_id"]
|
92
|
-
id = self.instance_variable_get("@#{x}_id") unless id
|
93
|
-
id
|
94
|
-
end
|
95
|
-
end
|
5
|
+
include TrakioClient
|
96
6
|
|
97
7
|
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require "trakio_client/end_point"
|
2
|
+
require "trakio_client/alias"
|
3
|
+
require "trakio_client/annotate"
|
4
|
+
require "trakio_client/company"
|
5
|
+
require "trakio_client/exceptions"
|
6
|
+
require "trakio_client/identify"
|
7
|
+
require "trakio_client/track"
|
8
|
+
require "trakio_client/version"
|
9
|
+
require "rest_client"
|
10
|
+
require "json"
|
11
|
+
require "date"
|
12
|
+
|
13
|
+
|
14
|
+
module TrakioClient
|
15
|
+
|
16
|
+
def self.included base
|
17
|
+
base.extend ClassMethods
|
18
|
+
base.send :attr_accessor, :api_token
|
19
|
+
base.send :attr_accessor, :https
|
20
|
+
base.send :attr_accessor, :host
|
21
|
+
base.send :attr_accessor, :channel
|
22
|
+
base.send :attr_accessor, :distinct_id
|
23
|
+
base.send :attr_accessor, :company_id
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
|
28
|
+
def init(*args)
|
29
|
+
api_token, params = args
|
30
|
+
raise Exceptions::InvalidToken.new('Missing API Token') unless api_token
|
31
|
+
raise Exceptions::NoDistinctIdForDefaultInstance if params and params.has_key?(:distinct_id)
|
32
|
+
raise Exceptions::NoCompanyIdForDefaultInstance if params and params.has_key?(:company_id)
|
33
|
+
@default_instance = self.new(*args)
|
34
|
+
end
|
35
|
+
|
36
|
+
def default_instance
|
37
|
+
raise Exceptions::Uninitiated unless @default_instance
|
38
|
+
@default_instance
|
39
|
+
end
|
40
|
+
|
41
|
+
def default_instance=(instance)
|
42
|
+
@default_instance = instance
|
43
|
+
end
|
44
|
+
|
45
|
+
def distinct_id value=nil
|
46
|
+
raise Exceptions::NoDistinctIdForDefaultInstance
|
47
|
+
end
|
48
|
+
alias :distinct_id= :distinct_id
|
49
|
+
|
50
|
+
def company_id value=nil
|
51
|
+
raise Exceptions::NoCompanyIdForDefaultInstance
|
52
|
+
end
|
53
|
+
alias :company_id= :company_id
|
54
|
+
|
55
|
+
def method_missing(method, *args, &block)
|
56
|
+
# passes to the default_instance so that
|
57
|
+
# Trakio.channel returns Trakio.default_instance.channel
|
58
|
+
@default_instance.send(method, *args, &block)
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize(*args)
|
64
|
+
api_token, params = args
|
65
|
+
api_token = self.class.default_instance.api_token unless api_token
|
66
|
+
|
67
|
+
@api_token = api_token or raise Exceptions::InvalidToken.new('Missing API Token')
|
68
|
+
@https = true
|
69
|
+
@host = 'api.trak.io/v1'
|
70
|
+
|
71
|
+
%w{https host channel distinct_id company_id}.each do |name|
|
72
|
+
instance_variable_set("@#{name}", params[name.to_sym]) if params && params.has_key?(name.to_sym)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
['Alias', 'Annotate', 'Company', 'Identify', 'Track'].each do |method_object|
|
77
|
+
TrakioClient.module_eval "
|
78
|
+
def #{method_object.downcase} *args
|
79
|
+
@#{method_object.downcase} ||= TrakioClient::#{method_object}.new(self)
|
80
|
+
@#{method_object.downcase}.run(*args)
|
81
|
+
end
|
82
|
+
"
|
83
|
+
end
|
84
|
+
|
85
|
+
def page_view *args
|
86
|
+
@track ||= Track.new(self)
|
87
|
+
@track.page_view(*args)
|
88
|
+
end
|
89
|
+
|
90
|
+
protected
|
91
|
+
|
92
|
+
['distinct','company'].each do |x|
|
93
|
+
define_method :"#{x}_id_from_parameters" do |parameters|
|
94
|
+
id = parameters[:"#{x}_id"]
|
95
|
+
id = self.instance_variable_get("@#{x}_id") unless id
|
96
|
+
id
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
module TrakioClient
|
2
2
|
class Alias < EndPoint
|
3
3
|
|
4
4
|
def run p = {}
|
@@ -10,19 +10,19 @@ class Trakio
|
|
10
10
|
distinct_id: distinct_id,
|
11
11
|
alias: alias_,
|
12
12
|
}
|
13
|
-
|
13
|
+
|
14
14
|
send_request('alias', params)
|
15
15
|
end
|
16
16
|
|
17
17
|
def check_parameters alias_, distinct_id
|
18
18
|
unless distinct_id
|
19
|
-
raise
|
19
|
+
raise Exceptions::MissingParameter.new('The `distinct_id` parameter must be provided.')
|
20
20
|
end
|
21
21
|
unless alias_
|
22
|
-
raise
|
22
|
+
raise Exceptions::MissingParameter.new('The `alias` parameter must be provided.')
|
23
23
|
end
|
24
24
|
unless alias_.is_a?(String) or alias_.is_a?(Array)
|
25
|
-
raise
|
25
|
+
raise Exceptions::InvalidParameter.new('The `alias` parameter must be a string or an array.')
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
module TrakioClient
|
2
2
|
class Annotate < EndPoint
|
3
3
|
|
4
4
|
def run p = {}
|
@@ -18,10 +18,10 @@ class Trakio
|
|
18
18
|
|
19
19
|
def check_parameters event, properties
|
20
20
|
unless event
|
21
|
-
raise
|
21
|
+
raise Exceptions::MissingParameter.new("The `event` parameter must be provided.")
|
22
22
|
end
|
23
23
|
unless properties.is_a?(Hash)
|
24
|
-
raise
|
24
|
+
raise Exceptions::InvalidParameter.new("The `properties` parameter must be a hash.")
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
module TrakioClient
|
2
2
|
class Company < EndPoint
|
3
3
|
|
4
4
|
def run p = {}
|
@@ -27,13 +27,13 @@ class Trakio
|
|
27
27
|
|
28
28
|
def check_parameters company_id, properties, distinct_ids
|
29
29
|
if !company_id
|
30
|
-
raise
|
30
|
+
raise Exceptions::MissingParameter.new('The `company_id` parameter must be provided.')
|
31
31
|
end
|
32
32
|
if !properties.is_a?(Hash)
|
33
|
-
raise
|
33
|
+
raise Exceptions::InvalidParameter.new("The `properties` parameter must be a hash.")
|
34
34
|
end
|
35
35
|
if !distinct_ids.is_a?(Array)
|
36
|
-
raise
|
36
|
+
raise Exceptions::InvalidParameter.new('The `people_distinct_ids` parameter must be an array.')
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
module TrakioClient
|
2
2
|
class EndPoint
|
3
3
|
|
4
4
|
extend Forwardable
|
@@ -27,7 +27,7 @@ class Trakio
|
|
27
27
|
|
28
28
|
# status must be error
|
29
29
|
# here we will raise the required exception as in the API
|
30
|
-
exception = constantize(result[:exception]
|
30
|
+
exception = constantize("TrakioClient::Exceptions::#{result[:exception]}") # name of the class
|
31
31
|
message = result[:message] # extra information for the exception
|
32
32
|
raise exception.new(message)
|
33
33
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
module TrakioClient
|
2
2
|
class Identify < EndPoint
|
3
3
|
|
4
4
|
def run p = {}
|
@@ -49,16 +49,16 @@ class Trakio
|
|
49
49
|
|
50
50
|
def check_parameters distinct_id, properties
|
51
51
|
unless properties.is_a?(Hash)
|
52
|
-
raise
|
52
|
+
raise Exceptions::InvalidParameter.new("The `properties` parameter must be a hash.")
|
53
53
|
end
|
54
54
|
unless distinct_id
|
55
|
-
raise
|
55
|
+
raise Exceptions::MissingParameter.new('The `distinct_id` parameter must be provided.')
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
def check_companies companies
|
60
60
|
unless companies.all?{ |x| x.is_a?(Hash) } && companies.all?{ |x| x.include? :company_id }
|
61
|
-
raise
|
61
|
+
raise Exceptions::InvalidProperty.new('The `companies` property must be an array of hashes each with a value for `company_id`')
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
module TrakioClient
|
2
2
|
class Track < EndPoint
|
3
3
|
|
4
4
|
def run p = {}
|
@@ -52,19 +52,19 @@ class Trakio
|
|
52
52
|
|
53
53
|
def check_parameters event, distinct_id, company_id, properties
|
54
54
|
unless event
|
55
|
-
raise
|
55
|
+
raise Exceptions::MissingParameter.new("The `event` parameter must be provided.")
|
56
56
|
end
|
57
57
|
unless distinct_id || company_id
|
58
|
-
raise
|
58
|
+
raise Exceptions::MissingParameter.new('Either a `company_id` or `distinct_id` parameter must be provided.')
|
59
59
|
end
|
60
60
|
unless properties.is_a?(Hash)
|
61
|
-
raise
|
61
|
+
raise Exceptions::InvalidParameter.new("The `properties` parameter must be a hash.")
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
65
|
def check_page_view_parameters url
|
66
66
|
unless url
|
67
|
-
raise
|
67
|
+
raise Exceptions::InvalidParameter.new("The `url` parameter must be provided.")
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -23,7 +23,7 @@ describe Trakio do
|
|
23
23
|
}).to_return(:body => {
|
24
24
|
status: 'error',
|
25
25
|
code: 401,
|
26
|
-
exception: "
|
26
|
+
exception: "InvalidToken",
|
27
27
|
message: "Missing or invalid API token.",
|
28
28
|
details: "You must provide a valid API token, see http://docs.trak.io/authentication.html."
|
29
29
|
}.to_json)
|
data/trakio-ruby.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require '
|
4
|
+
require 'trakio_client/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "trakio-ruby"
|
8
|
-
spec.version =
|
8
|
+
spec.version = TrakioClient::VERSION
|
9
9
|
spec.authors = ["Matthew Spence", "Tobie Warburton"]
|
10
10
|
spec.email = ["matt@trak.io", "tobie.warburton@gmail.com"]
|
11
11
|
spec.description = "Official trak.io ruby library for Ruby"
|
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.2.
|
4
|
+
version: 0.2.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-
|
12
|
+
date: 2014-10-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -154,14 +154,16 @@ files:
|
|
154
154
|
- Rakefile
|
155
155
|
- lib/trakio-ruby.rb
|
156
156
|
- lib/trakio.rb
|
157
|
-
- lib/trakio/
|
158
|
-
- lib/
|
159
|
-
- lib/
|
160
|
-
- lib/
|
161
|
-
- lib/
|
162
|
-
- lib/
|
163
|
-
- lib/
|
164
|
-
- lib/
|
157
|
+
- lib/trakio/client.rb
|
158
|
+
- lib/trakio_client.rb
|
159
|
+
- lib/trakio_client/alias.rb
|
160
|
+
- lib/trakio_client/annotate.rb
|
161
|
+
- lib/trakio_client/company.rb
|
162
|
+
- lib/trakio_client/end_point.rb
|
163
|
+
- lib/trakio_client/exceptions.rb
|
164
|
+
- lib/trakio_client/identify.rb
|
165
|
+
- lib/trakio_client/track.rb
|
166
|
+
- lib/trakio_client/version.rb
|
165
167
|
- spec/spec_helper.rb
|
166
168
|
- spec/trakio/alias_spec.rb
|
167
169
|
- spec/trakio/annotate_spec.rb
|
data/lib/trakio/version.rb
DELETED