preact 0.8.6 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,15 +2,7 @@ require 'preact/configuration'
2
2
  require 'preact/client'
3
3
  require 'preact/background_logger'
4
4
 
5
- require 'preact/objects/api_object'
6
- require 'preact/objects/person'
7
- require 'preact/objects/event'
8
- require 'preact/objects/action_event'
9
- require 'preact/objects/action_link'
10
- require 'preact/objects/account_event'
11
- require 'preact/objects/message'
12
- require 'preact/objects/account'
13
-
5
+ require 'json'
14
6
  require 'logger'
15
7
 
16
8
  module Preact
@@ -27,6 +19,7 @@ module Preact
27
19
  # Call this method to modify the configuration in your initializers
28
20
  def configure
29
21
  defaults = {}
22
+
30
23
  # try to use the yml config if we're on rails and it exists
31
24
  if defined? ::Rails
32
25
  config_yml = File.join(::Rails.root.to_s,"config","preact.yml")
@@ -55,6 +48,11 @@ module Preact
55
48
  require 'preact/warden'
56
49
  end
57
50
 
51
+ if defined? ::Preact::Sidekiq
52
+ # set up the default queue for the sidekiq job
53
+ ::Preact::Sidekiq::PreactLoggingWorker.sidekiq_options(:queue => self.configuration.sidekiq_queue)
54
+ end
55
+
58
56
  end
59
57
 
60
58
  def log_event(user, event, account = nil)
@@ -71,26 +69,26 @@ module Preact
71
69
  end
72
70
 
73
71
  if event.is_a?(String)
74
- preact_event = ActionEvent.new({
72
+ preact_event = {
75
73
  :name => event,
76
74
  :timestamp => Time.now.to_f
77
- })
75
+ }
78
76
  elsif event.is_a?(Hash)
79
- preact_event = ActionEvent.new(event)
80
- elsif event.is_a?(ActionEvent)
81
77
  preact_event = event
82
78
  else
83
- raise StandardError.new "Unknown event class, must pass a string event name, event hash or ActionEvent object"
79
+ raise StandardError.new "Unknown event class, must pass a string event name or event hash."
84
80
  end
85
81
 
86
82
  if account
87
83
  # attach the account info to the event
88
- preact_event.account = configuration.convert_to_account(account).as_json
84
+ preact_event[:account] = configuration.convert_to_account(account)
89
85
  end
90
86
 
91
87
  person = configuration.convert_to_person(user)
88
+
89
+ preact_event[:klass] = "actionevent"
92
90
 
93
- send_log(person.as_json, preact_event.as_json)
91
+ send_log(person, preact_event)
94
92
  end
95
93
 
96
94
  def log_account_event(event, account)
@@ -120,7 +118,7 @@ module Preact
120
118
  end
121
119
 
122
120
  # attach the account info to the event
123
- preact_event.account = configuration.convert_to_account(account).as_json
121
+ preact_event.account = configuration.convert_to_account(account)
124
122
 
125
123
  send_log(nil, preact_event.as_json)
126
124
  end
@@ -135,9 +133,9 @@ module Preact
135
133
  return nil
136
134
  end
137
135
 
138
- person = configuration.convert_to_person(user).as_json
136
+ person = configuration.convert_to_person(user)
139
137
 
140
- send_log(person)
138
+ client.update_person(person)
141
139
  end
142
140
 
143
141
  def update_account(account)
@@ -155,27 +153,6 @@ module Preact
155
153
  client.update_account(account)
156
154
  end
157
155
 
158
- # message - a Hash with the following required keys
159
- # :subject - subject of the message
160
- # :body - body of the message
161
- # * any additional keys are used as extra options for the message (:note, etc.)
162
- # DEPRECATED - DO NOT USE
163
- def message(user, message = {})
164
- # Don't send requests when disabled
165
- if configuration.disabled?
166
- logger.info "[Preact] Logging is disabled, not logging event"
167
- return nil
168
- elsif user.nil?
169
- logger.info "[Preact] No person specified, not logging event"
170
- return nil
171
- end
172
-
173
- person = configuration.convert_to_person(user).as_json
174
- message_obj = Message.new(message).as_json
175
-
176
- send_log(person, message_obj)
177
- end
178
-
179
156
  def client
180
157
  self.default_client ||= Client.new
181
158
  end
@@ -183,13 +160,12 @@ module Preact
183
160
  protected
184
161
 
185
162
  def send_log(person, event=nil)
186
- psn = person.as_json
187
- evt = event.nil? ? nil : event.as_json
163
+ psn = person.to_hash
164
+ evt = event.nil? ? nil : event.to_hash
188
165
 
189
- if defined?(Preact::Sidekiq)
166
+ if defined?(Preact::Sidekiq) && (configuration.logging_mode.nil? || configuration.logging_mode == :sidekiq)
190
167
  Preact::Sidekiq::PreactLoggingWorker.perform_async(psn, evt)
191
168
  else
