Module: Phonelib::DataImporterHelper

Included in:
Phonelib::DataImporter::Importer
Defined in:
lib/phonelib/data_importer_helper.rb

Overview

helper module for parsing raw libphonenumber data

Constant Summary collapse

XML_COMMENT_ATTRIBUTES =

xml comments attributes names that should not be parsed

%w(text comment)
XML_FORMAT_NAMES =

xml format attributes names

%w(intlFormat format)

Instance Method Summary collapse

Instance Method Details

#camel2snake(s) ⇒ Object

method that converts camel case to snake case



86
87
88
# File 'lib/phonelib/data_importer_helper.rb', line 86

def camel2snake(s)
  s.gsub(/[A-Z]+/) { |m| "_#{m.downcase}" }
end

#fill_prefixes(key, value, prefix, prefixes) ⇒ Object

method updates prefixes hash recursively



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/phonelib/data_importer_helper.rb', line 10

def fill_prefixes(key, value, prefix, prefixes)
  prefixes = {} if prefixes.nil?
  if prefix.size == 1
    prefixes[prefix.to_i] = {} unless prefixes[prefix.to_i]
    prefixes[prefix.to_i][key] = value
  else
    pr = prefix[0].to_i
    prefixes[pr] = fill_prefixes(key, value, prefix[1..-1], prefixes[pr])
  end
  prefixes
end

#get_hash_from_xml(data, type) ⇒ Object

method creates hash from xml elements/element attributes



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/phonelib/data_importer_helper.rb', line 35

def get_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|
        hash[name2sym(child.name)] = str_clean(child.children.first)
      end
  end
  hash
end

#is_not_format(name) ⇒ Object

method for checking if element name is not a format element



23
24
25
# File 'lib/phonelib/data_importer_helper.rb', line 23

def is_not_format(name)
  !XML_FORMAT_NAMES.include? name
end

#main_from_xml(file) ⇒ Object

get main body from parsed xml document



67
68
69
70
71
72
73
# File 'lib/phonelib/data_importer_helper.rb', line 67

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) ⇒ Object

helper that converts xml element name to symbol



81
82
83
# File 'lib/phonelib/data_importer_helper.rb', line 81

def name2sym(name)
  camel2snake(name).to_sym
end

#parse_raw_file(file) ⇒ Object

method parses raw data file



55
56
57
58
59
60
61
62
63
64
# File 'lib/phonelib/data_importer_helper.rb', line 55

def parse_raw_file(file)
  data = {}
  File.readlines(file).each do |line|
    line = str_clean line
    next if line.empty? || line[0] == '#'
    prefix, line_data = line.split('|')
    data[prefix] = line_data && line_data.split('&')
  end
  data
end

#str_clean(s, white_space = true) ⇒ Object

helper that cleans string



76
77
78
# File 'lib/phonelib/data_importer_helper.rb', line 76

def str_clean(s, white_space = true)
  s.to_s.tr(white_space ? " \n" : "\n", '')
end

#without_comments(data) ⇒ Object

method filters xml elements excluding comments elements



28
29
30
31
32
# File 'lib/phonelib/data_importer_helper.rb', line 28

def without_comments(data)
  data.select do |el|
    !XML_COMMENT_ATTRIBUTES.include? el.name
  end
end