whenauser 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -57,7 +57,6 @@ to your Gemfile, and add something like this to `config/whenauser.rb`:
57
57
 
58
58
  See the [girl_friday wiki](https://github.com/mperham/girl_friday/wiki) for more information on how to use girl_friday.
59
59
 
60
-
61
60
  Options
62
61
  -------
63
62
 
@@ -67,6 +66,7 @@ WhenAUser::Rack accepts these options:
67
66
  * `webhook_url` -- defaults to 'http://whenauser.com/events'
68
67
  * `middleware` -- takes the symbol for a middleware and a block, configuring it
69
68
  * `queue` -- takes the class used for queuing (default: WhenAUser::MemoryQueue), and an optional hash
69
+ * `custom_data` -- this proc is passed env, and should return a hash to be merged into every event
70
70
 
71
71
  The `exceptions` middleware accepts these options:
72
72
 
@@ -74,13 +74,13 @@ The `exceptions` middleware accepts these options:
74
74
  * `ignore_exceptions` -- an array of exception class names, defaults to ['ActiveRecord::RecordNotFound', 'AbstractController::ActionNotFound', 'ActionController::RoutingError']
75
75
  * `ignore_crawlers` -- an array of strings to match against the user agent, includes a number of webcrawlers by default
76
76
  * `ignore_if` -- this proc is passed env and an exception; if it returns true, the exception is not reported to WhenAUser
77
- * `custom_data` -- this proc is passed env, and should return a hash to be merged into each event
77
+ * `custom_data` -- this proc is passed env, and should return a hash to be merged into each automatically generated exception event
78
78
 
79
79
  The `pageviews` middleware accepts these options:
80
80
 
81
81
  * `ignore_crawlers` -- an array of strings to match against the user agent, includes a number of webcrawlers by default
82
82
  * `ignore_if` -- this proc is passed env; if it returns true, the pageview is not reported to WhenAUser
83
- * `custom_data` -- this proc is passed env, and should return a hash to be merged into each event
83
+ * `custom_data` -- this proc is passed env, and should return a hash to be merged into each automatically generated event
84
84
 
85
85
  The WhenAUser::Pageviews middleware uses the same token as WhenAUser::Rack.
86
86
 
@@ -123,3 +123,8 @@ Use Cases
123
123
  * add a data point to a Librato graph
124
124
  * tag a user in WhenAUser, or in your CRM
125
125
  * segment a user in your email campaign tool
126
+
127
+ Compatibility
128
+ -------------
129
+
130
+ This gem can be used without Rails, but when used with Rails it depends on Rails 3 (we've tested with Rails 3.1 and 3.2). If you want to use girl_friday, you must use Ruby 1.9.2 or greater, JRuby, or Rubinius.
@@ -30,14 +30,14 @@ module WhenAUser
30
30
  @app.call(env)
31
31
  rescue Exception => exception
32
32
  env['whenauser.exception'] = exception
33
- send_event_now event(env, exception), @options[:token] unless should_be_ignored(env, exception)
33
+ send_event_now event(env, exception), @options[:token], env unless should_be_ignored(env, exception)
34
34
  raise exception
35
35
  end
36
36
  end
37
37
 
38
38
  private
39
- def send_event_now(event, token)
40
- WhenAUser.post_payload_to_token WhenAUser.prepare_event(event), token
39
+ def send_event_now(event, token, env)
40
+ WhenAUser.post_payload_to_token WhenAUser.prepare_event(event, env), token
41
41
  end
42
42
 
43
43
  def should_be_ignored(env, exception)
@@ -57,22 +57,16 @@ module WhenAUser
57
57
  end
58
58
 
59
59
  def event(env, exception)
60
- request = ActionDispatch::Request.new(env)
61
60
  backtrace = clean_backtrace(exception)
62
61
  event = {
63
62
  :_actor => actor_for_exception(exception),
64
63
  :_domain => 'exception',
65
64
  :_name => exception.class.to_s,
66
65
  :message => exception.to_s,
67
- :backtrace => backtrace.join("\n"),
68
- :request_url => request.url,
69
- :request_method => request.request_method,
70
- :params => request.params.except(*WhenAUser.filter_parameters),
71
- :user_agent => request.user_agent
66
+ :backtrace => backtrace.join("\n")
72
67
  }
73
- user = current_user(env)
68
+ user = WhenAUser.current_user(env)
74
69
  event.merge!(:current_user => user) if user
75
- event.merge!(:referer_url => request.referer) if request.referer
76
70
  event.merge!(@options[:custom_data].call(env))
77
71
  event
78
72
  end
@@ -7,18 +7,6 @@ module WhenAUser
7
7
  false
8
8
  end
9
9
 
10
- def current_user(env)
11
- controller = env['action_controller.instance']
12
- if current_user = controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
13
- [:login, :username, :email, :id].each do |field|
14
- return current_user.send(field) if current_user.respond_to?(field)
15
- end
16
- end
17
- nil
18
- rescue
19
- nil
20
- end
21
-
22
10
  def clean_backtrace(exception)
23
11
  if defined?(Rails) && Rails.respond_to?(:backtrace_cleaner)
24
12
  Rails.backtrace_cleaner.send(:filter, exception.backtrace)
@@ -54,15 +54,9 @@ module WhenAUser
54
54
  end
55
55
 
56
56
  def event(env, request, status, duration)
57
- actor = current_user(env) || 'anonymous'
58
57
  event = {
59
- :_actor => actor,
60
58
  :_domain => (status >= 400) ? 'pageerror' : 'pageview',
61
59
  :_name => page_event_name(request),
62
- :request_url => request.url,
63
- :request_method => request.request_method,
64
- :params => request.params.except(*WhenAUser.filter_parameters),
65
- :user_agent => request.user_agent,
66
60
  :status => status,
67
61
  :duration => "%.2f" % (duration * 1000)
68
62
  }
@@ -70,8 +64,6 @@ module WhenAUser
70
64
  event.merge!(:error => actor_for_exception(exception))
71
65
  event.merge!(:message => exception.to_s)
72
66
  end
73
- event.merge!(:referer_url => request.referer) if request.referer
74
- event.merge!(:rails_env => Rails.env) if defined?(Rails)
75
67
  event.merge!(@options[:custom_data].call(env))
76
68
  event
77
69
  end
@@ -1,6 +1,5 @@
1
1
  require 'rails/railtie'
2
- require 'action_view/log_subscriber'
3
- require 'action_controller/log_subscriber'
2
+ require 'active_record'
4
3
 
5
4
  module Whenauser
6
5
  class RailsConfigurator
@@ -82,5 +81,12 @@ module Whenauser
82
81
  end
83
82
  end
84
83
  end
84
+
85
+ config.after_initialize do
86
+ ActiveSupport.on_load(:active_record) do
87
+ require 'whenauser/active_record_extension'
88
+ include ActiveRecord::WhenAUserExtension
89
+ end
90
+ end
85
91
  end
86
92
  end
@@ -1,3 +1,3 @@
1
1
  module WhenAUser
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
3
3
  end
data/lib/whenauser.rb CHANGED
@@ -10,19 +10,21 @@ require 'logger'
10
10
  require 'active_support/core_ext/module/attribute_accessors'
11
11
 
12
12
  module WhenAUser
13
- mattr_accessor :filter_parameters, :buffer, :token, :webhook_url, :queue, :queue_options, :logger
13
+ mattr_accessor :filter_parameters, :buffer, :token, :webhook_url, :queue, :queue_options, :custom_data, :logger
14
14
 
15
15
  def self.default_ignored_crawlers
16
16
  %w(Baidu Gigabot Googlebot libwww-perl lwp-trivial msnbot SiteUptime Slurp WordPress ZIBB ZyBorg Yandex Jyxobot Huaweisymantecspider ApptusBot)
17
17
  end
18
18
 
19
19
  def self.send_event(event)
20
- WhenAUser.buffer << WhenAUser.prepare_event(event)
20
+ logger.debug "============= #{event.inspect} ============="
21
+ buffer << event
21
22
  end
22
23
 
23
- def self.flush
24
+ def self.flush(env={})
24
25
  return if (events = WhenAUser.buffer).empty?
25
26
  WhenAUser.buffer = []
27
+ events.each {|event| WhenAUser.prepare_event(event, env)}
26
28
  WhenAUser.queue.push(:payload => events)
27
29
  # WhenAUser.post_payload_to_token events.to_json, WhenAUser.token
28
30
  end
@@ -36,10 +38,34 @@ module WhenAUser
36
38
  http.request(req)
37
39
  end
38
40
  end
39
-
40
- def self.prepare_event(event)
41
- event[:_timestamp] = Time.now.to_f unless event[:_timestamp] || event['_timestamp']
41
+
42
+ def self.current_user(env)
43
+ if controller = env['action_controller.instance']
44
+ if current_user = controller.instance_variable_get('@current_user') || controller.instance_eval('current_user')
45
+ [:login, :username, :email, :id].each do |field|
46
+ return current_user.send(field) if current_user.respond_to?(field)
47
+ end
48
+ end
49
+ end
50
+ nil
51
+ rescue
52
+ nil
53
+ end
54
+
55
+ private
56
+ def self.prepare_event(event, env)
57
+ event[:_actor] ||= current_user(env) || 'anonymous'
58
+ event[:_timestamp] = Time.now.to_f unless (event[:_timestamp] || event['_timestamp'])
42
59
  event[:rails_env] = Rails.env if defined?(Rails)
60
+ unless env.empty?
61
+ event[:request_url] = env['whenauser.request_url']
62
+ event[:request_method] = env['whenauser.request_method']
63
+ event[:user_agent] = env['whenauser.user_agent']
64
+ event[:referer_url] = env['whenauser.referer_url'] if env['whenauser.referer_url']
65
+ end
66
+ request = ActionDispatch::Request.new(env)
67
+ event[:params] = request.params.except(*WhenAUser.filter_parameters)
68
+ event.merge!(WhenAUser.custom_data.call(env))
43
69
  event
44
70
  end
45
71
 
@@ -53,13 +79,19 @@ module WhenAUser
53
79
  WhenAUser.token = options[:token]
54
80
  WhenAUser.queue = options[:queue] || WhenAUser::MemoryQueue
55
81
  WhenAUser.queue_options = options[:queue_options] || {}
82
+ WhenAUser.custom_data = options[:custom_data] || lambda { |env| {} }
56
83
  end
57
84
 
58
85
  def call(env)
59
86
  WhenAUser.buffer = []
87
+ request = ActionDispatch::Request.new(env)
88
+ env['whenauser.request_url'] = request.url
89
+ env['whenauser.request_method'] = request.request_method
90
+ env['whenauser.user_agent'] = request.user_agent
91
+ env['whenauser.referer_url'] = request.referer
60
92
  @app.call(env)
61
93
  ensure
62
- WhenAUser.flush
94
+ WhenAUser.flush(env)
63
95
  end
64
96
  end
65
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whenauser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-08 00:00:00.000000000 Z
13
+ date: 2012-08-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport