Class: RSScache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rsslist = nil, filepath: '.', debug: true) ⇒ RSScache

Returns a new instance of RSScache.



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

def initialize(rsslist=nil, filepath: '.', debug: true)

  rsslist ||= File.join(filepath, 'rsscache.xml')
  @dx = open_dynarex(rsslist)
  @filepath = filepath
  @cache_filepath = File.join(filepath, 'rsscache')
  FileUtils.mkdir_p @cache_filepath
  
  @err_report = []
  @debug = debug

end

Instance Attribute Details

#dxObject (readonly)

Returns the value of attribute dx.



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

def dx
  @dx
end

#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

#import(raw_s) ⇒ Object

Import a list of URLs into the Dynarex document. URLs which already exist are ignored.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rsscache.rb', line 32

def import(raw_s)
  
  s, _ = RXFHelper.read(raw_s)
  
  s.strip.lines.each do |raw_url|

    url = raw_url.chomp
    puts 'url : '  + url.inspect if @debug
    r = @dx.find_by_url url.chomp

    if r then
      puts 'exists' if @debug
    else
      puts 'new URL found' if @debug
      @dx.create url: url
    end
    
  end    
  
  save()
end

#refreshObject Also known as: update

refresh each RSS feed



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
# File 'lib/rsscache.rb', line 56

def refresh
  
  @err_report = []

  puts '@dx.to_xml'  + @dx.to_xml(pretty: true)  if @debug
  
  @dx.all.each do |feed|
    
    puts 'feed:' + feed.inspect if @debug
    
    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
  
  puts '@dx: ' + @dx.to_xml(pretty: true) if @debug
  save()

end

#saveObject



102
103
104
105
106
107
# File 'lib/rsscache.rb', line 102

def save()

  @dx.save File.join(@filepath, 'rsscache.xml')
  File.write File.join(@filepath, 'rsscache.txt'), @dx.to_s

end