Class: Feed

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/feed.rb

Constant Summary collapse

TTL =
30.minutes
TTL_ON_ERROR =
10.minutes
TIMEOUT =

In seconds

10

Instance Method Summary collapse

Instance Method Details

#contentsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/feed.rb', line 16

def contents
  if expires_at.nil? || expires_at < Time.now.utc
    begin
      self.expires_at = Time.now.utc + TTL
      new_contents = remote_contents
      SimpleRSS.parse(new_contents) # Check that we can actually parse it
      write_attribute(:contents, new_contents)
      save
    rescue StandardError, Timeout::Error, SimpleRSSError => exception
      logger.error("Loading feed #{url} failed with #{exception.inspect}")
      self.expires_at = Time.now.utc + TTL_ON_ERROR
      save
    end
  else
    logger.info("Loading feed from cache: #{url}")
  end
  read_attribute(:contents)
end

#parsed_contentsObject



12
13
14
# File 'app/models/feed.rb', line 12

def parsed_contents
  @parsed_contents ||= SimpleRSS.parse(contents)
end

#remote_contentsObject



35
36
37
38
39
# File 'app/models/feed.rb', line 35

def remote_contents
  Timeout.timeout(TIMEOUT) {
    simple_get(url)
  }
end