Sha256: 9a6a4c63e48ea6209aa3f795261c283dd842f41a552707a2ea7257bcc31e7778

Contents?: true

Size: 1.3 KB

Versions: 13

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require 'yaml'

module CompaniesHouseHub
  class FilingHistoryFormatter
    def initialize(filing_history)
      @filing_history = filing_history
    end

    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

    def descriptions
      CompaniesHouseHub.load_yml('filing_history_descriptions')['description']
    end

    def cleanup(name)
      name.tr!('*', '')
      name.tr!("\s", '-')
    end

    # Replaces the placeholders in yaml descriptions with what we get from Companies House (and is
    # stored in FilingHistory#description_values).
    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
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
companies_house_hub-0.1.12 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.11 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.16 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.15 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.14 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.13 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.12 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.11 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.10 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.9 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.8 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.7 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.0.6 lib/companies_house_hub/filing_history_formatter.rb