jonleighton-bcms_feeds 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/app/models/feed.rb +4 -2
 - data/test/unit/feed_test.rb +19 -16
 - metadata +2 -2
 
    
        data/app/models/feed.rb
    CHANGED
    
    | 
         @@ -18,15 +18,17 @@ class Feed < ActiveRecord::Base 
     | 
|
| 
       18 
18 
     | 
    
         
             
                  begin
         
     | 
| 
       19 
19 
     | 
    
         
             
                    self.expires_at = Time.now.utc + TTL
         
     | 
| 
       20 
20 
     | 
    
         
             
                    write_attribute(:contents, remote_contents)
         
     | 
| 
      
 21 
     | 
    
         
            +
                    save
         
     | 
| 
       21 
22 
     | 
    
         
             
                  rescue StandardError, Timeout::Error => exception
         
     | 
| 
       22 
23 
     | 
    
         
             
                    logger.error("Loading feed #{url} failed with #{exception.inspect}")
         
     | 
| 
       23 
24 
     | 
    
         
             
                    self.expires_at = Time.now.utc + TTL_ON_ERROR
         
     | 
| 
       24 
     | 
    
         
            -
                     
     | 
| 
      
 25 
     | 
    
         
            +
                    save
         
     | 
| 
       25 
26 
     | 
    
         
             
                  end
         
     | 
| 
       26 
27 
     | 
    
         
             
                else
         
     | 
| 
       27 
28 
     | 
    
         
             
                  logger.info("Loading feed from cache: #{url}")
         
     | 
| 
       28 
     | 
    
         
            -
                  read_attribute(:contents)
         
     | 
| 
       29 
29 
     | 
    
         
             
                end
         
     | 
| 
      
 30 
     | 
    
         
            +
                
         
     | 
| 
      
 31 
     | 
    
         
            +
                read_attribute(:contents)
         
     | 
| 
       30 
32 
     | 
    
         
             
              end
         
     | 
| 
       31 
33 
     | 
    
         | 
| 
       32 
34 
     | 
    
         
             
              def remote_contents
         
     | 
    
        data/test/unit/feed_test.rb
    CHANGED
    
    | 
         @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + "/../test_helper" 
     | 
|
| 
       2 
2 
     | 
    
         | 
| 
       3 
3 
     | 
    
         
             
            class FeedTest < ActiveSupport::TestCase
         
     | 
| 
       4 
4 
     | 
    
         
             
              def setup
         
     | 
| 
       5 
     | 
    
         
            -
                @feed = Feed. 
     | 
| 
      
 5 
     | 
    
         
            +
                @feed = Feed.create!(:url => "http://example.com/blog.rss")
         
     | 
| 
       6 
6 
     | 
    
         
             
                @contents = "<feed>Some feed</feed>"
         
     | 
| 
       7 
7 
     | 
    
         | 
| 
       8 
8 
     | 
    
         
             
                now = Time.now
         
     | 
| 
         @@ -12,13 +12,13 @@ class FeedTest < ActiveSupport::TestCase 
     | 
|
| 
       12 
12 
     | 
    
         
             
                Feed.send(:remove_const, :TIMEOUT)
         
     | 
| 
       13 
13 
     | 
    
         
             
                Feed.const_set(:TIMEOUT, 1)
         
     | 
| 
       14 
14 
     | 
    
         
             
              end
         
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
       16 
     | 
    
         
            -
               
     | 
| 
      
 15 
     | 
    
         
            +
              
         
     | 
| 
      
 16 
     | 
    
         
            +
              test "remote_contents should fetch the feed" do
         
     | 
| 
       17 
17 
     | 
    
         
             
                @feed.expects(:open).with("http://example.com/blog.rss").returns(stub(:read => @contents))
         
     | 
| 
       18 
18 
     | 
    
         
             
                assert_equal @contents, @feed.remote_contents
         
     | 
| 
       19 
19 
     | 
    
         
             
              end
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
               
     | 
| 
      
 21 
     | 
    
         
            +
              test "remote_contents should raise a Timeout::Error if fetching the feed takes longer then Feed::TIMEOUT" do
         
     | 
| 
       22 
22 
     | 
    
         
             
                def @feed.open(url)
         
     | 
| 
       23 
23 
     | 
    
         
             
                  sleep(2)
         
     | 
| 
       24 
24 
     | 
    
         
             
                  stubs(:read => "bla")
         
     | 
| 
         @@ -27,32 +27,35 @@ class FeedTest < ActiveSupport::TestCase 
     | 
|
| 
       27 
27 
     | 
    
         
             
                assert_raise(Timeout::Error) { @feed.remote_contents }
         
     | 
| 
       28 
28 
     | 
    
         
             
              end
         
     | 
| 
       29 
29 
     | 
    
         | 
| 
       30 
     | 
    
         
            -
               
     | 
| 
      
 30 
     | 
    
         
            +
              test "contents with no expiry should return the remote contents and save it" do
         
     | 
| 
       31 
31 
     | 
    
         
             
                @feed.expires_at = nil
         
     | 
| 
       32 
     | 
    
         
            -
                 
     | 
