Class: ProgramTV::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/program-tv/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(data, destination) ⇒ Writer

Returns a new instance of Writer.



7
8
9
10
# File 'lib/program-tv/writer.rb', line 7

def initialize(data, destination)
  @data = data
  @destination = destination
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/program-tv/writer.rb', line 12

def run
  @data.each do |schedule|
    today_schedule = []
    if File.exist?(xml_path(schedule))
      File.open(xml_path(schedule), "r+") do |file|
        today_schedule = Hash.from_xml(file.read)[:tv][:programme].map do |p|
          p[:attributes][:title] = p[:title]
          p[:attributes]
        end
      end
    end
    new_schedule = (today_schedule + schedule).
        select{ |p| p[:start] > Time.now.strftime("%Y%m%d000000 %z") }.
        uniq.
        sort{ |a,b| a[:start] <=> b[:start] }
    File.open(xml_path(schedule), "w+") do |file|
      file.write(build_xml(new_schedule))
    end
  end
end