Sha256: b31b975dbd0499826d9f45ea7ec208269e12814c633915e84b092b1d714a4dee

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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(trace, **extra)
        @trace = trace
        @extra = extra
      end

      def save(file_path)
        file_path ||= default_file_path

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

      private

      attr_reader :trace, :extra

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

      def trace_data
        JSON.generate({
          description: "Generated by StackTrace::Viz",
          **extra,
          trace: trace
        })
      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

1 entries across 1 versions & 1 rubygems

Version Path
stack_trace-viz-0.1.0 lib/stack_trace/viz/html.rb