Class: Maildis::RecipientParser
- Inherits:
-
Object
- Object
- Maildis::RecipientParser
- Defined in:
- lib/maildis/recipient_parser.rb
Constant Summary collapse
- @@expected_columns =
[:name, :email]
Class Method Summary collapse
- .empty_csv?(file_path) ⇒ Boolean
- .extract_data(file_path) ⇒ Object
- .extract_recipients(file_path) ⇒ Object
- .normalize_headers(headers) ⇒ Object
- .parse_csv(csv) ⇒ Object
- .parse_file(file_path) ⇒ Object
- .recipient_instance(row) ⇒ Object
- .valid_csv?(file_path) ⇒ Boolean
- .validate_file_path(file_path) ⇒ Object
- .validate_headers(headers, expected) ⇒ Object
Class Method Details
.empty_csv?(file_path) ⇒ Boolean
23 24 25 26 |
# File 'lib/maildis/recipient_parser.rb', line 23 def empty_csv?(file_path) csv = extract_data file_path csv.empty? end |
.extract_data(file_path) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/maildis/recipient_parser.rb', line 35 def extract_data(file_path) begin CSV.read(file_path, {headers: true}) rescue ArgumentError => e begin # Force UTF-8 data = IO.read(file_path).force_encoding("ISO-8859-1").encode("utf-8", replace: nil) CSV.parse(data, {headers: true}) rescue => e raise "Failed to parse CSV. Unable to force UTF-8 conversion."+ " " + e. end end end |
.extract_recipients(file_path) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/maildis/recipient_parser.rb', line 28 def extract_recipients(file_path) validate_file_path file_path csv = extract_data file_path validate_headers(normalize_headers(csv.headers), @@expected_columns) parse_csv(csv) end |
.normalize_headers(headers) ⇒ Object
71 72 73 |
# File 'lib/maildis/recipient_parser.rb', line 71 def normalize_headers(headers) headers.map(&:downcase) end |
.parse_csv(csv) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/maildis/recipient_parser.rb', line 49 def parse_csv(csv) result = [] csv.each do |r| row = {} r.to_hash.each_pair {|k,v| row.merge!({k.downcase => v})} result << recipient_instance(row) end result end |
.parse_file(file_path) ⇒ Object
67 68 69 |
# File 'lib/maildis/recipient_parser.rb', line 67 def parse_file(file_path) IO.read(file_path).force_encoding("ISO-8859-1").encode("utf-8", replace: nil) end |
.recipient_instance(row) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/maildis/recipient_parser.rb', line 59 def recipient_instance(row) recipient = Recipient.new row['name'], row['email'] row.each do |key, value| recipient.merge_fields << MergeField.new(key, value) unless %w{name email}.include? key end recipient end |
.valid_csv?(file_path) ⇒ Boolean
13 14 15 16 17 18 19 20 21 |
# File 'lib/maildis/recipient_parser.rb', line 13 def valid_csv?(file_path) csv = extract_data file_path begin validate_headers(normalize_headers(csv.headers), @@expected_columns) true rescue ValidationError => e false end end |
.validate_file_path(file_path) ⇒ Object
75 76 77 |
# File 'lib/maildis/recipient_parser.rb', line 75 def validate_file_path(file_path) raise ValidationError, "File not found: #{file_path}" unless File.exists?(file_path) end |
.validate_headers(headers, expected) ⇒ Object
79 80 81 82 83 |
# File 'lib/maildis/recipient_parser.rb', line 79 def validate_headers(headers, expected) expected.each do |header| raise ValidationError, "Missing '#{header.to_s}' column" unless headers.include? header.to_s end end |