Class: RSScache

Inherits:
Object
  • Object
show all
Defined in:
lib/rsscache.rb

Instance Method Summary collapse

Constructor Details

#initialize(rsslist, feedsfilepath = Dir.pwd) ⇒ RSScache



13
14
15
16
17
18
19
# File 'lib/rsscache.rb', line 13

def initialize(rsslist, feedsfilepath=Dir.pwd)

  @dx = open_dynarex(rsslist)
  @rsslist, @feedsfilepath = rsslist, feedsfilepath
  FileUtils.mkdir_p feedsfilepath

end

Instance Method Details

#open_dynarex(x) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/rsscache.rb', line 60

def open_dynarex(x)

  if x.lines.length == 1 and File.exists?(x) and \
                                    File.extname(x) == '.txt'then
    Dynarex.new.import x
  else
    Dynarex.new x
  end
end

#refreshObject Also known as: update



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
48
49
50
51
52
53
54
55
56
# File 'lib/rsscache.rb', line 21

def refresh

  @dx.all.each do |feed|

    if feed.next_refresh.empty? or \
                           Time.now >= Time.parse(feed.next_refresh) then

      any_new_items = updates? feed

      feed.refresh_rate = if feed.refresh_rate.empty? then

        10 

      else

        if Time.now > Time.parse(feed.next_refresh) + \
                        feed.refresh_rate.to_i and not any_new_items then
          feed.refresh_rate.to_i + 10
        end

      end


      feed.next_refresh = Time.now + feed.refresh_rate.to_i * 60


    else

      feed.refresh_rate = feed.refresh_rate.to_i - 10 if feed.refresh_rate.to_i > 10

    end
  end

  save_dynarex()

end

#save_dynarexObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rsscache.rb', line 70

def save_dynarex()

  if @rsslist.lines.length == 1 and File.exists?(@rsslist)

    if File.extname(@rsslist) == '.txt'then

      File.write @rsslist, @dx.to_s

    else

      @dx.save

    end

  end
end

#updates?(feed) ⇒ Boolean

checks for any updates and saves the latest RSS file to

the cache if there is


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rsscache.rb', line 90

def updates?(feed)

  # fetch the feeds from the web
  rss = SimpleRSS.parse(open(feed.url))

  rssfile = File.join(@feedsfilepath, feed.url[6..-1].gsub(/\W+/,'').\
                             reverse.slice(0,40).reverse).downcase + '.xml'
  
  if File.exists? rssfile then

    rss_cache = SimpleRSS.parse File.read(rssfile)
    new_rss_items = rss.items - rss_cache.items
    (File.write rssfile, rss.source; return true) if new_rss_items.any?
    
  else

    File.write rssfile, rss.source
    feed.title = rss.title if feed.title.empty?
    return true
    
  end
  
  return false
end