class RockBooks::ReceiptsHyperlinkConverter

Constants

INVOICE_REGEX
RECEIPT_REGEX

Attributes

html_string[R]
num_dirs_up[R]

Public Class Methods

convert(html_string, html_filespec) click to toggle source
# File lib/rock_books/reports/helpers/receipts_hyperlink_converter.rb, line 4
def self.convert(html_string, html_filespec)
  ReceiptsHyperlinkConverter.new(html_string, html_filespec).convert
end
new(html_string, html_filespec) click to toggle source
# File lib/rock_books/reports/helpers/receipts_hyperlink_converter.rb, line 13
def initialize(html_string, html_filespec)
  @html_string = html_string
  @num_dirs_up = html_filespec.include?('/single-account/') ? 3 : 2
end

Public Instance Methods

convert() click to toggle source
# File lib/rock_books/reports/helpers/receipts_hyperlink_converter.rb, line 19
def convert
  process_link_type = ->(line, regex, dir_name) do
    matches = regex.match(line)
    if matches
      listed_filespec = matches[1]
      anchor_line(line, listed_filespec, dir_name)
    else
      line
    end
  end

  html_string.split("\n").map do |line|
    line = process_link_type.(line, RECEIPT_REGEX, 'receipts')
    process_link_type.(line, INVOICE_REGEX, 'invoices')
  end.join("\n")
end

Private Instance Methods

anchor_line(line, listed_filespec, dir_name) click to toggle source
# File lib/rock_books/reports/helpers/receipts_hyperlink_converter.rb, line 47
        def anchor_line(line, listed_filespec, dir_name)
  label = {
    'receipts' => 'Receipt',
    'invoices' => 'Invoice'
  }.fetch(dir_name)

  line.gsub( \
        /#{label}:\s*#{listed_filespec}/, \
        %Q{#{label}: <a href="#{dirized_filespec(listed_filespec, dir_name)}">#{listed_filespec}</a>})
end
dirized_filespec(listed_filespec, dir_name) click to toggle source

If the HTML file being created is in DATA_DIR/rockbooks-reports/html/single-account, then

the processed link should be '../../../receipts/[receipt_filespec]'

else it's in DATA_DIR/rockbooks-reports/html, and

the processed link should be '../../receipts/[receipt_filespec]'

`dir_name` will be 'receipts' or 'invoices'

# File lib/rock_books/reports/helpers/receipts_hyperlink_converter.rb, line 43
        def dirized_filespec(listed_filespec, dir_name)
  File.join(('../' * num_dirs_up), dir_name, listed_filespec)
end