Sha256: efe1ccf74954a7f94fc0e343f6ce25a0c6c2c5700cfc1ef4b1e5c7c7ba6166f5

Contents?: true

Size: 1.31 KB

Versions: 11

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'yaml'
# 02200605
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

11 entries across 11 versions & 1 rubygems

Version Path
companies_house_hub-0.1.10 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.9 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.8 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.7 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.6 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.5 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.4 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.3 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.2 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.1 lib/companies_house_hub/filing_history_formatter.rb
companies_house_hub-0.1.0 lib/companies_house_hub/filing_history_formatter.rb