Class: RockBooks::ReceiptsHyperlinkConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/rock_books/reports/helpers/receipts_hyperlink_converter.rb

Constant Summary collapse

RECEIPT_REGEX =
/Receipt:\s*(\S*)/
INVOICE_REGEX =
/Invoice:\s*(\S*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_string, html_filespec) ⇒ ReceiptsHyperlinkConverter

Returns a new instance of ReceiptsHyperlinkConverter.



13
14
15
16
# 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

Instance Attribute Details

#html_stringObject (readonly)

Returns the value of attribute html_string.



11
12
13
# File 'lib/rock_books/reports/helpers/receipts_hyperlink_converter.rb', line 11

def html_string
  @html_string
end

#num_dirs_upObject (readonly)

Returns the value of attribute num_dirs_up.



11
12
13
# File 'lib/rock_books/reports/helpers/receipts_hyperlink_converter.rb', line 11

def num_dirs_up
  @num_dirs_up
end

Class Method Details

.convert(html_string, html_filespec) ⇒ Object



4
5
6
# 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

Instance Method Details

#convertObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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