preact 0.8.0 → 0.8.1

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.
@@ -0,0 +1,25 @@
1
+ class PreactGenerator < Rails::Generators::Base
2
+ desc "This generator creates a Preact initializer file at config/initializers"
3
+ argument :project_code, :type => :string
4
+ argument :api_secret, :type => :string
5
+
6
+ def create_initializer_file
7
+ create_file "config/initializers/preact.rb", <<-FILE
8
+ # Preact Logging Configuration
9
+ # see documentation about configuration options here: https://github.com/preact/preact-ruby
10
+ Preact.configure do |config|
11
+ config.code = "#{project_code}"
12
+ config.secret = "#{api_secret}"
13
+
14
+ # automatically log controller actions for authed users
15
+ # disable this if you want to only log manual events
16
+ config.autolog = true
17
+
18
+ # disable in Rails non-production environments
19
+ # uncomment this if you don't want to log development activities
20
+ #config.disabled = !Rails.env.production?
21
+ end
22
+ FILE
23
+ end
24
+
25
+ end
data/lib/preact.rb CHANGED
@@ -31,14 +31,21 @@ module Preact
31
31
  yield(configuration) if block_given?
32
32
 
33
33
  # Configure logger. Default to use Rails
34
- self.logger ||= configuration.logger || (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
34
+ self.logger ||= configuration.logger || (defined?(::Rails) ? ::Rails.logger : Logger.new(STDOUT))
35
35
 
36
36
  raise StandardError.new "Must specify project code and secret when configuring the Preact api client" unless configuration.valid?
37
37
 
38
- if defined? Rails
38
+ if defined? ::Rails
39
+ # load the rails extensions
40
+ require 'preact/rails'
41
+
39
42
  # never log things if we're in Rails test environment
40
- configuration.disabled = true if Rails.env.test?
43
+ configuration.disabled = true if ::Rails.env.test?
44
+ elsif defined? ::Warden
45
+ # not using rails, so try to use warden
46
+ require 'preact/warden'
41
47
  end
48
+
42
49
  end
43
50
 
44
51
  def log_event(user, event, account = nil)
@@ -11,6 +11,8 @@ module Preact
11
11
  attr_accessor :disabled
12
12
  attr_accessor :person_builder
13
13
  attr_accessor :account_builder
14
+ attr_accessor :autolog
15
+ attr_accessor :autolog_ignored_actions
14
16
 
15
17
  # Logger settings
16
18
  attr_accessor :logger
@@ -24,7 +26,9 @@ module Preact
24
26
  @scheme = 'https'
25
27
  @host = 'api.preact.io'
26
28
  @base_path = '/api/v2'
27
-
29
+
30
+ @autolog = false
31
+ @autolog_ignored_actions = []
28
32
  @disabled = false
29
33
  @person_builder = nil
30
34
 
@@ -50,6 +54,25 @@ module Preact
50
54
  def base_uri
51
55
  "#{scheme}://#{code}:#{secret}@#{host}#{base_path}"
52
56
  end
57
+
58
+ def autolog_enabled?
59
+ autolog == true
60
+ end
61
+
62
+ def autolog_should_log?(controller, action)
63
+ # check to see if we're ignoring this action
64
+ if autolog_ignored_actions && autolog_ignored_actions.is_a?(Array)
65
+
66
+ # check to see if we've ignored this specific action
67
+ return false if autolog_ignored_actions.include?("#{controller}##{action}")
68
+
69
+ # check to see if we've ignored all actions from this controller
70
+ return false if autolog_ignored_actions.include?("#{controller}#*")
71
+
72
+ end
73
+
74
+ true
75
+ end
53
76
 
54
77
  def convert_to_person(user)
55
78
  if person_builder
@@ -0,0 +1,5 @@
1
+ require 'preact/rails/controllers/helpers'
2
+
3
+ module Preact::Rails
4
+
5
+ end
@@ -0,0 +1,66 @@
1
+ module Preact
2
+ module Controllers
3
+ module Helpers
4
+ extend ActiveSupport::Concern
5
+
6
+ def preact_autolog_controller_action
7
+ # only track logged-in users
8
+ return true unless current_user
9
+
10
+ controller = params[:controller]
11
+ action = params[:action]
12
+ target_id = params[:id]
13
+ note = nil
14
+
15
+ return true unless controller && action
16
+
17
+ return true unless Preact.configuration.autolog_should_log?(controller, action)
18
+
19
+ event_name = "#{controller}##{action}"
20
+
21
+ if response.status == 404
22
+ note = "NOT FOUND"
23
+ elsif response.status == 500
24
+ event_name = "!#{event_name}--error"
25
+ end
26
+
27
+ event = {
28
+ :name => event_name,
29
+ :target_id => target_id,
30
+ :medium => "autolog-rails-v1.0",
31
+ :note => note,
32
+ :extras => {
33
+ :_ip => request.remote_ip,
34
+ :_url => request.url,
35
+ :_ua => request.env['HTTP_USER_AGENT']
36
+ }
37
+ }
38
+
39
+ preact_log(event)
40
+
41
+ true
42
+ end
43
+
44
+ # helper method on the controller to make logging events easy
45
+ def preact_log(event, account=nil)
46
+ if account
47
+ Preact.log_event(current_user, event, account)
48
+ else
49
+ Preact.log_event(current_user, event)
50
+ end
51
+ end
52
+
53
+ # attach the after_filter to all controllers if we've enabled autologging
54
+ if Preact.configuration.autolog_enabled?
55
+ ActiveSupport.on_load(:action_controller) do
56
+ after_filter :preact_autolog_controller_action
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+ end
63
+
64
+ class ActionController::Base
65
+ include Preact::Controllers::Helpers
66
+ end
@@ -1,3 +1,3 @@
1
1
  module Preact
2
- VERSION = "0.8.0"
2
+ VERSION = "0.8.1"
3
3
  end
@@ -0,0 +1,5 @@
1
+ require 'preact/warden/hooks'
2
+
3
+ module Preact::Warden
4
+
5
+ end
@@ -0,0 +1,7 @@
1
+ # after-auth hook to log the login
2
+ Warden::Manager.after_authentication do |user,auth,opts|
3
+ Preact.log_event(user, "login")
4
+ end
5
+ Warden::Manager.before_logout do |user,auth,opts|
6
+ Preact.log_event(user, "logout")
7
+ 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.0
4
+ version: 0.8.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -131,6 +131,7 @@ extensions: []
131
131
  extra_rdoc_files:
132
132
  - LICENSE.txt
133
133
  files:
134
+ - lib/generators/preact_generator.rb
134
135
  - lib/preact/background_logger.rb
135
136
  - lib/preact/client.rb
136
137
  - lib/preact/configuration.rb
@@ -142,9 +143,13 @@ files:
142
143
  - lib/preact/objects/event.rb
143
144
  - lib/preact/objects/message.rb
144
145
  - lib/preact/objects/person.rb
146
+ - lib/preact/rails/controllers/helpers.rb
147
+ - lib/preact/rails.rb
145
148
  - lib/preact/sidekiq/preact_logging_worker.rb
146
149
  - lib/preact/sidekiq.rb
147
150
  - lib/preact/version.rb
151
+ - lib/preact/warden/hooks.rb
152
+ - lib/preact/warden.rb
148
153
  - lib/preact.rb
149
154
  - readme.md
150
155
  - LICENSE.txt