192
- #client.create_event(psn, evt)
193
169
  # use the background thread logger
194
170
  Preact::BackgroundLogger.new.async.perform(psn, evt)
195
171
  end
@@ -4,25 +4,31 @@ require 'rest_client'
4
4
  module Preact
5
5
  class Client
6
6
 
7
- def create_event(person, action_event)
7
+ def create_event(person, event)
8
8
  params = {
9
- :person => person,
10
- :event => action_event
9
+ :person => Preact.configuration.prepare_person_hash(person),
10
+ :event => Preact.configuration.prepare_event_hash(event)
11
11
  }
12
+
13
+ if params[:event][:account]
14
+ params[:event][:account] = Preact.configuration.prepare_account_hash(params[:event][:account])
15
+ end
16
+
12
17
  data = post_request("/events", params)
13
18
  end
14
19
 
15
20
  def update_person(person)
16
21
  params = {
17
- :person => person
22
+ :person => Preact.configuration.prepare_person_hash(person)
18
23
  }
19
24
 
20
25
  data = post_request("/people", params)
21
26
  end
22
27
 
23
28
  def update_account(account)
29
+
24
30
  params = {
25
- :account => account
31
+ :account => Preact.configuration.prepare_account_hash(account)
26
32
  }
27
33
 
28
34
  data = post_request("/accounts", params)
@@ -35,7 +41,14 @@ module Preact
35
41
 
36
42
  Preact.logger.debug "[Preact] post_request to #{Preact.configuration.base_uri + method} with #{params.inspect}"
37
43
 
38
- res = RestClient.post Preact.configuration.base_uri + method, params.to_json, :content_type => :json, :accept => :json
44
+ res = RestClient::Request.execute({
45
+ :method => :post,
46
+ :url => Preact.configuration.base_uri + method,
47
+ :payload => params,
48
+ :headers => { :content_type => :json, :accept => :json },
49
+ :open_timeout => Preact.configuration.request_timeout,
50
+ :timeout => Preact.configuration.request_timeout
51
+ })
39
52
  data = MultiJson.decode(res.body)
40
53
  end
41
54
 
@@ -44,13 +57,22 @@ module Preact
44
57
 
45
58
  Preact.logger.debug "[Preact] get_request to #{Preact.configuration.base_uri + method} with #{params.inspect}"
46
59
 
47
- res = RestClient.get Preact.configuration.base_uri + method, { :params => params }
60
+ res = RestClient::Request.execute({
61
+ :method => :get,
62
+ :url => Preact.configuration.base_uri + method,
63
+ :params => params,
64
+ :headers => { :content_type => :json, :accept => :json },
65
+ :open_timeout => Preact.configuration.request_timeout,
66
+ :timeout => Preact.configuration.request_timeout
67
+ })
68
+
48
69
  data = MultiJson.decode(res.body)
49
70
  end
50
71
 
51
72
  def prepare_request_params(params = {})
52
73
  params.merge({
53
- :format => "json"
74
+ :format => "json",
75
+ :source => Preact.configuration.user_agent
54
76
  })
55
77
  end
56
78
 
@@ -13,6 +13,9 @@ module Preact
13
13
  attr_accessor :account_builder
14
14
  attr_accessor :autolog
15
15
  attr_accessor :autolog_ignored_actions
16
+ attr_accessor :sidekiq_queue
17
+ attr_accessor :request_timeout
18
+ attr_accessor :logging_mode
16
19
 
17
20
  # Logger settings
18
21
  attr_accessor :logger
@@ -31,6 +34,10 @@ module Preact
31
34
  @autolog_ignored_actions = []
32
35
  @disabled = false
33
36
  @person_builder = nil
37
+
38
+ @logging_mode = nil
39
+ @sidekiq_queue = :default
40
+ @request_timeout = 5
34
41
 
35
42
  @user_agent = "ruby-preact:#{Preact::VERSION}"
36
43
 
@@ -83,33 +90,76 @@ module Preact
83
90
  def convert_to_person(user)
84
91
  if person_builder
85
92
  if person_builder.respond_to?(:call)
86
- Person.new(person_builder.call(user))
93
+ hash = person_builder.call(user)
87
94
  else
88
95
  raise "person_builder must be callable"
89
96
  end
90
97
  elsif user.respond_to?(:to_preact)
91
- Person.new(user.to_preact)
98
+ hash = user.to_preact
92
99
  elsif user.is_a? Hash
93
- Person.new(user)
100
+ hash = user
94
101
  else
95
- Person.new(default_user_to_preact_hash(user))
102
+ hash = default_user_to_preact_hash(user)
96
103
  end
104
+
105
+ hash
97
106
  end
98
107
 
99
108
  def convert_to_account(account)
100
109
  if account_builder
101
110
  if account_builder.respond_to?(:call)
102
- Account.new(account_builder.call(account))
111
+ hash = account_builder.call(account)
103
112
  else
104
113
  raise "account_builder must be callable"
105
114
  end
