preact 0.5.1 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/preact.rb +51 -13
- data/lib/preact/client.rb +7 -7
- data/lib/preact/configuration.rb +34 -7
- data/lib/preact/objects/account.rb +12 -0
- data/lib/preact/objects/event.rb +2 -2
- data/lib/preact/version.rb +1 -1
- data/readme.md +50 -8
- metadata +2 -1
data/lib/preact.rb
CHANGED
@@ -7,6 +7,8 @@ require 'preact/objects/event'
|
|
7
7
|
require 'preact/objects/action_event'
|
8
8
|
require 'preact/objects/message'
|
9
9
|
|
10
|
+
require 'logger'
|
11
|
+
|
10
12
|
module Preact
|
11
13
|
|
12
14
|
class << self
|
@@ -16,12 +18,17 @@ module Preact
|
|
16
18
|
|
17
19
|
attr_accessor :default_client
|
18
20
|
|
21
|
+
attr_accessor :logger
|
22
|
+
|
19
23
|
# Call this method to modify the configuration in your initializers
|
20
24
|
def configure
|
21
25
|
self.configuration ||= Configuration.new
|
22
26
|
|
23
27
|
yield(configuration) if block_given?
|
24
28
|
|
29
|
+
# Configure logger. Default to use Rails
|
30
|
+
self.logger = Logger.new(STDOUT) # ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
|
31
|
+
|
25
32
|
raise StandardError.new "Must specify project code and secret when configuring the Preact api client" unless configuration.valid?
|
26
33
|
|
27
34
|
if defined? Rails
|
@@ -29,25 +36,50 @@ module Preact
|
|
29
36
|
configuration.disabled = true if Rails.env.test?
|
30
37
|
end
|
31
38
|
end
|
32
|
-
|
33
|
-
def log_event(user,
|
39
|
+
|
40
|
+
def log_event(user, event, account = nil)
|
34
41
|
# Don't send requests when disabled
|
35
|
-
|
36
|
-
|
42
|
+
if configuration.disabled?
|
43
|
+
logger.info "[Preact] Logging is disabled, not logging event"
|
44
|
+
return nil
|
45
|
+
elsif user.nil?
|
46
|
+
logger.error "[Preact] No person specified, not logging event"
|
47
|
+
return nil
|
48
|
+
elsif event.nil?
|
49
|
+
logger.error "[Preact] No event specified, not logging event"
|
50
|
+
return nil
|
51
|
+
end
|
37
52
|
|
38
53
|
person = configuration.convert_to_person(user).as_json
|
39
|
-
event = ActionEvent.new({
|
40
|
-
:name => event_name,
|
41
|
-
:timestamp => Time.now.to_f
|
42
|
-
}.merge(extras)).as_json
|
43
54
|
|
44
|
-
|
55
|
+
if event.is_a?(String)
|
56
|
+
preact_event = ActionEvent.new({
|
57
|
+
:name => event,
|
58
|
+
:timestamp => Time.now.to_f
|
59
|
+
}).as_json
|
60
|
+
elsif event.is_a?(Hash)
|
61
|
+
preact_event = ActionEvent.new(event)
|
62
|
+
elsif !event.is_a?(ActionEvent)
|
63
|
+
raise StandardError.new "Unknown event class, must pass a string event name, event hash or ActionEvent object"
|
64
|
+
end
|
65
|
+
|
66
|
+
if account
|
67
|
+
# attach the account info to the event
|
68
|
+
preact_event.account = configuration.convert_to_account(account).as_json
|
69
|
+
end
|
70
|
+
|
71
|
+
send_log(person, preact_event)
|
45
72
|
end
|
46
73
|
|
47
74
|
def update_person(user)
|
48
75
|
# Don't send requests when disabled
|
49
|
-
|
50
|
-
|
76
|
+
if configuration.disabled?
|
77
|
+
logger.info "[Preact] Logging is disabled, not logging event"
|
78
|
+
return nil
|
79
|
+
elsif user.nil?
|
80
|
+
logger.info "[Preact] No person specified, not logging event"
|
81
|
+
return nil
|
82
|
+
end
|
51
83
|
|
52
84
|
person = configuration.convert_to_person(user).as_json
|
53
85
|
|
@@ -58,10 +90,16 @@ module Preact
|
|
58
90
|
# :subject - subject of the message
|
59
91
|
# :body - body of the message
|
60
92
|
# * any additional keys are used as extra options for the message (:note, etc.)
|
93
|
+
# DEPRECATED - DO NOT USE
|
61
94
|
def message(user, message = {})
|
62
95
|
# Don't send requests when disabled
|
63
|
-
|
64
|
-
|
96
|
+
if configuration.disabled?
|
97
|
+
logger.info "[Preact] Logging is disabled, not logging event"
|
98
|
+
return nil
|
99
|
+
elsif user.nil?
|
100
|
+
logger.info "[Preact] No person specified, not logging event"
|
101
|
+
return nil
|
102
|
+
end
|
65
103
|
|
66
104
|
person = configuration.convert_to_person(user).as_json
|
67
105
|
message_obj = Message.new(message).as_json
|
data/lib/preact/client.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'multi_json'
|
2
2
|
require 'rest_client'
|
3
3
|
|
4
|
-
|
4
|
+
module Preact
|
5
|
+
class Client
|
5
6
|
|
6
7
|
def create_event(person, action_event)
|
7
8
|
params = {
|
8
9
|
:person => person,
|
9
10
|
:event => action_event
|
10
11
|
}
|
11
|
-
|
12
12
|
data = post_request("/events", params)
|
13
13
|
end
|
14
14
|
|
@@ -25,7 +25,7 @@ class Preact::Client
|
|
25
25
|
def post_request(method, params={})
|
26
26
|
params = prepare_request_params(params)
|
27
27
|
|
28
|
-
|
28
|
+
Preact.logger.debug "[Preact] post_request to #{Preact.configuration.base_uri + method} with #{params.inspect}"
|
29
29
|
|
30
30
|
res = RestClient.post Preact.configuration.base_uri + method, params.to_json, :content_type => :json, :accept => :json
|
31
31
|
data = MultiJson.decode(res.body)
|
@@ -33,18 +33,18 @@ class Preact::Client
|
|
33
33
|
|
34
34
|
def get_request(method, params={})
|
35
35
|
params = prepare_request_params(params)
|
36
|
-
|
36
|
+
|
37
|
+
Preact.logger.debug "[Preact] get_request to #{Preact.configuration.base_uri + method} with #{params.inspect}"
|
38
|
+
|
37
39
|
res = RestClient.get Preact.configuration.base_uri + method, { :params => params }
|
38
40
|
data = MultiJson.decode(res.body)
|
39
41
|
end
|
40
42
|
|
41
43
|
def prepare_request_params(params = {})
|
42
44
|
params.merge({
|
43
|
-
# :user => Preact.configuration.code,
|
44
|
-
# :password => Preact.configuration.secret,
|
45
45
|
:format => "json"
|
46
46
|
})
|
47
47
|
end
|
48
48
|
|
49
|
-
|
49
|
+
end
|
50
50
|
end
|
data/lib/preact/configuration.rb
CHANGED
@@ -10,11 +10,15 @@ module Preact
|
|
10
10
|
# Default option settings
|
11
11
|
attr_accessor :disabled
|
12
12
|
attr_accessor :person_builder
|
13
|
+
attr_accessor :account_builder
|
14
|
+
|
15
|
+
# Logger settings
|
16
|
+
attr_accessor :logger
|
13
17
|
|
14
18
|
# The URL of the API server
|
15
|
-
|
16
|
-
|
17
|
-
|
19
|
+
attr_reader :scheme
|
20
|
+
attr_reader :host
|
21
|
+
attr_reader :base_path
|
18
22
|
|
19
23
|
def initialize
|
20
24
|
@scheme = 'https'
|
@@ -54,18 +58,34 @@ module Preact
|
|
54
58
|
else
|
55
59
|
raise "person_builder must be callable"
|
56
60
|
end
|
57
|
-
elsif user.respond_to?(:
|
58
|
-
Person.new(user.
|
61
|
+
elsif user.respond_to?(:to_preact)
|
62
|
+
Person.new(user.to_preact)
|
59
63
|
elsif user.is_a? Hash
|
60
64
|
Person.new(user)
|
61
65
|
else
|
62
|
-
Person.new(
|
66
|
+
Person.new(default_user_to_preact_hash(user))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def convert_to_account(account)
|
71
|
+
if account_builder
|
72
|
+
if account_builder.respond_to?(:call)
|
73
|
+
Account.new(account_builder.call(account))
|
74
|
+
else
|
75
|
+
raise "account_builder must be callable"
|
76
|
+
end
|
77
|
+
elsif account.respond_to?(:to_preact)
|
78
|
+
Account.new(account.to_preact)
|
79
|
+
elsif account.is_a? Hash
|
80
|
+
Account.new(account)
|
81
|
+
else
|
82
|
+
Account.new(default_account_to_preact_hash(account))
|
63
83
|
end
|
64
84
|
end
|
65
85
|
|
66
86
|
private
|
67
87
|
|
68
|
-
def
|
88
|
+
def default_user_to_preact_hash(user)
|
69
89
|
{
|
70
90
|
:name => user.name,
|
71
91
|
:email => user.email,
|
@@ -76,5 +96,12 @@ module Preact
|
|
76
96
|
}
|
77
97
|
end
|
78
98
|
|
99
|
+
def default_account_to_preact_hash(account)
|
100
|
+
{
|
101
|
+
:id => account.id,
|
102
|
+
:name => (account.name if account.respond_to?(:name))
|
103
|
+
}
|
104
|
+
end
|
105
|
+
|
79
106
|
end
|
80
107
|
end
|
data/lib/preact/objects/event.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
class Preact::Event < Preact::ApiObject
|
2
2
|
|
3
|
-
attr_accessor :name, :
|
3
|
+
attr_accessor :name, :timestamp, :account
|
4
4
|
|
5
5
|
def as_json(options={})
|
6
6
|
{
|
7
7
|
:name => self.name,
|
8
|
-
:magnitude => self.magnitude,
|
9
8
|
:timestamp => self.timestamp,
|
9
|
+
:account => self.account,
|
10
10
|
:source => Preact.configuration.user_agent # version of this logging library
|
11
11
|
}
|
12
12
|
end
|
data/lib/preact/version.rb
CHANGED
data/readme.md
CHANGED
@@ -25,14 +25,25 @@ Preact.configure do |config|
|
|
25
25
|
# config.disabled = Rails.env.development?
|
26
26
|
|
27
27
|
# Uncomment this this line to customize the data sent with your Person objects.
|
28
|
-
# Your procedure should return a Hash of attributes
|
28
|
+
# Your custom procedure should return a Hash of attributes
|
29
29
|
# config.person_builder = lambda {|user| {:keys => :values}}
|
30
|
+
|
31
|
+
# Defaults to Rails.logger or Logger.new(STDOUT). Set to Logger.new('/dev/null') to disable logging.
|
32
|
+
# config.logger = Logger.new('/dev/null')
|
30
33
|
end
|
31
34
|
```
|
32
35
|
|
33
36
|
Usage
|
34
37
|
---
|
35
38
|
|
39
|
+
The Preact.log_event method takes two required parameters and an optional third parameter.
|
40
|
+
|
41
|
+
You must pass both a `person` and an `event`.
|
42
|
+
|
43
|
+
The `person` parameter may be either a Hash or an ActiveRecord model (see below).
|
44
|
+
|
45
|
+
The `event` parameter may be either a String if you just are passing the event name, or it may be a Hash of the event object including other properties like `revenue`, `note` and a nested `extras` hash.
|
46
|
+
|
36
47
|
```ruby
|
37
48
|
person = {
|
38
49
|
:name => "Christopher Gooley",
|
@@ -49,10 +60,11 @@ person = {
|
|
49
60
|
#common event examples:
|
50
61
|
Preact.log_event(person, 'logged-in')
|
51
62
|
Preact.log_event(person, 'upgraded')
|
52
|
-
Preact.log_event(person, 'processed:payment', :revenue => 900) # revenue specified in cents
|
53
|
-
Preact.log_event(person,
|
63
|
+
Preact.log_event(person, { :name => 'processed:payment', :revenue => 900 }) # revenue specified in cents
|
64
|
+
Preact.log_event(person, { :name => 'uploaded:file', :note => "awesome_resume.pdf" })
|
54
65
|
|
55
|
-
Preact.log_event(person,
|
66
|
+
Preact.log_event(person, {
|
67
|
+
:name => 'purchased:item',
|
56
68
|
:note => "black shoes",
|
57
69
|
:revenue => 2500,
|
58
70
|
:extras => {
|
@@ -61,13 +73,23 @@ Preact.log_event(person, 'purchased:item', {
|
|
61
73
|
})
|
62
74
|
```
|
63
75
|
|
76
|
+
If you are a Preact B2B user, you should also log the `account` that this event occurred within. You can do that by passing a third parameter to Preact.log_event to specify the account information. The preferred method for `account` is to use the ActiveRecord integration outlined below.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
Preact.log_event(
|
80
|
+
{ :email => "bob@honda.com", :name => "Bob Smith" }, # person
|
81
|
+
{ :name => 'uploaded:file', :note => "awesome_resume.pdf" }, # event
|
82
|
+
{ :id => 1234, :name => "Honda"} # account
|
83
|
+
)
|
84
|
+
```
|
85
|
+
|
64
86
|
ActiveRecord Integration
|
65
87
|
---
|
66
|
-
In your `User` model, you can define a `
|
88
|
+
In your `User` model, you can define a `to_preact` method returning a Hash. Preact will detect and use this method on users passed to its logging events.
|
67
89
|
|
68
90
|
```ruby
|
69
91
|
class User < ActiveRecord::Base
|
70
|
-
def
|
92
|
+
def to_preact
|
71
93
|
{
|
72
94
|
:name => self.name,
|
73
95
|
:email => self.email,
|
@@ -83,8 +105,28 @@ end
|
|
83
105
|
```
|
84
106
|
|
85
107
|
```ruby
|
86
|
-
Preact.log_event(
|
87
|
-
Preact.log_event(
|
108
|
+
Preact.log_event(@current_user, 'restored_answer_data')
|
109
|
+
Preact.log_event(@current_user, { :name => 'updated-profile', :extras => {:twitter => "@gooley"} })
|
110
|
+
```
|
111
|
+
|
112
|
+
Likewise, if you are a Preact B2B user, you can define the `to_preact` method on the model that defines your Account grouping. For instance, if you attach your Users into "Projects" you would add the `to_preact` method into your Project model.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
class Project < ActiveRecord::Base
|
116
|
+
def to_preact
|
117
|
+
{
|
118
|
+
:name => self.name,
|
119
|
+
:id => self.id
|
120
|
+
}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
Then, you just pass that model to the log_event method and we will associate the user's action with that account.
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
Preact.log_event(@current_user, 'restored_answer_data', @current_user.project)
|
129
|
+
Preact.log_event(@current_user, { :name => 'updated-profile', :extras => {:twitter => "@gooley"} }, @current_user.project)
|
88
130
|
```
|
89
131
|
|
90
132
|
Sidekiq Integration
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: preact
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -118,6 +118,7 @@ extra_rdoc_files:
|
|
118
118
|
files:
|
119
119
|
- lib/preact/client.rb
|
120
120
|
- lib/preact/configuration.rb
|
121
|
+
- lib/preact/objects/account.rb
|
121
122
|
- lib/preact/objects/action_event.rb
|
122
123
|
- lib/preact/objects/api_object.rb
|
123
124
|
- lib/preact/objects/event.rb
|