Class: Input::Rss

Inherits:
Object
  • Object
show all
Defined in:
lib/xify/input/rss.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Rss

Returns a new instance of Rss.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/xify/input/rss.rb', line 6

def initialize(config)
  @config = config

  @uri = URI.parse config['uri']

  working_dir = "#{ENV['HOME']}/.xify/Rss"
  Dir.mkdir working_dir rescue Errno::EEXIST
  @latest_file = "#{working_dir}/#{@uri.to_s.gsub(/\W+/, '_')}"

  @latest_time = Time.now
end

Instance Method Details

#updatesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/xify/input/rss.rb', line 18

def updates
  opts = {}
  opts[:first] = :now if @config['trigger']['now']
  Rufus::Scheduler.singleton.repeat @config['trigger']['schedule'], opts do |job|
    job_interval = job.last_time - @latest_time
    @latest_time = job.last_time
    open(@uri) do |rss|
      latest = Time.parse File.read(@latest_file) rescue Time.now - job_interval
      feed = RSS::Parser.parse(rss)
      feed.items
        .select do |item|
          item.pubDate > latest
        end
        .sort_by do |item|
          item.pubDate
        end
        .each do |item|
          yield Xify::Event.new(
            item.dc_creator,
            "*#{item.title}*\n\n#{item.description}",
            link: item.link,
            parent: feed.channel.title,
            parent_link: feed.channel.link,
            time: item.pubDate
          )
          update_latest item
        end
    end
  end
end