module Phonelib::DataImporterHelper

@private helper module for parsing raw libphonenumber data

Constants

XML_COMMENT_ATTRIBUTES

xml comments attributes names that should not be parsed

XML_FORMAT_NAMES

xml format attributes names

Public Instance Methods

camel2snake(s) click to toggle source

method that converts camel case to snake case

# File lib/phonelib/data_importer_helper.rb, line 129
def camel2snake(s)
  s.gsub(/[A-Z]+/) { |m| "_#{m.downcase}" }
end
file_path(file) click to toggle source
# File lib/phonelib/data_importer_helper.rb, line 9
def file_path(file)
  "#{File.dirname(__FILE__)}/../../#{file}"
end
fill_prefixes(key, value, prefix, prefixes) click to toggle source

method updates prefixes hash recursively

# File lib/phonelib/data_importer_helper.rb, line 36
def fill_prefixes(key, value, prefix, prefixes)
  prefixes = {} if prefixes.nil?
  if prefix.size == 1
    pr = prefix.to_i
    prefixes[pr] ||= {}
    prefixes[pr][key] = value
  else
    pr = prefix[0].to_i
    prefixes[pr] = fill_prefixes(key, value, prefix[1..-1], prefixes[pr])
  end
  prefixes
end
hash_from_xml(data, type) click to toggle source

method creates hash from xml elements/element attributes

# File lib/phonelib/data_importer_helper.rb, line 62
def hash_from_xml(data, type)
  hash = {}
  case type
  when :attributes
    data.attributes.each do |k, v|
      hash[name2sym(k)] = str_clean(v)
    end
  when :children
    data.each do |f|
      hash[name2sym(f[0])] = f[1]
    end
  when :element
    data.elements.each do |child|
      if child.name == 'possibleLengths'
        hash[Core::POSSIBLE_PATTERN] =
            possible_length_regex(hash_from_xml(child, :attributes))
      else
        hash[name2sym(child.name)] = str_clean(child.children.first)
      end
    end
  end
  hash
end
main_from_xml(file) click to toggle source

get main body from parsed xml document

# File lib/phonelib/data_importer_helper.rb, line 110
def main_from_xml(file)
  xml_data = File.read(file)
  xml_data.force_encoding('utf-8')

  doc = Nokogiri::XML(xml_data)
  doc.elements.first.elements.first
end
name2sym(name) click to toggle source

helper that converts xml element name to symbol

# File lib/phonelib/data_importer_helper.rb, line 124
def name2sym(name)
  camel2snake(name).to_sym
end
not_format?(name) click to toggle source

method for checking if element name is not a format element

# File lib/phonelib/data_importer_helper.rb, line 50
def not_format?(name)
  !XML_FORMAT_NAMES.include? name
end
parse_raw_file(file) click to toggle source

method parses raw data file

# File lib/phonelib/data_importer_helper.rb, line 98
def parse_raw_file(file)
  data = {}
  File.readlines(file).each do |line|
    line = str_clean line, false
    next if line.empty? || line[0] == '#'
    prefix, line_data = line.split('|')
    data[prefix] = line_data && line_data.strip.split('&')
  end
  data
end
possible_length_regex(attributes) click to toggle source
# File lib/phonelib/data_importer_helper.rb, line 86
def possible_length_regex(attributes)
  return '' unless attributes[:national]
  attributes[:national].split(',').map do |m|
    if m.include? '-'
      "\\d{#{m.gsub(/[\[\]]/, '').gsub('-', ',')}}"
    else
      "\\d{#{m}}"
    end
  end.join('|')
end
save_data_file() click to toggle source

method saves parsed data to data files

# File lib/phonelib/data_importer_helper.rb, line 14
def save_data_file
  File.open(file_path(Phonelib::Core::FILE_MAIN_DATA), 'wb+') do |f|
    Marshal.dump(@data, f)
  end
end
save_extended_data_file() click to toggle source

method saves extended data file

# File lib/phonelib/data_importer_helper.rb, line 21
def save_extended_data_file
  extended = {
    Phonelib::Core::EXT_PREFIXES => @prefixes,
    Phonelib::Core::EXT_GEO_NAMES => @geo_names,
    Phonelib::Core::EXT_COUNTRY_NAMES => @countries,
    Phonelib::Core::EXT_TIMEZONES => @timezones,
    Phonelib::Core::EXT_CARRIERS => @carriers
  }
  File.open(file_path(Phonelib::Core::FILE_EXT_DATA), 'wb+') do |f|
    Marshal.dump(extended, f)
  end
  puts 'DATA SAVED'
end
str_clean(s, white_space = true) click to toggle source

helper that cleans string

# File lib/phonelib/data_importer_helper.rb, line 119
def str_clean(s, white_space = true)
  s.to_s.tr(white_space ? " \n" : "\n", '')
end
without_comments(data) click to toggle source

method filters xml elements excluding comments elements

# File lib/phonelib/data_importer_helper.rb, line 55
def without_comments(data)
  data.select do |el|
    !XML_COMMENT_ATTRIBUTES.include? el.name
  end
end