Class: Honeybadger::Rack::UserFeedback
- Inherits:
-
Object
- Object
- Honeybadger::Rack::UserFeedback
- Extended by:
- Forwardable
- Defined in:
- lib/honeybadger/rack/user_feedback.rb
Overview
Middleware for Rack applications. Adds a feedback form to the Rack response when an error has occurred.
Instance Method Summary collapse
- #action ⇒ Object
- #call(env) ⇒ Object
- #custom_template_file ⇒ Object
- #custom_template_file? ⇒ Boolean
-
#initialize(app, agent = nil) ⇒ UserFeedback
constructor
A new instance of UserFeedback.
- #render_form(error_id, action = action()) ⇒ Object
- #template_file ⇒ Object
Constructor Details
#initialize(app, agent = nil) ⇒ UserFeedback
Returns a new instance of UserFeedback.
24 25 26 27 |
# File 'lib/honeybadger/rack/user_feedback.rb', line 24 def initialize(app, agent = nil) @app = app @agent = agent.kind_of?(Agent) && agent end |
Instance Method Details
#action ⇒ Object
TODO:
Make this method and others actually private.
45 46 47 48 49 |
# File 'lib/honeybadger/rack/user_feedback.rb', line 45 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
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/honeybadger/rack/user_feedback.rb', line 29 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_file ⇒ Object
58 59 60 |
# File 'lib/honeybadger/rack/user_feedback.rb', line 58 def custom_template_file @custom_template_file ||= File.join(config[:root], 'lib', 'honeybadger', 'templates', 'feedback_form.erb') end |
#custom_template_file? ⇒ Boolean
63 64 65 |
# File 'lib/honeybadger/rack/user_feedback.rb', line 63 def custom_template_file? custom_template_file && File.exist?(custom_template_file) end |
#render_form(error_id, action = action()) ⇒ Object
52 53 54 55 |
# File 'lib/honeybadger/rack/user_feedback.rb', line 52 def render_form(error_id, action = action()) return unless action ERB.new(@template ||= File.read(template_file)).result(binding) end |
#template_file ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/honeybadger/rack/user_feedback.rb', line 68 def template_file if custom_template_file? custom_template_file else File.('../../templates/feedback_form.erb', __FILE__) end end |