Class: SandiMeter::HtmlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/sandi_meter/html_generator.rb

Instance Method Summary collapse

Instance Method Details

#copy_assets!(path) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sandi_meter/html_generator.rb', line 6

def copy_assets!(path)
  asset_dir_path = File.join(path, 'sandi_meter/assets')
  FileUtils.mkdir(asset_dir_path) unless Dir.exists?(asset_dir_path)


  Dir[File.join(File.dirname(__FILE__), "../../html/*.{js,css,png}")].each do |file|
    FileUtils.cp file, File.join(asset_dir_path, File.basename(file))
  end

  FileUtils.cp File.join(File.dirname(__FILE__), "../../html", "index.html"), File.join(path, 'sandi_meter', 'index.html')
end

#generate_data!(path) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sandi_meter/html_generator.rb', line 18

def generate_data!(path)
  raw_data = File.read(File.join(path, 'sandi_meter', 'sandi_meter.log')).split("\n")
  raw_data.map! { |row| row.split(';').map(&:to_i) }

  data = []
  raw_data.each do |row|
    hash = {}
    row.first(8).each_slice(2).each_with_index do |el, i|
      hash["r#{i + 1}0"] = el.first
      hash["r#{i + 1}1"] = el.last
    end

    hash['timestamp'] = row.last * 1000
    data << hash
  end

  index_file = File.join(path, 'sandi_meter', 'index.html')
  index = File.read(index_file)
  index.gsub!('<% plot_data %>', data.to_json)

  File.open(index_file, 'w') do |file|
    file.write(index)
  end
end

#generate_details!(path, data) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sandi_meter/html_generator.rb', line 43

def generate_details!(path, data)
  details = ""

  if data[:first_rule][:log][:classes].any?
    data[:first_rule][:log][:misindented_classes] ||= []
    data[:first_rule][:log][:misindented_classes].each do |class_params|
      class_params.insert(1, nil)
    end

    details << string_to_h2("Classes with 100+ lines")
    details << generate_details_block(
      ["Class name", "Size", "Path"],
      proper_data: data[:first_rule][:log][:classes],
      warning_data: data[:first_rule][:log][:misindented_classes],
      hint: "NOTE: Red classes are misindented. Start improving your project by fixing them.",
      warning_message: 'Misindented classes'
    )
  end

  if data[:second_rule][:log][:methods].any?
    data[:second_rule][:log][:misindented_methods] ||= []
    data[:second_rule][:log][:misindented_methods].each do |method_params|
      method_params.insert(2, nil)
    end

    details << string_to_h2("Methods with 5+ lines")
    details << generate_details_block(
      ["Class name", "Method name", "Size", "Path"],
      proper_data: data[:second_rule][:log][:methods].sort_by { |a| -a[2].to_i },
      warning_data: data[:second_rule][:log][:misindented_methods].sort_by { |a| -a[1].to_i },
      hint: "NOTE: Red methods are misindented. Continue your way to perfect code by fixing them.",
      warning_message: 'Misindented methods'
    )
  end

  if data[:third_rule][:log][:method_calls].any?
    details << string_to_h2("Method calls with 4+ arguments")
    details << generate_details_block(
      ["# of arguments", "Path"],
      proper_data: data[:third_rule][:log][:method_calls]
    )
  end

  if data[:fourth_rule][:log][:controllers].any?
    details << string_to_h2("Controllers with 1+ instance variables")
    details << generate_details_block(
      ["Controller name", "Action name", "Instance variables"],
      proper_data: data[:fourth_rule][:log][:controllers]
    )
  end

  index_file = File.join(path, 'sandi_meter', 'index.html')
  index = File.read(index_file)
  index.gsub!('<% details %>', details)

  File.open(index_file, 'w') do |file|
    file.write(index)
  end
end