Commit 4678fd1dac08c8a860754d6be4cccadca769f053

Authored by Stephen Crosby
2 parents d8135392 bb860a2c
Exists in master and in 1 other branch production

Merge pull request #702 from brianvanburken/refactoring

Legibility improvement on ErrorReport
Showing 1 changed file with 24 additions and 24 deletions   Show diff stats
app/models/error_report.rb
@@ -15,15 +15,18 @@ require 'hoptoad_notifier' @@ -15,15 +15,18 @@ require 'hoptoad_notifier'
15 # * <tt>:notifier</tt> - information to identify the source of the error report 15 # * <tt>:notifier</tt> - information to identify the source of the error report
16 # 16 #
17 class ErrorReport 17 class ErrorReport
18 - attr_reader :error_class, :message, :request, :server_environment, :api_key, :notifier, :user_attributes, :framework 18 + attr_reader :error_class, :message, :request, :server_environment, :api_key,
  19 + :notifier, :user_attributes, :framework, :notice
19 20
20 cattr_accessor :fingerprint_strategy do 21 cattr_accessor :fingerprint_strategy do
21 Fingerprint 22 Fingerprint
22 end 23 end
23 24
24 def initialize(xml_or_attributes) 25 def initialize(xml_or_attributes)
25 - @attributes = (xml_or_attributes.is_a?(String) ? Hoptoad.parse_xml!(xml_or_attributes) : xml_or_attributes).with_indifferent_access  
26 - @attributes.each{|k, v| instance_variable_set(:"@#{k}", v) } 26 + @attributes = xml_or_attributes
  27 + @attributes = Hoptoad.parse_xml!(@attributes) if @attributes.is_a? String
  28 + @attributes = @attributes.with_indifferent_access
  29 + @attributes.each { |k, v| instance_variable_set(:"@#{k}", v) }
27 end 30 end
28 31
29 def rails_env 32 def rails_env
@@ -33,30 +36,29 @@ class ErrorReport @@ -33,30 +36,29 @@ class ErrorReport
33 end 36 end
34 37
35 def app 38 def app
36 - @app ||= App.where(:api_key => api_key).first 39 + @app ||= App.where(api_key: api_key).first
37 end 40 end
38 41
39 def backtrace 42 def backtrace
40 - @normalized_backtrace ||= Backtrace.find_or_create(:raw => @backtrace) 43 + @normalized_backtrace ||= Backtrace.find_or_create(raw: @backtrace)
41 end 44 end
42 45
43 def generate_notice! 46 def generate_notice!
44 return unless valid? 47 return unless valid?
45 return @notice if @notice 48 return @notice if @notice
46 @notice = Notice.new( 49 @notice = Notice.new(
47 - :message => message,  
48 - :error_class => error_class,  
49 - :backtrace_id => backtrace.id,  
50 - :request => request,  
51 - :server_environment => server_environment,  
52 - :notifier => notifier,  
53 - :user_attributes => user_attributes,  
54 - :framework => framework 50 + message: message,
  51 + error_class: error_class,
  52 + backtrace_id: backtrace.id,
  53 + request: request,
  54 + server_environment: server_environment,
  55 + notifier: notifier,
  56 + user_attributes: user_attributes,
  57 + framework: framework
55 ) 58 )
56 error.notices << @notice 59 error.notices << @notice
57 @notice 60 @notice
58 end 61 end
59 - attr_reader :notice  
60 62
61 ## 63 ##
62 # Error associate to this error_report 64 # Error associate to this error_report
@@ -66,23 +68,22 @@ class ErrorReport @@ -66,23 +68,22 @@ class ErrorReport
66 # @return [ Error ] 68 # @return [ Error ]
67 def error 69 def error
68 @error ||= app.find_or_create_err!( 70 @error ||= app.find_or_create_err!(
69 - :error_class => error_class,  
70 - :environment => rails_env,  
71 - :fingerprint => fingerprint 71 + error_class: error_class,
  72 + environment: rails_env,
  73 + fingerprint: fingerprint
72 ) 74 )
73 end 75 end
74 76
75 def valid? 77 def valid?
76 - !!app 78 + app.present?
77 end 79 end
78 80
79 def should_keep? 81 def should_keep?
80 app_version = server_environment['app-version'] || '' 82 app_version = server_environment['app-version'] || ''
81 - if self.app.current_app_version.present? && ( app_version.length <= 0 || Gem::Version.new(app_version) < Gem::Version.new(self.app.current_app_version) )  
82 - false  
83 - else  
84 - true  
85 - end 83 + current_version = app.current_app_version
  84 + return true unless current_version.present?
  85 + return false if app_version.length <= 0
  86 + Gem::Version.new(app_version) >= Gem::Version.new(current_version)
86 end 87 end
87 88
88 private 89 private
@@ -90,5 +91,4 @@ class ErrorReport @@ -90,5 +91,4 @@ class ErrorReport
90 def fingerprint 91 def fingerprint
91 @fingerprint ||= fingerprint_strategy.generate(notice, api_key) 92 @fingerprint ||= fingerprint_strategy.generate(notice, api_key)
92 end 93 end
93 -  
94 end 94 end