Sha256: 8d369ac644eb7b620b54ddefe34b13c07aea8442238b3cc0ff20518e0e51abd2

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

require 'erb'
require 'uri'

module Honeybadger
  class UserFeedback
    TEMPLATE = File.read(File.expand_path('../templates/feedback_form.html.erb', __FILE__)).freeze

    def initialize(app)
      @app = app
    end

    def action
      config = Honeybadger.configuration
      URI.parse("#{config.protocol}://#{config.host}:#{config.port}/v1/feedback/").to_s
    rescue URI::InvalidURIError
      nil
    end

    def render_form(error_id, action = action)
      return unless action
      ERB.new(TEMPLATE).result(binding)
    end

    def enabled?
      Honeybadger.configuration.feedback && Honeybadger.configuration.features['feedback']
    end

    def call(env)
      status, headers, body = @app.call(env)
      if enabled? && 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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
honeybadger-1.10.3 lib/honeybadger/user_feedback.rb
honeybadger-1.10.2 lib/honeybadger/user_feedback.rb
honeybadger-1.10.1 lib/honeybadger/user_feedback.rb
honeybadger-1.10.0 lib/honeybadger/user_feedback.rb
honeybadger-1.10.0.beta2 lib/honeybadger/user_feedback.rb
honeybadger-1.10.0.beta1 lib/honeybadger/user_feedback.rb