class TelephoneNumber::TestDataImporter

Constants

COUNTRIES
URL

Public Class Methods

fetch_data(output_hash) click to toggle source
# File lib/importers/test_data_importer.rb, line 73
def self.fetch_data(output_hash)
  COUNTRIES.each do |key, value|
    output_hash[key] = {}

    value.each_with_index do |num, counter|
      page = HTTParty.get(format(URL, num, key.to_s))
      parsed_page = Nokogiri::HTML.parse(page)
      body = parsed_page.elements.first.elements.css('body').first
      parsed_data = parse_remote_data(counter, body.elements.css('table')[2])
      output_hash[key].merge!(parsed_data)
    end
  end
  return output_hash
end
import!() click to toggle source
# File lib/importers/test_data_importer.rb, line 66
def self.import!
  output_hash = {}
  fetch_data(output_hash)
  write_file(output_hash)
  true
end
parse_remote_data(counter, table) click to toggle source
# File lib/importers/test_data_importer.rb, line 88
def self.parse_remote_data(counter, table)
  output = { counter.to_s => {} }
  table.elements.each do |row|
    next if row.elements.one?
    key = case row.elements.css('th').text
          when 'E164 format'
            :e164_formatted
          when 'National format'
            :national_formatted
          when 'International format'
            :international_formatted
          end

    output[counter.to_s][key] = row.elements.css('td').text if key
  end
  return output
end
write_file(data) click to toggle source
# File lib/importers/test_data_importer.rb, line 106
def self.write_file(data)
  File.open('test/valid_numbers.yml', 'w') do |file|
    file.write "# This file is generated automatically by TestDataImporter. \n" \
      "# Any changes made to this file will be overridden next time test data is generated. \n" \
      "# Please edit TestDataImporter if you need to add test cases. \n"

    file.write data.to_yaml
  end
end