Last active
February 28, 2019 13:32
-
-
Save LucoLe/9e2e24916d1c3e76cf18901fa1917273 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ruby-progressbar' | |
res = ActiveRecord::Base.connection.execute(<<~SQL).to_a | |
select customer_reference_number, email, mobile_number, country_code | |
from users | |
where mobile_number is not null | |
and mobile_number != '' | |
SQL | |
progressbar = ProgressBar.create total: res.size, | |
format: '%a %e %P% Processed: %c from %C records', | |
title: 'Generating invalid number categories' | |
not_mobile_numbers = [] | |
not_supported_country = [] | |
invalid_mobile_numbers = [] | |
res.each do |row| | |
phone = Phonelib.parse row['mobile_number'] | |
if !phone.valid? | |
invalid_mobile_numbers << row | |
elsif !phone.types.include? :mobile | |
not_mobile_numbers << row | |
elsif (phone.countries & PhoneNumberInput::SMS_ENABLED_COUNTRY_CODES).empty? | |
not_supported_country << row | |
end | |
progressbar.increment | |
end | |
progressbar = ProgressBar.create total: not_mobile_numbers.size, | |
format: '%a %e %P% Processed: %c from %C records', | |
title: 'Exporting not_mobile_numbers CSV' | |
CSV.open("/home/users/LucoLe/not_mobile_numbers.csv", "wb") do |csv| | |
csv << ['User CRN', 'Email', 'Mobile number', 'Country', ''] | |
not_mobile_numbers.each do |row| | |
csv << [row['customer_reference_number'], row['email'], row['mobile_number'], row['country_code']] | |
progressbar.increment | |
end | |
end | |
progressbar = ProgressBar.create total: not_supported_country.size, | |
format: '%a %e %P% Processed: %c from %C records', | |
title: 'Exporting not_supported_country CSV' | |
CSV.open("/home/users/LucoLe/not_supported_country.csv", "wb") do |csv| | |
csv << ['User CRN', 'Email', 'Mobile number', 'Country', ''] | |
not_supported_country.each do |row| | |
csv << [row['customer_reference_number'], row['email'], row['mobile_number'], row['country_code']] | |
progressbar.increment | |
end | |
end | |
progressbar = ProgressBar.create total: invalid_mobile_numbers.size, | |
format: '%a %e %P% Processed: %c from %C records', | |
title: 'Exporting invalid_mobile_numbers CSV' | |
CSV.open("/home/users/LucoLe/invalid_mobile_numbers.csv", "wb") do |csv| | |
csv << ['User CRN', 'Email', 'Mobile number', 'Country', ''] | |
invalid_mobile_numbers.each do |row| | |
csv << [row['customer_reference_number'], row['email'], row['mobile_number'], row['country_code']] | |
progressbar.increment | |
end | |
end | |
progressbar.log 'Done' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment