Sha256: 4ccd996313a6b86a860c14b71169688d5727f3972de5ff7ece6d5fadeee37cd2

Contents?: true

Size: 1.94 KB

Versions: 9

Compression:

Stored size: 1.94 KB

Contents

require 'erb'
require 'uri'
require 'forwardable'

begin
  require 'i18n'
rescue LoadError
  module Honeybadger
    module I18n
      def self.t(key, options={})
        options[:default]
      end
    end
  end
end

module Honeybadger
  module Rack
    class UserFeedback
      extend Forwardable

      def initialize(app, agent = nil)
        @app = app
        @agent = agent.kind_of?(Agent) ? agent : Honeybadger::Agent.instance
      end

      def call(env)
        return @app.call(env) unless config[:'feedback.enabled']
        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

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

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

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

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

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

      private

      attr_reader :agent
      def_delegator :agent, :config
      def_delegator :config, :logger
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
honeybadger-3.0.2 lib/honeybadger/rack/user_feedback.rb
honeybadger-3.0.1 lib/honeybadger/rack/user_feedback.rb
honeybadger-3.0.0 lib/honeybadger/rack/user_feedback.rb
honeybadger-3.0.0.beta6 lib/honeybadger/rack/user_feedback.rb
honeybadger-3.0.0.beta5 lib/honeybadger/rack/user_feedback.rb
honeybadger-3.0.0.beta4 lib/honeybadger/rack/user_feedback.rb
honeybadger-3.0.0.beta3 lib/honeybadger/rack/user_feedback.rb
honeybadger-3.0.0.beta2 lib/honeybadger/rack/user_feedback.rb
honeybadger-3.0.0.beta1 lib/honeybadger/rack/user_feedback.rb