Class: RSStimeline

Inherits:
Object
  • Object
show all
Includes:
RXFReadWriteModule
Defined in:
lib/rss_timeline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feeds = [], rssfile: 'timeline.rss', xslt: nil, filepath: '.', debug: false, target_filepath: nil) ⇒ RSStimeline

Returns a new instance of RSStimeline.



16
17
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
# File 'lib/rss_timeline.rb', line 16

def initialize(feeds=[], rssfile: 'timeline.rss', xslt: nil,
               filepath: '.', debug: false, target_filepath: nil)

  @source_feeds, @debug, @rssfile, @newupdate = feeds, debug, rssfile, false
  @target_filepath = target_filepath

  puts 'inside initialize' if @debug

  @filepath = File.join(filepath, 'rss_timeline')
  @cache_filepath = File.join(@filepath, 'cache')

  # create a cache directory if it doesn't already exist
  FileX.mkdir_p @cache_filepath

  if FileX.exists? rssfile then
    @timeline = RSScreator.new rssfile
  else

    @timeline = RSScreator.new
    self.title = 'My RSStimeline feed'
    self.description = 'Generated using the RSStimeline gem'

  end

  @timeline.xslt = xslt if xslt
  puts '@timeline.xslt : ' + @timeline.xslt.inspect if @debug

end

Instance Attribute Details

#rssfileObject

Returns the value of attribute rssfile.



14
15
16
# File 'lib/rss_timeline.rb', line 14

def rssfile
  @rssfile
end

Instance Method Details

#descriptionObject



106
107
108
# File 'lib/rss_timeline.rb', line 106

def description()
  @timeline.description
end

#description=(val) ⇒ Object



110
111
112
# File 'lib/rss_timeline.rb', line 110

def description=(val)
  @timeline.description = val
end


114
115
116
# File 'lib/rss_timeline.rb', line 114

def link(val)
  @timeline.link
end

#link=(val) ⇒ Object



118
119
120
# File 'lib/rss_timeline.rb', line 118

def link=(val)
  @timeline = val
end

#titleObject



122
123
124
# File 'lib/rss_timeline.rb', line 122

def title()
  @timeline.title
end

#title=(val) ⇒ Object



126
127
128
# File 'lib/rss_timeline.rb', line 126

def title=(val)
  @timeline.title = val
end

#updateObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rss_timeline.rb', line 45

def update()

  # fetch the feeds from the web
  feeds = @source_feeds.map do |feed|
    #force_encoding('UTF-8')
    [feed, SimpleRSS.parse(URI.open(feed).read.force_encoding('UTF-8'))]
  end

  # check for each feed from the cache.
  # if the feed is in the cache, compare the 2 to find any new items.
  # New items should be added to the main timeline RSS feed

  updated = false

  feeds.each do |feed, rss|

    rssfile = File.join(@cache_filepath, feed[6..-1].gsub(/\W+/,'').\
                                           reverse.slice(0,40).reverse)

    if File.exists? rssfile then

      rss_cache = SimpleRSS.parse FileX.read(rssfile).force_encoding('UTF-8')

      fresh, old = [rss.items, rss_cache.items].map do |feed|
        feed.clone.each {|x| x.delete :guid }
      end


      new_items = fresh - old

      if @debug then
        puts 'fresh: ' + fresh.inspect
        puts 'old: ' + old.inspect
        puts 'new_items: ' + new_items.inspect
      end

      new_rss_items = new_items.map do |x|
        rss.items.find {|y| y[:title] == x[:title]}
      end

      new_rss_items.reverse.each {|item| add_new item}

      if new_rss_items.any? then
        puts 'new_rss_items: ' + new_rss_items.inspect if @debug
        updated = true
        FileX.write rssfile, rss.source
      end

    else

      updated = true
      add_new rss.items.first
      FileX.write rssfile, rss.source

    end
  end

  save() if updated

end