class CompaniesHouseHub::FilingHistoryFormatter

Public Class Methods

new(filing_history) click to toggle source
# File lib/companies_house_hub/filing_history_formatter.rb, line 7
def initialize(filing_history)
  @filing_history = filing_history
end

Public Instance Methods

formatted(format = 'pdf') click to toggle source
# File lib/companies_house_hub/filing_history_formatter.rb, line 11
def formatted(format = 'pdf')
  name = descriptions[@filing_history.description]
  name = name.downcase

  # If "made_up_date" exists then replace it with the filing date and don't include filing.date
  # in the file name (made_up_date will be used).
  if name =~ /{.*}/
    name = replace_placeholders(name)
    date = nil
  end

  cleanup(name)

  [name, date].compact.join('-') << ".#{format}"
end

Private Instance Methods

cleanup(name) click to toggle source
# File lib/companies_house_hub/filing_history_formatter.rb, line 33
def cleanup(name)
  name.tr!('*', '')
  name.tr!("\s", '-')
end
descriptions() click to toggle source
# File lib/companies_house_hub/filing_history_formatter.rb, line 29
def descriptions
  CompaniesHouseHub.load_yml('filing_history_descriptions')['description']
end
replace_placeholders(text) click to toggle source

Replaces the placeholders in yaml descriptions with what we get from Companies House (and is stored in FilingHistory#description_values).

# File lib/companies_house_hub/filing_history_formatter.rb, line 40
def replace_placeholders(text)
  matches = text.scan(/\{([a-z_]+)\}/).flatten

  return text unless matches.any?

  replaced = text.dup

  matches.each do |match|
    value = @filing_history.description_values.dig(match.to_sym)
    replaced.sub!(/{#{match}}/, value)
  end

  replaced
end