Module: Rails::Hyperdrive::AuditHeader
- Defined in:
- lib/rails/hyperdrive/audit_header.rb
Constant Summary collapse
- YAML_LINE =
/\A#\s*hyperdrive:/.freeze
- HTML_LINE =
/\A<!--\s*hyperdrive:.*-->\s*\z/.freeze
Class Method Summary collapse
- .build(source_gem:, version:, sha256:, installed_at: Time.now.utc) ⇒ Object
- .build_html(source_gem:, version:, sha256:, installed_at: Time.now.utc) ⇒ Object
- .fields(source_gem:, version:, sha256:, installed_at: Time.now.utc) ⇒ Object
- .inject_into_frontmatter(body, header_block) ⇒ Object
- .prepend_html(body, header_block) ⇒ Object
-
.strip(body) ⇒ Object
Strips only the contiguous injected block, never stray "# hyperdrive:" or "" lines elsewhere in the body.
- .strip_html(lines) ⇒ Object
- .strip_yaml(lines) ⇒ Object
Class Method Details
.build(source_gem:, version:, sha256:, installed_at: Time.now.utc) ⇒ Object
19 20 21 22 23 |
# File 'lib/rails/hyperdrive/audit_header.rb', line 19 def build(source_gem:, version:, sha256:, installed_at: Time.now.utc) fields(source_gem: source_gem, version: version, sha256: sha256, installed_at: installed_at) .map { |f| "# hyperdrive: #{f}" } .join("\n") end |
.build_html(source_gem:, version:, sha256:, installed_at: Time.now.utc) ⇒ Object
25 26 27 28 29 |
# File 'lib/rails/hyperdrive/audit_header.rb', line 25 def build_html(source_gem:, version:, sha256:, installed_at: Time.now.utc) fields(source_gem: source_gem, version: version, sha256: sha256, installed_at: installed_at) .map { |f| "<!-- hyperdrive: #{f} -->" } .join("\n") end |
.fields(source_gem:, version:, sha256:, installed_at: Time.now.utc) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/rails/hyperdrive/audit_header.rb', line 11 def fields(source_gem:, version:, sha256:, installed_at: Time.now.utc) [ "source=#{source_gem}@#{version}", "sha256=#{sha256}", "installed_at=#{installed_at.iso8601}" ] end |
.inject_into_frontmatter(body, header_block) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rails/hyperdrive/audit_header.rb', line 31 def inject_into_frontmatter(body, header_block) lines = body.lines unless lines.first&.strip == "---" return "---\n#{header_block}\n---\n\n#{body}" end closing_index = lines[1..].index { |l| l.strip == "---" } return body unless closing_index absolute_closing = closing_index + 1 lines.insert(absolute_closing, header_block + "\n") lines.join end |
.prepend_html(body, header_block) ⇒ Object
45 46 47 |
# File 'lib/rails/hyperdrive/audit_header.rb', line 45 def prepend_html(body, header_block) "#{header_block}\n\n#{body}" end |
.strip(body) ⇒ Object
Strips only the contiguous injected block, never stray "# hyperdrive:" or "" lines elsewhere in the body.
51 52 53 54 55 56 57 58 |
# File 'lib/rails/hyperdrive/audit_header.rb', line 51 def strip(body) lines = body.lines if lines.first&.strip == "---" strip_yaml(lines) else strip_html(lines) end end |
.strip_html(lines) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/rails/hyperdrive/audit_header.rb', line 73 def strip_html(lines) return lines.join unless lines.first =~ HTML_LINE i = 0 i += 1 while lines[i] =~ HTML_LINE i += 1 if lines[i] && lines[i].strip.empty? lines[i..].join end |
.strip_yaml(lines) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rails/hyperdrive/audit_header.rb', line 60 def strip_yaml(lines) closing_index = lines[1..].index { |l| l.strip == "---" } return lines.join unless closing_index absolute_closing = closing_index + 1 first = (1...absolute_closing).find { |i| lines[i] =~ YAML_LINE } return lines.join unless first last = first last += 1 while last < absolute_closing && lines[last] =~ YAML_LINE (lines[0...first] + lines[last..]).join end |