Class: Flydata::RollbarHookSetup

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/error_reporting.rb

Instance Method Summary collapse

Constructor Details

#initialize(log, access_token = ENV['ROLLBAR_ACCESS_TOKEN'], environment = ENV['ROLLBAR_ENV']) ⇒ RollbarHookSetup

Returns a new instance of RollbarHookSetup.


7
8
9
10
11
12
13
# File 'lib/flydata/error_reporting.rb', line 7

def initialize(log,
    access_token=ENV['ROLLBAR_ACCESS_TOKEN'],
    environment=ENV['ROLLBAR_ENV'])
  @log = log
  @access_token = access_token
  @environment = environment
end

Instance Method Details

#already_setup?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'lib/flydata/error_reporting.rb', line 32

def already_setup?
  @log.instance_variable_defined?('@_rollbar_hook_enabled')
end

#config_available?Boolean

Returns:

  • (Boolean)

23
24
25
26
27
28
29
30
# File 'lib/flydata/error_reporting.rb', line 23

def config_available?
  return false unless @access_token
  return false if @access_token.empty?

  return false unless @environment
  return false if @environment.empty?
  true
end

#rollbar_configureObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/flydata/error_reporting.rb', line 49

def rollbar_configure
  return unless rollbar_installed?
  require rollbar_gem_name

  Rollbar.configure do |config|
    # Only actually send rollbar errors to endpoint in production and staging.
    config.enabled = ['production', 'staging'].include?(@environment)
    config.access_token = @access_token
    config.environment = @environment || 'development'

    config.custom_data_method = lambda do |message, exception, context|
      { flydata_home: FLYDATA_HOME }
    end
  end
end

#rollbar_flag?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/flydata/error_reporting.rb', line 19

def rollbar_flag?
  File.exists?(File.join(FLYDATA_HOME, 'use_rollbar'))
end

#rollbar_gem_nameObject

Use to stub in tests


37
38
39
# File 'lib/flydata/error_reporting.rb', line 37

def rollbar_gem_name
  'rollbar'
end

#rollbar_installed?Boolean

Returns:

  • (Boolean)

41
42
43
44
45
46
47
# File 'lib/flydata/error_reporting.rb', line 41

def rollbar_installed?
  if Gem::Specification.respond_to?(:find_all_by_name)
    Gem::Specification.find_all_by_name(rollbar_gem_name).any?
  else
    Gem.source_index.find_name(rollbar_gem_name).first
  end
end

#setupObject


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flydata/error_reporting.rb', line 65

def setup
  return false unless should_setup?

  rollbar_configure

  old_error = @log.method(:error)

  # Replace the existing logger's error method so that it will report to rollbar messages or exceptions
  # when .error is called.
  @log.define_singleton_method :error do |*args|
    old_error.call(*args)

    if $!.nil?
      Rollbar.error(args.first) # Just the message portion
    else
      Rollbar.error($!) # Send the exception details
    end
  end

  # Make this method idempotent: future calls will have no effect now.
  @log.instance_variable_set('@_rollbar_hook_enabled', true)
  true
end

#should_setup?Boolean

Returns:

  • (Boolean)

15
16
17
# File 'lib/flydata/error_reporting.rb', line 15

def should_setup?
  (!already_setup?) && rollbar_installed? && rollbar_flag? && config_available?
end