jsl-feedzirra 0.0.12.4 → 0.0.12.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -77,7 +77,7 @@ module Feedzirra
77
77
  retrievable.update_from_feed(updated_feed)
78
78
  else
79
79
  # all elements are "new", since we weren't dealing with a Feed element.
80
- updated_feed.new_entries = updated_feed.entries
80
+ updated_feed.mark_all_entries_new!
81
81
  end
82
82
  end
83
83
 
@@ -88,7 +88,7 @@ module Feedzirra
88
88
  begin
89
89
  if curl.response_code == 304
90
90
  updated_feed = retrievable
91
- updated_feed.new_entries = [ ]
91
+ updated_feed.mark_all_entries_old!
92
92
  else
93
93
  updated_feed = parser_for_xml(curl.body_str).run
94
94
  updated_feed.feed_url = curl.last_effective_url
@@ -19,6 +19,18 @@ module Feedzirra
19
19
  @id || @url
20
20
  end
21
21
 
22
+ def new_entry?
23
+ @_new_entry.nil? ? true : @_new_entry
24
+ end
25
+
26
+ def mark_old!
27
+ @_new_entry = false
28
+ end
29
+
30
+ def mark_new!
31
+ @_new_entry = true
32
+ end
33
+
22
34
  ##
23
35
  # Writter for published. By default, we keep the "oldest" publish time found.
24
36
  def published=(val)
@@ -27,7 +39,7 @@ module Feedzirra
27
39
  end
28
40
 
29
41
  ##
30
- # Writter for udapted. By default, we keep the most recenet update time found.
42
+ # Writter for updated. By default, we keep the most recent update time found.
31
43
  def updated=(val)
32
44
  parsed = parse_datetime(val)
33
45
  @updated = parsed if !@updated || parsed > @updated
@@ -2,9 +2,14 @@ module Feedzirra
2
2
  module FeedUtilities
3
3
  UPDATABLE_ATTRIBUTES = %w(title feed_url url last_modified)
4
4
 
5
- attr_writer :new_entries, :updated, :last_modified
5
+ attr_writer :updated, :last_modified, :entries
6
6
  attr_accessor :etag
7
7
 
8
+ def initialize
9
+ @entries = []
10
+ super
11
+ end
12
+
8
13
  def last_modified
9
14
  @last_modified ||= begin
10
15
  entry = entries.reject {|e| e.published.nil? }.sort_by { |entry| entry.published if entry.published }.last
@@ -12,12 +17,18 @@ module Feedzirra
12
17
  end
13
18
  end
14
19
 
20
+ [ :old, :new ].each do |flag|
21
+ define_method("mark_all_entries_#{flag}!") do # def mark_all_entries_new!
22
+ @entries.each{ |e| e.__send__(:"mark_#{flag}!") } # @entries.each {|e| e.mark_new! }
23
+ end # end
24
+ end
25
+
15
26
  def updated?
16
27
  @updated
17
28
  end
18
29
 
19
30
  def new_entries
20
- @new_entries ||= []
31
+ @entries.select{ |e| e.new_entry? }
21
32
  end
22
33
 
23
34
  def has_new_entries?
@@ -25,8 +36,13 @@ module Feedzirra
25
36
  end
26
37
 
27
38
  def update_from_feed(feed)
28
- self.new_entries += find_new_entries_for(feed)
29
- self.entries.unshift(*self.new_entries)
39
+ # Mark all existing entries as old, then add new entries, which are
40
+ # marked as new by default.
41
+ self.entries.each{|e| e.mark_old! }
42
+
43
+ find_new_entries_for(feed).each do |e|
44
+ self.entries << e
45
+ end
30
46
 
31
47
  updated! if UPDATABLE_ATTRIBUTES.any? { |name| update_attribute(feed, name) }
32
48
  end
@@ -50,18 +66,14 @@ module Feedzirra
50
66
  end
51
67
 
52
68
  def find_new_entries_for(feed)
