class SipoMailer::Models::AddressBook
Attributes
book[R]
contacts[R]
Public Class Methods
find(id)
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 12 def self.find(id) new.find(id) end
new()
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 7 def initialize @file = load_book @contacts = without_dups end
Public Instance Methods
find(id)
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 16 def find(id) find_all(id).first end
Private Instance Methods
find_all(id)
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 22 def find_all(id) @contacts.select { |c| c.id == id.to_s } end
load_book()
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 42 def load_book file = SipoMailer.config.yaml[:address_book] if file.nil? print "Do nastavení dejte cestu k CSV souboru adresáře.\n" exit end open(file) rescue Errno::ENOENT print "Soubor #{file} nelze otevřít. Máte správně nastavenou cestu?\n" exit end
parse_contacts(header, book)
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 54 def parse_contacts(header, book) hashes = book.map { |contact| header.zip contact }.map(&:to_h) hashes.map do |contact| params = symbolize_keys(contact) if Models::Contact.valid?(params) Models::Contact.new(params) else row = contact.values.compact.join(', ') print "Nepodařilo se zpracovat řádku adresáře: #{row}. "\ "Je správně oddělen středníkem?\n" next end end.compact end
read_book()
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 77 def read_book book = [] File.open(@file, 'r') do |f| f.each_line do |line| book << line.delete("\n").split(';') end end header = book[0] book = book[1..-1] parse_contacts(header, book) end
symbolize_keys(hsh)
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 69 def symbolize_keys(hsh) {}.tap do |new_hash| hsh.each_pair do |key, value| new_hash[key.to_sym] = value end end end
without_dups()
click to toggle source
# File lib/sipo_mailer/models/address_book.rb, line 26 def without_dups contacts = read_book ids = contacts.map(&:id) dups = ids.select { |id| ids.count(id) > 1 }.uniq return contacts if dups.empty? print "Duplicitní kontakty nebudou zpracovány:\n" dups.each do |dup_id| contacts.each do |contact| printf(" %s: %s\n", contact.id, contact.email) if contact.id == dup_id end end contacts.reject { |contact| dups.include?(contact.id) } end