lifestream 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
@@ -9,11 +9,11 @@ module Lifestream
9
9
  def parse
10
10
  @feed = RSS::Parser.parse(@raw_data, false)
11
11
  rescue => e
12
- raise Lifestream::Channel::Rss2::MalformedFeed.new("The feed from #{@url} could not be parsed") if Lifestream.options[:whiny]
12
+ raise Lifestream::Channel::Rss2::MalformedFeed.new("The feed from #{@request.url} could not be parsed #{e}") if Lifestream.options[:whiny]
13
13
  end
14
14
 
15
15
  def feed_to_a
16
- @feed.items || []
16
+ @feed.items
17
17
  end
18
18
 
19
19
  def to_branch(branch)
@@ -15,11 +15,27 @@ module Lifestream
15
15
  protected
16
16
 
17
17
  def download
18
- response = @request.get
19
- raise unless response.kind_of?(Net::HTTPSuccess)
20
- @raw_data = response.body
18
+ response_with_cache do
19
+ response = @request.get
20
+ raise unless response.kind_of?(Net::HTTPSuccess)
21
+ @raw_data = response.body
22
+ end
21
23
  rescue => e
22
- Lifestream::Channel::DownloadError.new("The URL #{@request.url} failed to download") if Lifestream.options[:whiny]
24
+ raise Lifestream::Channel::DownloadError.new("The URL #{@request.url} failed to download. #{e}") if Lifestream.options[:whiny]
25
+ end
26
+
27
+ def response_with_cache
28
+ yield and return unless Lifestream.options[:cache]
29
+ cache_path = File.join(Lifestream.options[:cache], "#{name}.xml")
30
+ if !File.exist?(cache_path) || File.mtime(cache_path) + Lifesteam.options[:cache_expiration] < Time.now
31
+ yield
32
+ cache = File.new(cache_path, 'wb')
33
+ cache.flock(File::LOCK_EX)
34
+ cache.write(@raw_data)
35
+ cache.flock(File::LOCK_UN)
36
+ else
37
+ @raw_data = File.read(cache_path)
38
+ end
23
39
  end
24
40
 
25
41
  def create_branches
@@ -45,7 +61,7 @@ module Lifestream
45
61
  private
46
62
 
47
63
  def raise_method_error method
48
- raise Lifestream::Channel::MethodError, "Method `#{method}` must be overriden in the child class"
64
+ raise Lifestream::Channel::MethodError, "Method `#{method}' must be overriden in the child class"
49
65
  end
50
66
 
51
67
  class MethodError < StandardError; end
data/lib/lifestream.rb CHANGED
@@ -8,10 +8,16 @@ module Lifestream
8
8
  # Defaults to true
9
9
  # * config: Path to the lifestream.yml file that contains the feeds to download.
10
10
  # Defaults to gem location, you will probably want to set this
11
+ # * cache: The path that the downloaded feeds should be cached in, defaults to tmp
12
+ # Set cache to false to disable it
13
+ # * cache_expiration: The amount of time that the cache is fresh for
14
+ # Defaults to 1 hour
11
15
  def self.options
12
16
  @options ||= {
13
17
  :config => 'lifestream.yml',
14
- :whiny => true
18
+ :whiny => true,
19
+ :cache => 'tmp',
20
+ :cache_expiration => (60 * 60 * 1)
15
21
  }
16
22
  end
17
23
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lifestream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Hunter