class Fonbok::Reader

Public Class Methods

new(path) click to toggle source
# File lib/fonbok/reader.rb, line 7
def initialize(path)
  @path = path
  @doc = Nokogiri::XML(File.open(@path, 'r'))
end

Public Instance Methods

import() click to toggle source
# File lib/fonbok/reader.rb, line 12
def import
  phonebook_from_xml.tap do |phonebook|
    @doc.xpath('/phonebooks/phonebook/contact').each do |xml_contact|
      contact = contact_from_xml(xml_contact)
      xml_contact.xpath('telephony/number').each do |xml_number|
        number = number_from_xml(xml_number)
        contact.add_number(number)
      end
      phonebook.add_contact(contact)
    end
  end
end
inspect() click to toggle source
# File lib/fonbok/reader.rb, line 25
def inspect
  "<Fonbok::Reader path: #{@path}>"
end

Private Instance Methods

contact_from_xml(xml_contact) click to toggle source
# File lib/fonbok/reader.rb, line 36
def contact_from_xml(xml_contact)
  contact_name = xml_contact.xpath('person/realName').text
  Contact.new(name: contact_name)
end
number_from_xml(xml_number) click to toggle source
# File lib/fonbok/reader.rb, line 41
def number_from_xml(xml_number)
  NumberClassCaster.cast xml_number['type'], xml_number.text
end
phonebook_from_xml() click to toggle source
# File lib/fonbok/reader.rb, line 31
def phonebook_from_xml
  phonebook_name = @doc.at_xpath('/phonebooks/phonebook/@name').value
  Phonebook.new(name: phonebook_name)
end