rorvswild 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: defe2d991680e75ff224dcdf201db4410853b1c4
4
- data.tar.gz: 6af12c56716629ac4b2a4abed82493639d49ba52
3
+ metadata.gz: f1688d79676d52dc69efdbf5cb2149f0f1e8af89
4
+ data.tar.gz: 3c3428fd9bb732a4b18513206cb240e7125ad7ee
5
5
  SHA512:
6
- metadata.gz: b2815352f7222b25537f8a075527cb0a11a8a108029f9a19c91459156784b286519a20321c3a51cc0b9f3e2ecf59da637f8c57d504ab97fbe4da5f58e0c30db9
7
- data.tar.gz: 26d522bbce03f923a85ce76f04e54d8476124b90a9d2e205ca85431a5a746a9ad0f369989f0544139093e6c5775cd1060f9aa92c264f5c12a85168ef4c96ddd5
6
+ metadata.gz: 2320d56b3563cf5a0257117e0b2eadf1958546aef3b40c84616da98d4068dc2a2844b879396e0532f0c910f8103f058896c8d963c11abcff8a24b19d03d2fd90
7
+ data.tar.gz: baffb82f1be10cf110c3d22230873af90e3bc2bc3269c04df3a1a4b896f504d062a85ff623927238f47a93f9840eb1e28ac880f470bccf68add3d8f2284bfdc3
@@ -1,3 +1,3 @@
1
1
  module RorVsWild
2
- VERSION = "0.2.2".freeze
2
+ VERSION = "0.2.3".freeze
3
3
  end
data/lib/rorvswild.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "rorvswild/version"
2
2
  require "json/ext"
3
3
  require "net/http"
4
+ require "logger"
4
5
  require "uri"
5
6
 
6
7
  module RorVsWild
@@ -46,23 +47,35 @@ module RorVsWild
46
47
  }
47
48
  end
48
49
 
49
- attr_reader :api_url, :api_key, :app_id, :explain_sql_threshold, :log_sql_threshold
50
+ attr_reader :api_url, :api_key, :app_id, :explain_sql_threshold, :log_sql_threshold, :app_root, :app_root_regex
50
51
 
51
52
  def initialize(config)
52
53
  config = self.class.default_config.merge(config)
53
54
  @explain_sql_threshold = config[:explain_sql_threshold]
54
55
  @log_sql_threshold = config[:log_sql_threshold]
56
+ @app_root = config[:app_root]
55
57
  @api_url = config[:api_url]
56
58
  @api_key = config[:api_key]
57
59
  @app_id = config[:app_id]
60
+ @logger = config[:logger]
58
61
  @data = {}
62
+
63
+ if defined?(Rails)
64
+ @logger ||= Rails.logger
65
+ @app_root ||= Rails.root.to_s
66
+ @parameter_filter = ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters)
67
+ end
68
+
69
+ @logger ||= Logger.new(STDERR)
70
+ @app_root_regex = app_root ? /\A#{app_root}/ : nil
71
+
59
72
  setup_callbacks
60
73
  RorVsWild.register_default_client(self)
61
74
  end
62
75
 
63
76
  def setup_callbacks
64
77
  client = self
65
- if defined?(Rails)
78
+ if defined?(ActiveSupport::Notifications)
66
79
  ActiveSupport::Notifications.subscribe("sql.active_record", &method(:after_sql_query))
67
80
  ActiveSupport::Notifications.subscribe("render_template.action_view", &method(:after_view_rendering))
68
81
  ActiveSupport::Notifications.subscribe("process_action.action_controller", &method(:after_http_request))
@@ -248,7 +261,8 @@ module RorVsWild
248
261
  GEM_HOME_REGEX = ENV["GEM_HOME"] ? /\A#{ENV["GEM_HOME"]}/.freeze : nil
249
262
 
250
263
  def extract_most_relevant_location(stack)
251
- location = stack.find { |str| !(str =~ GEM_HOME_REGEX) } if GEM_HOME_REGEX
264
+ location = stack.find { |str| str =~ app_root_regex && !(str =~ GEM_HOME_REGEX) } if app_root_regex
265
+ location ||= stack.find { |str| !(str =~ GEM_HOME_REGEX) } if GEM_HOME_REGEX
252
266
  split_file_location(relative_path(location || stack.first))
253
267
  end
254
268
 
@@ -270,7 +284,7 @@ module RorVsWild
270
284
  end
271
285
 
272
286
  def relative_path(path)
273
- defined?(Rails) ? path.sub(Rails.root.to_s, "".freeze) : path
287
+ app_root_regex ? path.sub(app_root_regex, "".freeze) : path
274
288
  end
275
289
 
