Class: AdLocalize::CsvParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ad_localize/csv_parser.rb

Constant Summary collapse

CSV_WORDING_KEYS_COLUMN =
"key"
PLURAL_KEY_REGEXP =
/\#\#\{([A-Za-z]+)\}/
ADAPTIVE_KEY_REGEXP =
/\#\#\{(\d+)\}/
INFO_PLIST_KEY_REGEXP =
/(NS.+UsageDescription)|(CF.+Name)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCsvParser

Returns a new instance of CsvParser.



10
11
12
# File 'lib/ad_localize/csv_parser.rb', line 10

def initialize
  @locales = []
end

Instance Attribute Details

#localesObject

Returns the value of attribute locales.



8
9
10
# File 'lib/ad_localize/csv_parser.rb', line 8

def locales
  @locales
end

Instance Method Details

#extract_data(file_name) ⇒ Object

Raise a StandardError if no locale is detected in the csv



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ad_localize/csv_parser.rb', line 15

def extract_data(file_name)
  data = {}
  CSV.foreach(file_name, headers: true, skip_blanks: true) do |row|
    validity_status = check_row(row)
    if validity_status.zero?
      keys_column_index = row.index(CSV_WORDING_KEYS_COLUMN)
      fields = row.fields
      LOGGER.log(:warn, :yellow, "Missing key in line #{$.}") unless fields[keys_column_index...fields.count].all?(&:nil?)
      next
    elsif validity_status == -1
      LOGGER.log(:error, :red, "[CSV FORMAT] #{file_name} is not a valid file")
      exit
    else
      find_locales(row) if locales.empty?
      row_data = parse_row(row)
      data.deep_merge!(row_data) unless row_data.nil?
    end
  end
  remove_numeral_conflicts!(data)
  data
end