106
115
  elsif account.respond_to?(:to_preact)
107
- Account.new(account.to_preact)
116
+ hash = account.to_preact
108
117
  elsif account.is_a? Hash
109
- Account.new(account)
118
+ hash = account
110
119
  else
111
- Account.new(default_account_to_preact_hash(account))
120
+ hash = default_account_to_preact_hash(account)
121
+ end
122
+
123
+ hash
124
+ end
125
+
126
+ def prepare_person_hash(person)
127
+ if external_id = person[:external_identifier] || person["external_identifier"]
128
+ person[:uid] ||= external_id
129
+ person.delete(:external_identifier)
130
+ person.delete("external_identifier")
131
+ end
132
+
133
+ if created_at = person[:created_at] || person["created_at"]
134
+ if created_at.respond_to?(:to_i)
135
+ created_at = created_at.to_i
136
+ end
137
+
138
+ person[:created_at] = created_at
139
+ person.delete("created_at")
112
140
  end
141
+
142
+ person
143
+ end
144
+
145
+ def prepare_account_hash(account)
146
+ # id for account should actually be passed as external_identifier
147
+ # make that correction here before sending (LEGACY SUPPORT)
148
+ external_id = account[:external_identifier] || account["external_identifier"]
149
+ if account_id = account[:id] || account["id"]
150
+ if external_id.nil?
151
+ account[:external_identifier] = account_id
152
+ account.delete(:id)
153
+ account.delete("id")
154
+ end
155
+ end
156
+
157
+ account
158
+ end
159
+
160
+ def prepare_event_hash(event)
161
+ event[:source] = Preact.configuration.user_agent
162
+ event
113
163
  end
114
164
 
115
165
  private
@@ -1,6 +1,8 @@
1
+ require 'sidekiq'
2
+
1
3
  module Preact::Sidekiq
2
4
  class PreactLoggingWorker
3
- include Sidekiq::Worker
5
+ include ::Sidekiq::Worker
4
6
 
5
7
  def perform(person, event=nil)
6
8
  client = Preact::Client.new
@@ -1,3 +1,3 @@
1
1
  module Preact
2
- VERSION = "0.8.6"
2
+ VERSION = "1.0.0"
3
3
  end
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.8.6
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -135,14 +135,9 @@ files:
135
135
  - lib/preact/background_logger.rb
136
136
  - lib/preact/client.rb
137
137
  - lib/preact/configuration.rb
138
- - lib/preact/objects/account.rb
139
- - lib/preact/objects/account_event.rb
140
- - lib/preact/objects/action_event.rb
141
138
  - lib/preact/objects/action_link.rb
142
139
  - lib/preact/objects/api_object.rb
143
140
  - lib/preact/objects/event.rb
144
- - lib/preact/objects/message.rb
145
- - lib/preact/objects/person.rb
146
141
  - lib/preact/rails/controllers/helpers.rb
147
142
  - lib/preact/rails.rb
148
143
  - lib/preact/sidekiq/preact_logging_worker.rb
@@ -1,21 +0,0 @@
1
- class Preact::Account < Preact::ApiObject
2
-
3
- attr_accessor :id, :name
4
- attr_accessor :license_status, :license_mrr, :license_type, :license_count, :license_value, :license_duration, :license_renewal
5
-
6
- def as_json(options={})
7
- {
8
- :name => self.name,
9
- :external_identifier=> self.id,
10
-
11
- :license_type => self.license_type,
12
- :license_count => self.license_count,
13
- :license_renewal => self.license_renewal,
14
- :license_value => self.license_value,
15
- :license_mrr => self.license_mrr,
16
- :license_duration => self.license_duration,
17
- :license_status => self.license_status
18
- }
19
- end
20
-
21
- end
@@ -1,12 +0,0 @@
1
- module Preact
2
- class AccountEvent < Event
3
-
4
- def as_json(options={})
5
- super(options).merge({
6
- :klass => "accountevent"
7
- })
8
- end
9
-
10
- end
11
-
12
- end
@@ -1,5 +0,0 @@
1
- module Preact
2
- class ActionEvent < Event
3
-
4
- end
5
- end
@@ -1,13 +0,0 @@
1
- class Preact::Message < Preact::Event
2
-
3
- attr_accessor :subject, :body
4
-
5
- def as_json(options={})
6
- super(options).merge({
7
- :klass => "message",
8
- :subject => self.subject,
9
- :body => self.body
10
- })
11
- end
12
-
13
- end
@@ -1,15 +0,0 @@
1
- class Preact::Person < Preact::ApiObject
2
-
3
- attr_accessor :name, :email, :external_identifier, :properties, :uid, :created_at
4
-
5
- def as_json(options={})
6
- d = {
7
- :name => self.name,
8
- :email => self.email,
9
- :uid => self.uid || self.external_identifier,
10
- :created_at => self.created_at ? self.created_at.to_i : nil,
11
- :properties => self.properties || {}
12
- }
13
- end
14
-
15
- end