276
290
  def exception_to_hash(exception, extra_details = nil)
@@ -298,12 +312,7 @@ module RorVsWild
298
312
  end
299
313
 
300
314
  def filter_sensitive_data(hash)
301
- if defined?(Rails)
302
- @sensitive_filter ||= ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters)
303
- @sensitive_filter.filter(hash)
304
- else
305
- hash
306
- end
315
+ @parameter_filter ? @parameter_filter.filter(hash) : hash
307
316
  end
308
317
 
309
318
  def filter_environment_variables(hash)
@@ -311,13 +320,8 @@ module RorVsWild
311
320
  end
312
321
 
313
322
  def log_error(exception)
314
- if defined?(Rails)
315
- Rails.logger.error("[RorVsWild] " + exception.inspect)
316
- Rails.logger.error("[RorVsWild] " + exception.backtrace.join("\n[RorVsWild] "))
317
- else
318
- $stderr.puts("[RorVsWild] " + exception.inspect)
319
- $stderr.puts("[RorVsWild] " + exception.backtrace.join("\n[RorVsWild] "))
320
- end
323
+ logger.puts("[RorVsWild] " + exception.inspect)
324
+ logger.puts("[RorVsWild] " + exception.backtrace.join("\n[RorVsWild] "))
321
325
  end
322
326
  end
323
327
 
@@ -57,34 +57,38 @@ class RorVsWildTest < MiniTest::Unit::TestCase
57
57
  end
58
58
 
59
59
  def test_extract_most_relevant_location
60
- callstack = ["#{ENV["GEM_HOME"]}/lib/sql.rb:1:in `method1'", "/app/models/user.rb:2:in `method2'"]
61
- assert_equal(%w[/app/models/user.rb 2 method2], client.send(:extract_most_relevant_location, callstack))
60
+ callstack = ["#{ENV["GEM_HOME"]}/lib/sql.rb:1:in `method1'", "/usr/lib/ruby/net/http.rb:2:in `method2'", "/rails/root/app/models/user.rb:3:in `method3'"]
61
+ assert_equal(%w[/app/models/user.rb 3 method3], client.send(:extract_most_relevant_location, callstack))
62
+
62
63
  assert_equal(["#{ENV["GEM_HOME"]}/lib/sql.rb", "1", "method1"], client.send(:extract_most_relevant_location, ["#{ENV["GEM_HOME"]}/lib/sql.rb:1:in `method1'"]))
63
64
  end
64
65
 
66
+ def test_extract_most_relevant_location_when_there_is_not_app_root
67
+ client = initialize_client
68
+ callstack = ["#{ENV["GEM_HOME"]}/lib/sql.rb:1:in `method1'", "/usr/lib/ruby/net/http.rb:2:in `method2'", "/rails/root/app/models/user.rb:3:in `method3'"]
69
+ assert_equal(%w[/usr/lib/ruby/net/http.rb 2 method2], client.send(:extract_most_relevant_location, callstack))
70
+ end
71
+
65
72
  def test_extract_most_relevant_location_when_there_is_no_method_name
66
73
  assert_equal(["/foo/bar.rb", "123", nil], client.send(:extract_most_relevant_location, ["/foo/bar.rb:123"]))
67
74
  end
68
75
 
76
+ def test_extract_most_relevant_location_when_gem_home_is_in_heroku_app_root
77
+ client = initialize_client(app_root: app_root = File.dirname(gem_home = ENV["GEM_HOME"]))
78
+ callstack = ["#{gem_home}/lib/sql.rb:1:in `method1'", "/usr/lib/ruby/net/http.rb:2:in `method2'", "#{app_root}/app/models/user.rb:3:in `method3'"]
79
+ assert_equal(["/app/models/user.rb", "3", "method3"], client.send(:extract_most_relevant_location, callstack))
80
+ end
81
+
69
82
  private
70
83
 
71
84
  def client
72
- if !@client
73
- RorVsWild::Client.any_instance.stubs(:setup_callbacks)
74
- @client ||= RorVsWild::Client.new({})
75
- @client.stubs(:post_request)
76
- @client.stubs(:post_task)
77
- end
78
- @client
85
+ @client ||= initialize_client(app_root: "/rails/root")
79
86
  end
80
- end
81
-
82
- # Simulate Rails.root
83
-
84
- require "pathname"
85
87
 
86
- module Rails
87
- def self.root
88
- Pathname.new("/rails/root")
88
+ def initialize_client(options = {})
89
+ client ||= RorVsWild::Client.new(options)
90
+ client.stubs(:post_request)
91
+ client.stubs(:post_task)
92
+ client
89
93
  end
90
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rorvswild
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Bernard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-14 00:00:00.000000000 Z
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler