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(*args) ⇒ Client

Initializes a new feedtosis library. It must be initialized with a valid URL as the first argument. A following Hash, if given, may have the following keys:

* 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


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(*args)
  @url      = args.first
  
  @options  = args.extract_options!
  @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