Module: RssReader::PageExtensions

Defined in:
lib/rss_reader/page_extensions.rb

Instance Method Summary collapse

Instance Method Details

#cache?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rss_reader/page_extensions.rb', line 40

def cache?
  false
end

#feed_for(str) ⇒ Object



36
37
38
# File 'lib/rss_reader/page_extensions.rb', line 36

def feed_for(str)
  FeedParser::Feed.new(str)
end

#fetch_rss(uri, cache_time) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rss_reader/page_extensions.rb', line 8

def fetch_rss(uri, cache_time)
  c = File.join(ActionController::Base.page_cache_directory, uri.tr(':/','_'))
  if (cached_feed = feed_for(IO.read(c)) rescue nil)
    return cached_feed if File.mtime(c) > (Time.now - cache_time)
    since = File.mtime(c).httpdate
  else
    since = "1970-01-01 00:00:00"
  end
  u = URI::parse(uri)
  begin
    http = Net::HTTP.new(u.host, u.port)
    http.use_ssl = true if u.port == 443
    answer = http.get("#{u.request_uri}", {"If-Modified-Since" => since, 'User-Agent' => "RadiantCMS rss_reader Extension #{RadiantRssReaderExtension::VERSION}"} )
    feed = feed_for(answer.body)
  rescue
    return cached_feed
  end
  case answer.code
  when '304'
    return cached_feed
  when '200'
    File.open(c,'w+') { |fp| fp << answer.body }
    return feed
  else
    raise StandardError, "#{answer.code} #{answer.message}"
  end
end