Class: RSScache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RSScache.



16
17
18
19
20
21
22
23
24
# File 'lib/rsscache.rb', line 16

def initialize(rsslist, feedsfilepath=Dir.pwd)

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

end

Instance Attribute Details

#err_reportObject (readonly)

Returns the value of attribute err_report.



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

def err_report
  @err_report
end

Instance Method Details

#open_dynarex(x) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/rsscache.rb', line 67

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



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
57
58
59
60
61
62
63
# File 'lib/rsscache.rb', line 26

def refresh
  
  @err_report = []

  @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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rsscache.rb', line 77

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

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rsscache.rb', line 97

def updates?(feed)

  # fetch the feeds from the web
  buffer, code = fetch(feed.url)

  if code == 200 then
    rss = SimpleRSS.parse(buffer)
  else
    @err_report << [feed.url, code]
    return false
  end

  if feed.filename.empty? then
    
    filename = feed.url[6..-1].gsub(/\W+/,'').\
                        reverse.slice(0,40).reverse.downcase + '.xml'
    feed.filename = filename

  end
  
  rssfile = File.join(@feedsfilepath, feed.filename)

  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