Class: Honeybadger::Rack::UserFeedback

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/honeybadger/rack/user_feedback.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, config) ⇒ UserFeedback

Returns a new instance of UserFeedback.



22
23
24
25
# File 'lib/honeybadger/rack/user_feedback.rb', line 22

def initialize(app, config)
  @app = app
  @config = config
end

Instance Method Details

#actionObject



41
42
43
44
45
# File 'lib/honeybadger/rack/user_feedback.rb', line 41

def action
  URI.parse("#{config.connection_protocol}://#{config[:'connection.host']}:#{config.connection_port}/v1/feedback/").to_s
rescue URI::InvalidURIError
  nil
end

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/honeybadger/rack/user_feedback.rb', line 27

def call(env)
  status, headers, body = @app.call(env)
  if env['honeybadger.error_id'] && form = render_form(env['honeybadger.error_id'])
    new_body = []
    body.each do |chunk|
      new_body << chunk.gsub("<!-- HONEYBADGER FEEDBACK -->", form)
    end
    body.close if body.respond_to?(:close)
    headers['Content-Length'] = new_body.reduce(0) { |a,e| a += e.bytesize }.to_s
    body = new_body
  end
  [status, headers, body]
end

#custom_template_fileObject



52
53
54
# File 'lib/honeybadger/rack/user_feedback.rb', line 52

def custom_template_file
  @custom_template_file ||= File.join(config[:root], 'lib', 'honeybadger', 'templates', 'feedback_form.erb')
end

#custom_template_file?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/honeybadger/rack/user_feedback.rb', line 56

def custom_template_file?
  custom_template_file && File.exists?(custom_template_file)
end

#render_form(error_id, action = action()) ⇒ Object



47
48
49
50
# File 'lib/honeybadger/rack/user_feedback.rb', line 47

def render_form(error_id, action = action())
  return unless action
  ERB.new(@template ||= File.read(template_file)).result(binding)
end

#template_fileObject



60
61
62
63
64
65
66
# File 'lib/honeybadger/rack/user_feedback.rb', line 60

def template_file
  if custom_template_file?
    custom_template_file
  else
    File.expand_path('../../templates/feedback_form.erb', __FILE__)
  end
end