Class: StructuredDataToSql::MysqlDumpXml::InvalidCharacterReport

Inherits:
Object
  • Object
show all
Defined in:
lib/structured_data_to_sql/mysql_dump_xml/invalid_character_report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path:, output_path:, summary_path:, events_path:, started_at:) ⇒ InvalidCharacterReport

Returns a new instance of InvalidCharacterReport.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/structured_data_to_sql/mysql_dump_xml/invalid_character_report.rb', line 13

def initialize(
  source_path:,
  output_path:,
  summary_path:,
  events_path:,
  started_at:
)
  @source_path = source_path.to_s
  @output_path = output_path.to_s
  @summary_path = Pathname(summary_path)
  @events_path = Pathname(events_path)
  @started_at = started_at
  @total = 0
  @per_file = Hash.new(0)
  @per_codepoint = Hash.new(0)
  @events = nil
end

Instance Attribute Details

#events_pathObject (readonly)

Returns the value of attribute events_path.



11
12
13
# File 'lib/structured_data_to_sql/mysql_dump_xml/invalid_character_report.rb', line 11

def events_path
  @events_path
end

#summary_pathObject (readonly)

Returns the value of attribute summary_path.



11
12
13
# File 'lib/structured_data_to_sql/mysql_dump_xml/invalid_character_report.rb', line 11

def summary_path
  @summary_path
end

#totalObject (readonly)

Returns the value of attribute total.



11
12
13
# File 'lib/structured_data_to_sql/mysql_dump_xml/invalid_character_report.rb', line 11

def total
  @total
end

Instance Method Details

#finish(success:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/structured_data_to_sql/mysql_dump_xml/invalid_character_report.rb', line 48

def finish(success:)
  return if @total.zero?

  @events&.close
  @events = nil
  FileUtils.mkdir_p(@summary_path.dirname)
  summary = {
    source_path: @source_path,
    output_path: @output_path,
    started_at: @started_at.utc.iso8601,
    completed_at: Time.now.utc.iso8601,
    success: success,
    total_scrubbed: @total,
    per_file_totals: @per_file,
    per_codepoint_totals: @per_codepoint,
    events_path: @events_path.to_s
  }
  File.write(
    @summary_path,
    JSON.pretty_generate(summary),
    mode: "w:utf-8"
  )
end

#record(file:, offset:, codepoint:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/structured_data_to_sql/mysql_dump_xml/invalid_character_report.rb', line 31

def record(file:, offset:, codepoint:)
  open_events
  hex = format("U+%04X", codepoint)
  event = {
    source_file: file.to_s,
    input_byte_offset: offset,
    codepoint: hex,
    decimal: codepoint,
    hex: hex,
    action: "removed"
  }
  @events.write("#{JSON.generate(event)}\n")
  @total += 1
  @per_file[file.to_s] += 1
  @per_codepoint[hex] += 1
end