53
- # this implementation is a hack, which is why it's so ugly.
54
- # it's to get around the fact that not all feeds have a published date.
55
- # however, they're always ordered with the newest one first.
56
- # So we go through the entries just parsed and insert each one as a new entry
57
- # until we get to one that has the same url as the the newest for the feed
58
- latest_entry = self.entries.first
59
- found_new_entries = []
60
- feed.entries.each do |entry|
61
- break if entry.url == latest_entry.url
62
- found_new_entries << entry
69
+ # This implementation is a hack to get around the fact that not all feeds
70
+ # have a published date - we exploit the fact that feeds are always
71
+ # ordered with newest element first to work backwards until we reach feed urls
72
+ # that we've already seen.
73
+ feed.entries.reverse.inject([]) do |res, entry|
74
+ break res if entry.url == self.entries.first.url
75
+ res << entry
63
76
  end
64
- found_new_entries
65
77
  end
66
78
 
67
79
  def existing_entry?(test_entry)
data/lib/feedzirra.rb CHANGED
@@ -41,5 +41,5 @@ require 'feedzirra/parser/atom_feed_burner'
41
41
 
42
42
  module Feedzirra
43
43
  USER_AGENT = "feedzirra http://github.com/pauldix/feedzirra/tree/master"
44
- VERSION = "0.0.12.4"
44
+ VERSION = "0.0.12.5"
45
45
  end
@@ -1,12 +1,44 @@
1
1
  require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
2
 
3
3
  describe Feedzirra::FeedUtilities do
4
- before(:each) do
4
+ before do
5
5
  @klass = Class.new do
6
6
  include Feedzirra::FeedEntryUtilities
7
7
  end
8
8
  end
9
9
 
10
+ describe "new_entry?" do
11
+ it "should default to being a new entry" do
12
+ @klass.new.new_entry?.should be_true
13
+ end
14
+ end
15
+
16
+ describe "#mark_old!" do
17
+ before do
18
+ @entry = @klass.new
19
+ end
20
+
21
+ it "should make new_entry? return false" do
22
+ @entry.new_entry?.should be_true
23
+ @entry.mark_old!
24
+ @entry.new_entry?.should be_false
25
+ end
26
+ end
27
+
28
+ describe "#mark_new!" do
29
+ before do
30
+ @entry = @klass.new
31
+ end
32
+
33
+ it "should make new_entry? return true" do
34
+ @entry.new_entry?.should be_true
35
+ @entry.mark_old!
36
+ @entry.new_entry?.should be_false
37
+ @entry.mark_new!
38
+ @entry.new_entry?.should be_true
39
+ end
40
+ end
41
+
10
42
  describe "handling dates" do
11
43
  it "should parse an ISO 8601 formatted datetime into Time" do
12
44
  time = @klass.new.parse_datetime("2008-02-20T8:05:00-010:00")
@@ -16,7 +48,7 @@ describe Feedzirra::FeedUtilities do
16
48
  end
17
49
 
18
50
  describe "sanitizing" do
19
- before(:each) do
51
+ before do
20
52
  @feed = Feedzirra::FeedParser.new(sample_atom_feed).run
21
53
  @entry = @feed.entries.first
22
54
  end
@@ -1,44 +1,37 @@
1
1
  require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
2
 
3
3
  describe Feedzirra::FeedUtilities do
4
- before(:each) do
4
+ before do
5
5
  @klass = Class.new do
6
6
  include Feedzirra::FeedUtilities
7
7
  end
8
+ @feed = @klass.new
8
9
  end
9
10
 
10
11
  describe "instance methods" do
11
12
  it "should provide an updated? accessor" do
12
- feed = @klass.new
13
- feed.should_not be_updated
14
- feed.updated = true
15
- feed.should be_updated
16
- end
17
-
18
- it "should provide a new_entries accessor" do
19
- feed = @klass.new
20
- feed.new_entries.should == []
21
- feed.new_entries = [:foo]
22
- feed.new_entries.should == [:foo]
13
+ @feed.should_not be_updated
14
+ @feed.updated = true
15
+ @feed.should be_updated
23
16
  end
24
17
 
25
18
  it "should provide an etag accessor" do
