Sha256: ac4d88cc9f91c76adee7bb320644bd149b177e3fc810deb571a3822dd63c3b67

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require "erb"
require "json"
require "securerandom"

module StackTrace
  module Viz
    class HTML
      LAYOUT_FILE = "../../public/main.html.erb"

      def initialize
        @traces = []
      end

      def save(file_path)
        file_path ||= default_file_path

        File.open(file_path, "w") { |f| f.write(content) }

        file_path
      end

      def add(trace, **extra)
        traces << {
          description: "Generated by StackTrace::Viz",
          **extra,
          trace: trace
        }
      end

      private

      attr_reader :traces

      def content
        erb.result_with_hash({ trace_data: trace_data })
      end

      def trace_data
        JSON.generate(traces)
      end

      def erb
        ERB.new(layout)
      end

      def layout
        File.read(layout_path)
      end

      def layout_path
        File.expand_path(LAYOUT_FILE, root_path)
      end

      def root_path
        File.dirname(__dir__)
      end

      def default_file_path
        File.expand_path(random_file_name, Dir.pwd)
      end

      def random_file_name
        "#{SecureRandom.uuid}.html"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stack_trace-viz-0.5.0 lib/stack_trace/viz/html.rb
stack_trace-viz-0.4.0 lib/stack_trace/viz/html.rb
stack_trace-viz-0.3.0 lib/stack_trace/viz/html.rb
stack_trace-viz-0.2.0 lib/stack_trace/viz/html.rb