| 
       33 
     | 
    
         
            -
                
         
     | 
| 
       34 
     | 
    
         
            -
                assert_equal @contents, @feed.contents
         
     | 
| 
       35 
     | 
    
         
            -
                assert_equal @contents, @feed.read_attribute(:contents)
         
     | 
| 
       36 
     | 
    
         
            -
                assert_equal Time.now.utc + Feed::TTL, @feed.expires_at
         
     | 
| 
      
 32 
     | 
    
         
            +
                should_get_remote_contents
         
     | 
| 
       37 
33 
     | 
    
         
             
              end
         
     | 
| 
       38 
34 
     | 
    
         | 
| 
       39 
     | 
    
         
            -
               
     | 
| 
      
 35 
     | 
    
         
            +
              test "contents with an expiry in the past return the remote contents and save it" do
         
     | 
| 
       40 
36 
     | 
    
         
             
                @feed.expires_at = Time.now - 1.hour
         
     | 
| 
      
 37 
     | 
    
         
            +
                should_get_remote_contents
         
     | 
| 
      
 38 
     | 
    
         
            +
              end
         
     | 
| 
      
 39 
     | 
    
         
            +
              
         
     | 
| 
      
 40 
     | 
    
         
            +
              def should_get_remote_contents
         
     | 
| 
       41 
41 
     | 
    
         
             
                @feed.stubs(:remote_contents).returns(@contents)
         
     | 
| 
       42 
     | 
    
         
            -
                
         
     | 
| 
       43 
42 
     | 
    
         
             
                assert_equal @contents, @feed.contents
         
     | 
| 
      
 43 
     | 
    
         
            +
                @feed.reload
         
     | 
| 
       44 
44 
     | 
    
         
             
                assert_equal @contents, @feed.read_attribute(:contents)
         
     | 
| 
       45 
     | 
    
         
            -
                 
     | 
| 
      
 45 
     | 
    
         
            +
                
         
     | 
| 
      
 46 
     | 
    
         
            +
                # I think the to_i is necessary because of a lack of precision provided by sqlite3. I think.
         
     | 
| 
      
 47 
     | 
    
         
            +
                # Anyway, without it the comparison fails.
         
     | 
| 
      
 48 
     | 
    
         
            +
                assert_equal (Time.now.utc + Feed::TTL).to_i, @feed.expires_at.to_i
         
     | 
| 
       46 
49 
     | 
    
         
             
              end
         
     | 
| 
       47 
50 
     | 
    
         | 
| 
       48 
     | 
    
         
            -
               
     | 
| 
      
 51 
     | 
    
         
            +
              test "contents with the expiry in the future should return the cached contents" do
         
     | 
| 
       49 
52 
     | 
    
         
             
                @feed.expires_at = Time.now + 1.hour
         
     | 
| 
       50 
53 
     | 
    
         
             
                @feed.write_attribute(:contents, @contents)
         
     | 
| 
       51 
54 
     | 
    
         | 
| 
       52 
55 
     | 
    
         
             
                assert_equal @contents, @feed.contents
         
     | 
| 
       53 
56 
     | 
    
         
             
              end
         
     | 
| 
       54 
57 
     | 
    
         | 
| 
       55 
     | 
    
         
            -
               
     | 
| 
      
 58 
     | 
    
         
            +
              test "TTL of cached contents should be extended if there is an error fetching the remote contents" do
         
     | 
| 
       56 
59 
     | 
    
         
             
                [StandardError, Timeout::Error].each do |exception|
         
     | 
| 
       57 
60 
     | 
    
         
             
                  @feed.expires_at = Time.now - 1.hour
         
     | 
| 
       58 
61 
     | 
    
         
             
                  @feed.stubs(:remote_contents).raises(exception)
         
     | 
| 
         @@ -63,7 +66,7 @@ class FeedTest < ActiveSupport::TestCase 
     | 
|
| 
       63 
66 
     | 
    
         
             
                end
         
     | 
| 
       64 
67 
     | 
    
         
             
              end
         
     | 
| 
       65 
68 
     | 
    
         | 
| 
       66 
     | 
    
         
            -
               
     | 
| 
      
 69 
     | 
    
         
            +
              test "parsed_contents should return the contents parsed by SimpleRSS" do
         
     | 
| 
       67 
70 
     | 
    
         
             
                @feed.stubs(:contents).returns(@contents)
         
     | 
| 
       68 
71 
     | 
    
         
             
                SimpleRSS.stubs(:parse).with(@contents).returns(parsed_contents = stub)
         
     | 
| 
       69 
72 
     | 
    
         | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification 
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: jonleighton-bcms_feeds
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version 
         
     | 
| 
       4 
     | 
    
         
            -
              version: 1.0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.0.4
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors: 
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Jon Leighton
         
     | 
| 
         @@ -9,7 +9,7 @@ autorequire: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
11 
     | 
    
         | 
| 
       12 
     | 
    
         
            -
            date: 2009-08- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2009-08-15 00:00:00 -07:00
         
     | 
| 
       13 
13 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       14 
14 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       15 
15 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     |