# frozen_string_literal: true require 'nokogiri' require_relative './event' require_relative './updated' module ForwardCalendar class ForwardCalendar def initialize(file) @file = file end ## # Immidiately returns the Updated element as soon as it gets parsed. # (Avoids the complete traverse of the file if the Updated element is set # at the beggining of the file) # def updated Nokogiri::XML::Reader(@file).each do |node| next unless node.name == 'updated' && node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT return Updated.parse(Nokogiri::XML(node.outer_xml), single: true) end end ## # Yields each Event from the Snapshot file being parsed. # def each_event Nokogiri::XML::Reader(@file).each do |node| next unless node.name == 'event' && node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT parsed_event = Nokogiri::XML.parse(node.outer_xml).remove_namespaces! yield Event.parse(parsed_event, single: true) end end end end