whenauser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in whenauser.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ whenauser
2
+ =========
3
+
4
+ Connect your Rails applications to WhenAUser.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,38 @@
1
+ require 'active_support/core_ext/class/attribute'
2
+ require 'active_support/log_subscriber'
3
+
4
+ module WhenAUser
5
+ class RequestLogSubscriber < ActiveSupport::LogSubscriber
6
+ def process_action(data)
7
+ return unless WhenAUser.pageview_events_enabled?
8
+ payload = data.payload
9
+ return if (status = payload[:status] || 500).to_i == 302
10
+ now = Time.now
11
+ wau = {
12
+ :_actor => 'anonymous',
13
+ :_domain => 'pageview',
14
+ :_name => "#{payload[:params]['controller']}##{payload[:params]['action']}",
15
+ :_timestamp => now.to_i,
16
+ :nsecs => now.nsec,
17
+ :duration => "%.2f" % data.duration,
18
+ :method => payload[:method],
19
+ :path => payload[:path],
20
+ :format => payload[:format],
21
+ :status => status,
22
+ :params => payload[:params].except(*WhenAUser.filter_parameters)
23
+ }
24
+ if payload[:exception]
25
+ exception, message = payload[:exception]
26
+ wau = wau.merge(:exception => exception, :error_message => message)
27
+ else
28
+ wau = wau.merge(:view => "%.2f" % payload[:view_runtime], :db => "%.2f" % payload[:db_runtime])
29
+ end
30
+ wau = wau.merge(custom_data)
31
+ WhenAUser.send_event(wau)
32
+ end
33
+
34
+ def custom_data
35
+ WhenAUser.data[Thread.current] || {}
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/railtie'
2
+ require 'action_view/log_subscriber'
3
+ require 'action_controller/log_subscriber'
4
+
5
+ module Lograge
6
+ class Railtie < Rails::Railtie
7
+ config.whenauser = ActiveSupport::OrderedOptions.new
8
+
9
+ initializer :whenauser do |app|
10
+ WhenAUser.setup(app)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module WhenAUser
2
+ VERSION = '0.1.0'
3
+ end
data/lib/whenauser.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'whenauser/version'
2
+ require 'whenauser/log_subscriber'
3
+ require 'active_support/core_ext/module/attribute_accessors'
4
+ require 'faraday'
5
+ require 'faraday_middleware'
6
+
7
+ class ActionController::Base
8
+ def self.inherited(subclass)
9
+ super
10
+ subclass.prepend_before_filter :whenauser_disable_pageview_events
11
+ subclass.before_filter :whenauser_pageview_events
12
+ end
13
+
14
+ def whenauser_disable_pageview_events
15
+ WhenAUser.disable_pageview_events!
16
+ true
17
+ end
18
+
19
+ def whenauser_pageview_events
20
+ WhenAUser.enable_pageview_events!
21
+ true
22
+ end
23
+ end
24
+
25
+ module WhenAUser
26
+ mattr_accessor :webhook_url
27
+ mattr_accessor :token
28
+ mattr_accessor :endpoint
29
+ mattr_accessor :filter_parameters
30
+ mattr_accessor :data
31
+ mattr_accessor :state
32
+
33
+ def self.setup(app)
34
+ WhenAUser::RequestLogSubscriber.attach_to :action_controller
35
+ self.webhook_url = app.config.whenauser.webhook_url
36
+ self.token = app.config.whenauser.token
37
+ self.filter_parameters = app.config.filter_parameters || []
38
+ self.endpoint = Faraday::Connection.new webhook_url do |builder|
39
+ builder.request :json
40
+ builder.adapter Faraday.default_adapter
41
+ end
42
+ self.data = {}
43
+ self.state = {}
44
+ end
45
+
46
+ def self.custom_data=(hash)
47
+ data[Thread.current] = hash
48
+ end
49
+
50
+ def self.send_event(event)
51
+ endpoint.post token, event.to_json
52
+ end
53
+
54
+ def self.disable_pageview_events!
55
+ state[Thread.current] = :disabled
56
+ end
57
+
58
+ def self.enable_pageview_events!
59
+ state[Thread.current] = :enabled
60
+ end
61
+
62
+ def self.pageview_events_enabled?
63
+ state[Thread.current] == :enabled
64
+ end
65
+ end
66
+
67
+ require 'whenauser/railtie' if defined?(Rails)
data/whenauser.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'whenauser/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'whenauser'
7
+ s.version = WhenAUser::VERSION
8
+ s.authors = ['David Anderson']
9
+ s.email = ['david@alpinegizmo.com']
10
+ s.homepage = 'https://github.com/tractionlabs/whenauser'
11
+ s.summary = %q{Connect your Rails applications to WhenAUser}
12
+ s.description = %q{Connect your Rails applications to WhenAUser}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_runtime_dependency 'activesupport'
20
+ s.add_runtime_dependency 'actionpack'
21
+ s.add_runtime_dependency 'faraday', '~> 0.8.0'
22
+ s.add_runtime_dependency 'faraday_middleware'
23
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whenauser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Anderson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: actionpack
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.8.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday_middleware
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Connect your Rails applications to WhenAUser
79
+ email:
80
+ - david@alpinegizmo.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - README.md
88
+ - Rakefile
89
+ - lib/whenauser.rb
90
+ - lib/whenauser/log_subscriber.rb
91
+ - lib/whenauser/railtie.rb
92
+ - lib/whenauser/version.rb
93
+ - whenauser.gemspec
94
+ homepage: https://github.com/tractionlabs/whenauser
95
+ licenses: []
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.24
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Connect your Rails applications to WhenAUser
118
+ test_files: []