hentry_consumer 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/hentry_consumer/h_entry.rb +4 -0
- data/lib/hentry_consumer/h_feed.rb +18 -2
- data/lib/hentry_consumer/version.rb +1 -1
- data/spec/lib/hentry_consumer/h_entry_spec.rb +11 -2
- data/spec/lib/hentry_consumer/h_feed_spec.rb +18 -5
- data/spec/support/example.html +2 -2
- metadata +1 -1
data/README.md
CHANGED
@@ -5,6 +5,10 @@ module HentryConsumer
|
|
5
5
|
attr_accessor :name, :categories, :author, :content, :bookmark, :published_at, :summary
|
6
6
|
alias_method :authors, :author
|
7
7
|
|
8
|
+
def older_than(time)
|
9
|
+
Time.parse(published_at.first) < time if time
|
10
|
+
end
|
11
|
+
|
8
12
|
def parse_p_name(element)
|
9
13
|
assign_value :name, element.text
|
10
14
|
end
|
@@ -1,8 +1,11 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
1
3
|
module HentryConsumer
|
2
4
|
class HFeed
|
3
|
-
attr_accessor :html, :entries
|
5
|
+
attr_accessor :html, :entries, :options
|
4
6
|
|
5
|
-
def initialize(html,
|
7
|
+
def initialize(html, options={})
|
8
|
+
@options = options
|
6
9
|
@html = Nokogiri::HTML(open(html, headers).read)
|
7
10
|
@entries = []
|
8
11
|
parse_html
|
@@ -11,6 +14,7 @@ module HentryConsumer
|
|
11
14
|
def parse_html
|
12
15
|
self.html.css(".h-entry").each do |hentry|
|
13
16
|
entry = HEntry.new(hentry.children)
|
17
|
+
next if entry.older_than(last_modified_at)
|
14
18
|
self.entries << entry
|
15
19
|
end
|
16
20
|
end
|
@@ -35,5 +39,17 @@ module HentryConsumer
|
|
35
39
|
to_hash.to_json(a)
|
36
40
|
end
|
37
41
|
|
42
|
+
def last_modified_at
|
43
|
+
time = @options[:last_modified_at] || return
|
44
|
+
time = Time.parse(time) if time.is_a? String
|
45
|
+
time.to_time
|
46
|
+
end
|
47
|
+
|
48
|
+
def headers
|
49
|
+
h = {}
|
50
|
+
h["If-Modified-Since"] = CGI.rfc1123_date(last_modified_at) if last_modified_at
|
51
|
+
h
|
52
|
+
end
|
53
|
+
|
38
54
|
end
|
39
55
|
end
|
@@ -15,8 +15,17 @@ describe HentryConsumer::HEntry do
|
|
15
15
|
entry.summary.should eq ["Signed up with 3 locations"]
|
16
16
|
end
|
17
17
|
it "has a time" do
|
18
|
-
entry.published_at.should eq ["2012-08-
|
18
|
+
entry.published_at.should eq ["2012-08-24 20:09-0700"]
|
19
19
|
end
|
20
|
+
|
21
|
+
it "is older than now" do
|
22
|
+
entry.older_than(Time.now).should be_true
|
23
|
+
end
|
24
|
+
|
25
|
+
it "is not older than 2010" do
|
26
|
+
entry.older_than(Time.parse("Jan 1 2010")).should be_false
|
27
|
+
end
|
28
|
+
|
20
29
|
it "has a bookmark" do
|
21
30
|
entry.bookmark.should eq "http://g5.com/feed/entries/2012-08-26-20-09-0700"
|
22
31
|
end
|
@@ -56,7 +65,7 @@ describe HentryConsumer::HEntry do
|
|
56
65
|
it { json["properties"]["author"].should be_an_instance_of Array }
|
57
66
|
it { json["properties"]["author"].first["type"].should include "h-card" }
|
58
67
|
it { json["properties"]["bookmark"].should eq "http://g5.com/feed/entries/2012-08-26-20-09-0700" }
|
59
|
-
it { json["properties"]["published_at"].should eq ["2012-08-
|
68
|
+
it { json["properties"]["published_at"].should eq ["2012-08-24 20:09-0700"] }
|
60
69
|
it { json["properties"]["summary"].should be_an_instance_of Array }
|
61
70
|
end
|
62
71
|
end
|
@@ -2,18 +2,31 @@ require 'hentry_consumer'
|
|
2
2
|
|
3
3
|
describe HentryConsumer::HFeed do
|
4
4
|
let(:feed) { HentryConsumer.parse(File.open("spec/support/example.html")) }
|
5
|
-
|
5
|
+
|
6
6
|
describe "caching" do
|
7
|
+
let(:time_feed) { HentryConsumer::HFeed.new("spec/support/example.html", last_modified_at: "8:09pm on August 25th, 2012") }
|
7
8
|
it "returns false on 304" do
|
8
9
|
HentryConsumer::HFeed.any_instance.stub(:open).
|
9
|
-
with("spec/support/example.html",
|
10
|
+
with("spec/support/example.html", "If-Modified-Since" => "Sun, 26 Aug 2012 03:09:00 GMT").
|
10
11
|
and_raise( OpenURI::HTTPError.new("304 Not Modified", nil) )
|
11
12
|
|
12
|
-
lambda { HentryConsumer::HFeed.new("spec/support/example.html",
|
13
|
+
lambda { HentryConsumer::HFeed.new("spec/support/example.html", last_modified_at: Time.parse("8:09pm on August 25th, 2012")) }.
|
13
14
|
should raise_error OpenURI::HTTPError, "304 Not Modified"
|
14
15
|
end
|
16
|
+
|
17
|
+
it "doesn't add old entries" do
|
18
|
+
time_feed.entries.should have(1).thing
|
19
|
+
end
|
20
|
+
|
21
|
+
it "header has last modified since" do
|
22
|
+
time_feed.headers.should eq({"If-Modified-Since" => "Sun, 26 Aug 2012 03:09:00 GMT"})
|
23
|
+
end
|
24
|
+
|
25
|
+
it "header has last modified since" do
|
26
|
+
feed.headers.should eq({})
|
27
|
+
end
|
15
28
|
end
|
16
|
-
|
29
|
+
|
17
30
|
|
18
31
|
describe "#to_json" do
|
19
32
|
let(:json) { JSON.parse(feed.to_json) }
|
@@ -22,7 +35,7 @@ describe HentryConsumer::HFeed do
|
|
22
35
|
it { json["items"].first["properties"]["entries"].should be_an_instance_of Array }
|
23
36
|
it { json["items"].first["properties"]["entries"].first["type"].should include "h-entry" }
|
24
37
|
end
|
25
|
-
|
38
|
+
|
26
39
|
describe "#to_s" do
|
27
40
|
let(:string) { feed.to_s }
|
28
41
|
it "should have nicely formatted content" do
|
data/spec/support/example.html
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
<h1 class="p-name">Senior Cat Living</h1>
|
3
3
|
<p class="woot p-summary woot">Signed up with 3 locations</p>
|
4
4
|
<p class="p-five">Signed up with 3 locations</p>
|
5
|
-
<time datetime="2012-08-
|
6
|
-
8:09pm on August
|
5
|
+
<time datetime="2012-08-24 20:09-0700" class="dt-published">
|
6
|
+
8:09pm on August 24th, 2012
|
7
7
|
</time>
|
8
8
|
<a class="p-category" href="http://g5.com/tag/new-customer" rel="tag">
|
9
9
|
New Customer
|