26
- feed = @klass.new
27
- feed.etag = "foo"
28
- feed.etag.should == "foo"
19
+ @feed.etag = "foo"
20
+ @feed.etag.should == "foo"
29
21
  end
30
22
 
31
23
  it "should provide a last_modified accessor" do
32
- feed = @klass.new
24
+ @feed = @klass.new
33
25
  time = Time.now
34
- feed.last_modified = time
35
- feed.last_modified.should == time
26
+ @feed.last_modified = time
27
+ @feed.last_modified.should == time
36
28
  end
37
-
29
+
38
30
  it "should return new_entries? as true when entries are put into new_entries" do
39
- feed = @klass.new
40
- feed.new_entries << :foo
41
- feed.should have_new_entries
31
+ @feed = @klass.new
32
+ feed_entry = Feedzirra::Parser::AtomEntry.new
33
+ @feed.instance_variable_set(:@entries, [ feed_entry ])
34
+ @feed.should have_new_entries
42
35
  end
43
36
 
44
37
  it "should return a last_modified value from the entry with the most recent published date if the last_modified date hasn't been set" do
@@ -59,9 +52,33 @@ describe Feedzirra::FeedUtilities do
59
52
  end
60
53
  end
61
54
 
55
+ describe "#mark_all_entries_old!" do
56
+ before do
57
+ @mock_entry = mock('entry')
58
+ @feed.entries = [ @mock_entry ]
59
+ end
60
+
61
+ it "should call #mark_old! on all entries" do
62
+ @mock_entry.expects(:mark_old!)
63
+ @feed.mark_all_entries_old!
64
+ end
65
+ end
66
+
67
+ describe "#mark_all_entries_new!" do
68
+ before do
69
+ @mock_entry = mock('entry')
70
+ @feed.entries = [ @mock_entry ]
71
+ end
72
+
73
+ it "should call #mark_new! on all entries" do
74
+ @mock_entry.expects(:mark_new!)
75
+ @feed.mark_all_entries_new!
76
+ end
77
+ end
78
+
62
79
  describe "#update_from_feed" do
63
80
  describe "updating feed attributes" do
64
- before(:each) do
81
+ before do
65
82
  # I'm using the Atom class when I know I should be using a different one. However, this update_from_feed
66
83
  # method would only be called against a feed item.
67
84
  @feed = Feedzirra::Parser::Atom.new
@@ -109,7 +126,7 @@ describe Feedzirra::FeedUtilities do
109
126
  end
110
127
 
111
128
  describe "updating entries" do
112
- before(:each) do
129
+ before do
113
130
  # I'm using the Atom class when I know I should be using a different one. However, this update_from_feed
114
131
  # method would only be called against a feed item.
115
132
  @feed = Feedzirra::Parser::Atom.new
@@ -117,16 +134,24 @@ describe Feedzirra::FeedUtilities do
117
134
  @feed.url = "http://pauldix.net"
118
135
  @feed.feed_url = "http://feeds.feedburner.com/PaulDixExplainsNothing"
119
136
  @feed.updated = false
120
- @updated_feed = @feed.dup
137
+
138
+ # Using a clone or dup would only copy references to instance variables in the
139
+ # object, so we serialize and deserialize it here to avoid problems caused by
140
+ # variable references being passed around.
141
+ @updated_feed = Marshal.load(Marshal.dump(@feed))
142
+
121
143
  @old_entry = Feedzirra::Parser::AtomEntry.new
122
144
  @old_entry.url = "http://pauldix.net/old.html"
123
145
  @old_entry.published = Time.now.to_s
146
+
124
147
  @new_entry = Feedzirra::Parser::AtomEntry.new
125
148
  @new_entry.url = "http://pauldix.net/new.html"
126
149
  @new_entry.published = (Time.now + 10).to_s
150
+
127
151
  @feed.entries << @old_entry
128
- @updated_feed.entries << @new_entry
152
+
129
153
  @updated_feed.entries << @old_entry
154
+ @updated_feed.entries << @new_entry
130
155
  end
131
156
 
132
157
  it "should update last-modified from the latest entry date" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsl-feedzirra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12.4
4
+ version: 0.0.12.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix