Class: Abqari::Importers::Support::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/abqari/importers/support/report.rb

Overview

Accumulates per-post results during an import run and writes a markdown report (_import_report.md) the operator can spot-check in their editor. Gitignored — see lib/abqari/templates/site_skeleton/.gitignore.

Each entry has a status (:imported, :skipped, :failed) plus a short reason. Warnings (e.g. image download failures that didn't sink the post) live alongside the entry so the operator can see exactly what went wrong with which post, rather than a flat global warning log.

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, importer:) ⇒ Report

Returns a new instance of Report.



22
23
24
25
26
27
# File 'lib/abqari/importers/support/report.rb', line 22

def initialize(source:, importer:)
  @source   = source
  @importer = importer
  @entries  = []
  @started  = Time.now
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



20
21
22
# File 'lib/abqari/importers/support/report.rb', line 20

def entries
  @entries
end

#importerObject (readonly)

Returns the value of attribute importer.



20
21
22
# File 'lib/abqari/importers/support/report.rb', line 20

def importer
  @importer
end

#sourceObject (readonly)

Returns the value of attribute source.



20
21
22
# File 'lib/abqari/importers/support/report.rb', line 20

def source
  @source
end

Instance Method Details

#add(slug:, title:, status:, reason: nil, warnings: []) ⇒ Object



29
30
31
32
# File 'lib/abqari/importers/support/report.rb', line 29

def add(slug:, title:, status:, reason: nil, warnings: [])
  @entries << Entry.new(slug: slug, title: title, status: status,
                        reason: reason, warnings: Array(warnings))
end

#count(status) ⇒ Object



34
35
36
# File 'lib/abqari/importers/support/report.rb', line 34

def count(status)
  @entries.count { |e| e.status == status }
end

#summary_lineObject

Compact one-line summary for stdout at the end of the run.



76
77
78
79
80
81
82
83
# File 'lib/abqari/importers/support/report.rb', line 76

def summary_line
  warns = @entries.sum { |e| e.warnings.size }
  parts = ["#{count(:imported)} imported"]
  parts << "#{count(:skipped)} skipped" if count(:skipped).positive?
  parts << "#{count(:failed)} failed"   if count(:failed).positive?
  parts << "#{warns} warnings"          if warns.positive?
  "#{parts.join(', ')}."
end

#to_markdownObject

Returns the formatted markdown for the run. The caller writes it wherever it wants (we don't bake in a path so tests can round-trip without touching disk).



41
42
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
# File 'lib/abqari/importers/support/report.rb', line 41

def to_markdown
  lines = []
  lines << "# Import report"
  lines << ''
  lines << "- **Importer:** `#{@importer}`"
  lines << "- **Source:** `#{@source}`"
  lines << "- **Started:** #{@started.utc.iso8601}"
  lines << "- **Imported:** #{count(:imported)}"
  lines << "- **Skipped:**  #{count(:skipped)}"
  lines << "- **Failed:**   #{count(:failed)}"
  lines << ''
  lines << '---'
  lines << ''

  if @entries.empty?
    lines << '_No posts were processed._'
  else
    @entries.each do |e|
      lines << "## #{e.title.to_s.empty? ? '(untitled)' : e.title}"
      lines << ''
      lines << "- Slug: `#{e.slug}`"
      lines << "- Status: **#{e.status}**"
      lines << "- Reason: #{e.reason}" if e.reason && !e.reason.empty?
      if e.warnings.any?
        lines << '- Warnings:'
        e.warnings.each { |w| lines << "  - #{w}" }
      end
      lines << ''
    end
  end

  lines.join("\n") + "\n"
end