Class: Feedtosis::Client

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

Overview

Feedtosis::Client is the primary interface to the feed reader. Call it with a url that was previously fetched while connected to the configured backend, and it will 1) only do a retrieval if deemed necessary based on the etag and modified-at of the last etag and 2) mark all entries retrieved as either new or not new. Entries retrieved are normalized using the feed-normalizer gem.

Constant Summary collapse

DEFAULTS =
{
  :backend => Hash.new,
  
  # The namespace will be prefixed to the key used for storage of the summary value.  Based on your
  # application needs, it may be useful to provide a custom prefix with initialization options.
  :namespace => 'feedtosis',
  
  # Some feed aggregators that we may be pulling from have entries that are present in one fetch and 
  # then disappear (Google blog search does this).  For these cases, we can't rely on only the digests of 
  # the last fetch to guarantee "newness" of a feed that we may have previously consumed.  We keep a 
  # number of previous sets of digests in order to make sure that we mark correct feeds as "new".
  :retained_digest_size => 10
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = { }) ⇒ Client

Initializes a new feedtosis library. It must be initialized with a valid URL as the first argument. A following optional options Hash may take the arguments:

* backend: a key-value store to be used for summary structures of feeds fetched.  Moneta backends work well, but any object acting like a Hash is valid.
* retained_digest_size: an Integer specifying the number of previous MD5 sets of entries to keep, used for new feed detection

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/feedtosis/client.rb', line 30

def initialize(url, options = { })
  @url      = url
  
  raise ArgumentError, "Feedtosis::Client options must be in Hash form if provided" unless options.is_a?(Hash)
  @options  = options.reverse_merge(DEFAULTS)

  @backend  = @options[:backend]
  
  unless @url.match(URI.regexp('http'))
    raise ArgumentError, "Url #{@url} is not valid!"
  end
  
  unless @backend.respond_to?(:[]) && @backend.respond_to?(:[]=)
    raise ArgumentError, "Backend needs to be a key-value store"
  end
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



10
11
12
# File 'lib/feedtosis/client.rb', line 10

def backend
  @backend
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/feedtosis/client.rb', line 10

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/feedtosis/client.rb', line 10

def url
  @url
end

Instance Method Details

#fetchObject

Retrieves the latest entries from this feed. Returns a Feedtosis::Result object which delegates methods to the Curl::Easy object making the request and the FeedNormalizer::Feed object that may have been created from the HTTP response body.



51
52
53
54
55
56
# File 'lib/feedtosis/client.rb', line 51

def fetch
  curl = build_curl_easy
  curl.perform
  feed = process_curl_response(curl)
  Feedtosis::Result.new(curl, feed)
end