Class: FeedClient

Inherits:
Object
  • Object
show all
Defined in:
app/clients/feed_client.rb

Overview

This class can fetch feeds and parse them. It also takes care of caching, sending HTTP headers that indicate the server to send only new entries.

Class Method Summary collapse

Class Method Details

.fetch(feed, http_caching: true, perform_autodiscovery: false) ⇒ Object

Fetch a feed, parse it and save the entries in the database. This is a class method.

Receives as arguments

  • feed: feed to be fetched. It must already be saved in the database and its fetch_url field must have a value.

  • http_caching (optional). Boolean indicating if client-side HTTP caching (with rack-cache) will be used.

Defaults to true.

  • perform_autodiscovery (optional). Boolean indicating if feed autodiscovery should be performed on an HTML response.

Defaults to false.

If HTTP caching is used the response will be retrieved from the local cache if the headers show it is still valid.

If the response to the GET is not a feed but an HTML document and the “perform_autodiscovery” argument is true, it tries to autodiscover a feed from the HTML. If a feed is autodiscovered, it is immediately fetched but passing a false to the “perform_autodiscovery” argument, to avoid entering an infinite loop of HTTP GETs.

Returns feed instance if fetch is successful, raises an error otherwise.


35
36
37
38
39
40
41
42
43
# File 'app/clients/feed_client.rb', line 35

def self.fetch(feed, http_caching: true, perform_autodiscovery: false)
  http_response = fetch_valid_feed feed, http_caching, perform_autodiscovery

  unless http_response.nil?
    feed = handle_html_response feed, http_response, perform_autodiscovery
  end

  return feed
end