Class: CachedChannel

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

Constant Summary collapse

CACHESIZE =

Size of the cache for each feed 100 items should be enough for everybody, even quite busy feeds

100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCachedChannel

Returns a new instance of CachedChannel.



122
123
124
125
126
127
128
129
# File 'lib/feed2imap/cache.rb', line 122

def initialize
  @lastcheck = Time::at(0)
  @items = []
  @itemstemp = [] # see below
  @nbnewitems = 0
  @failures = 0
  @parsefailures = 0
end

Instance Attribute Details

#failuresObject

Returns the value of attribute failures.



120
121
122
# File 'lib/feed2imap/cache.rb', line 120

def failures
  @failures
end

#itemsObject

Returns the value of attribute items.



120
121
122
# File 'lib/feed2imap/cache.rb', line 120

def items
  @items
end

#lastcheckObject

Returns the value of attribute lastcheck.



120
121
122
# File 'lib/feed2imap/cache.rb', line 120

def lastcheck
  @lastcheck
end

#parsefailuresObject

Returns the value of attribute parsefailures.



120
121
122
# File 'lib/feed2imap/cache.rb', line 120

def parsefailures
  @parsefailures
end

Instance Method Details

#commitObject



224
225
226
227
228
229
230
231
232
233
# File 'lib/feed2imap/cache.rb', line 224

def commit
  # too old items must be dropped
  n = @nbnewitems > CACHESIZE ? @nbnewitems : CACHESIZE
  @items = @itemstemp[0..n]
  if $updateddebug
    puts "Committing: new items: #{@nbnewitems} / items kept: #{@items.length}"
  end
  @itemstemp = []
  self
end

#fetch_failedObject



246
247
248
249
250
# File 'lib/feed2imap/cache.rb', line 246

def fetch_failed
  @failures = 0 if @failures.nil?
  @failures += 1
  return @failures
end

#get_new_items(items, always_new = false, ignore_hash = false) ⇒ Object

Returns the really new items amongst items



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/feed2imap/cache.rb', line 142

def get_new_items(items, always_new = false, ignore_hash = false)
  # save number of new items
  @nbnewitems = items.length
  # set items' cached version if not set yet
  newitems = []
  updateditems = []
  @itemstemp = @items
  items.each { |i| i.cacheditem ||= CachedItem::new(i) }
  if $updateddebug
    puts "-------Items downloaded before dups removal (#{items.length}) :----------"
    items.each { |i| puts "#{i.cacheditem.to_s}" }
  end
  # remove dups
  dups = true
  while dups
    dups = false
    for i in 0...items.length do
      for j in i+1...items.length do
        if items[i].cacheditem == items[j].cacheditem
          if $updateddebug
            puts "## Removed duplicate #{items[j].cacheditem.to_s}"
          end
          items.delete_at(j)
          dups = true
          break
        end
      end
      break if dups
    end
  end
  # debug : dump interesting info to stdout.
  if $updateddebug
    puts "-------Items downloaded after dups removal (#{items.length}) :----------"
    items.each { |i| puts "#{i.cacheditem.to_s}" }
    puts "-------Items already there (#{@items.length}) :----------"
    @items.each { |i| puts "#{i.to_s}" }
    puts "Items always considered as new: #{always_new.to_s}"
    puts "Items compared ignoring the hash: #{ignore_hash.to_s}"
  end
  items.each do |i|
    found = false
    # Try to find a perfect match
    @items.each do |j|
      # note that simple_compare only CachedItem, not RSSItem, so we have to use
      # j.simple_compare(i) and not i.simple_compare(j)
      if (i.cacheditem == j and not ignore_hash) or
          (j.simple_compare(i) and ignore_hash)
        i.cacheditem.index = j.index
        found = true
        # let's put j in front of itemstemp
        @itemstemp.delete(j)
        @itemstemp.unshift(j)
        break
      end
      # If we didn't find exact match, try to check if we have an update
      if j.is_ancestor_of(i)
        i.cacheditem.index = j.index
        i.cacheditem.updated = true
        updateditems.push(i)
        found = true
        # let's put j in front of itemstemp
        @itemstemp.delete(j)
        @itemstemp.unshift(i.cacheditem)
        break
      end
    end
    next if found
    # add as new
    i.cacheditem.create_index
    newitems.push(i)
    # add i.cacheditem to @itemstemp
    @itemstemp.unshift(i.cacheditem)
  end
  if $updateddebug
    puts "-------New items :----------"
    newitems.each { |i| puts "#{i.cacheditem.to_s}" }
    puts "-------Updated items :----------"
    updateditems.each { |i| puts "#{i.cacheditem.to_s}" }
  end
  return [newitems, updateditems]
end

#nbitemsObject

returns the number of items



236
237
238
# File 'lib/feed2imap/cache.rb', line 236

def nbitems
  @items.length
end

#parse_failedObject



240
241
242
243
244
# File 'lib/feed2imap/cache.rb', line 240

def parse_failed
  @parsefailures = 0 if @parsefailures.nil?
  @parsefailures += 1
  return @parsefailures
end