hnruby 0.01.1 → 0.01.2
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.
- checksums.yaml +4 -4
- data/lib/hnruby/comment.rb +22 -17
- data/lib/hnruby/storylist.rb +3 -3
- data/test/test_hackernews.rb +4 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b6d68737114cfb703d4d7007b6719583bc99331
|
4
|
+
data.tar.gz: 4f93b627eb99063c28fc9a0e870e87d399663e7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2404fb955d07e5aa329766ba34d4561e5e04d20aa49930d759a787152e05dfb62401157bd7b15ea3438076fc04c78bf2999f6cf38453f4826b1c0be61ce40d5
|
7
|
+
data.tar.gz: 3ffe6c85c9e4df51dabf563a270a8c863f26b8238d0ec79179ba5b9e9bda930a032c4843dc912fd265c6da67d4226a4d1d5bf7c09e88f3ebbafab6d1910c81ad
|
data/lib/hnruby/comment.rb
CHANGED
@@ -57,44 +57,49 @@ class Comment
|
|
57
57
|
|
58
58
|
# Returns true if +comment+ is a child of +self+.
|
59
59
|
def parent_of?(comment)
|
60
|
-
|
60
|
+
if comment.respond_to?(:parent)
|
61
|
+
comment.parent && comment.parent.id == @id
|
62
|
+
end
|
61
63
|
end
|
62
64
|
|
63
65
|
# Returns true if +self+ is a child of +comment+.
|
64
66
|
def child_of?(comment)
|
65
|
-
@parent && @parent.id == comment.id
|
67
|
+
@parent && comment.respond_to?(:id) && @parent.id == comment.id
|
66
68
|
end
|
67
69
|
|
68
|
-
#
|
69
|
-
|
70
|
-
|
70
|
+
# Returns 1 if +self+ is older than +comment+, and -1 if +comment+ is older
|
71
|
+
# than +self+. Since comments can't be exactly the same age, never returns 0.
|
72
|
+
#
|
73
|
+
# If +comment+ isn't actually a Comment, returns +nil+.
|
74
|
+
def <=>(comment) #Since IDs are sequential, this sorts by time
|
75
|
+
comment.respond_to?(:id) && comment.id <=> @id
|
71
76
|
end
|
72
77
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
"minute" => 60, "minutes" => 60,
|
77
|
-
"hour" => 3600, "hours" => 3600,
|
78
|
-
"day" => 86400, "days" => 86400
|
79
|
-
}[i.time.split[1]]
|
80
|
-
end
|
81
|
-
|
82
|
-
-(right <=> left)
|
78
|
+
# :nodoc:
|
79
|
+
def inspect
|
80
|
+
"<Comment> by #{@submitter}, #{@time}"
|
83
81
|
end
|
84
82
|
end
|
85
83
|
|
86
84
|
# Represents the list of comments pertaining to a particluar HN story.
|
87
85
|
class CommentPage
|
88
86
|
include Enumerable
|
89
|
-
#
|
87
|
+
# Returns this comment page's title.
|
90
88
|
attr_reader :title
|
89
|
+
# Returns the URL for this comment page on Hacker News.
|
91
90
|
attr_reader :url
|
92
91
|
|
93
92
|
# Returns a new CommentPage corresponding to <tt>url</tt>.
|
94
93
|
# cpage = HackerNews::CommentPage.new\
|
95
94
|
# "https://news.ycombinator.com/item?id=6621679" #=> "C--" <12 Comments>
|
95
|
+
#
|
96
|
+
# Alternately, if passed an integer, returns the CommentPage corresponding to
|
97
|
+
# the story whose ID is +url+.
|
98
|
+
# cpage = HackerNews::CommentPage.new 6621679 #=> "C--" <12 Comments>
|
96
99
|
def initialize(url)
|
97
|
-
|
100
|
+
url = "#{HN_URL}/item?id=#{url.to_i}" if url.is_a? Numeric
|
101
|
+
|
102
|
+
html = Nokogiri::HTML(open(url), nil, "UTF-8")
|
98
103
|
@title = html.title.chomp " | Hacker News"
|
99
104
|
@url = url
|
100
105
|
|
data/lib/hnruby/storylist.rb
CHANGED
@@ -20,7 +20,7 @@ module StoryList
|
|
20
20
|
private
|
21
21
|
def self.update_page(index)
|
22
22
|
url = HN_URL + "#{"news#{index}" if index > 1}"
|
23
|
-
html = (Nokogiri::HTML(open(url)) / "tr").to_a[4..-6]
|
23
|
+
html = (Nokogiri::HTML(open(url), nil, "UTF-8") / "tr").to_a[4..-6]
|
24
24
|
|
25
25
|
(0...html.length).step(3) do |i|
|
26
26
|
item = NewsItem.new(html[i], html[i + 1])
|
@@ -32,7 +32,7 @@ public
|
|
32
32
|
#Clear story cache--- any actual NewsItem objects will still need to be
|
33
33
|
#updated, though.
|
34
34
|
def self.update
|
35
|
-
@@items
|
35
|
+
@@items.clear
|
36
36
|
end
|
37
37
|
|
38
38
|
# If <tt>index</tt> is a number, returns a NewsItem representing the story
|
@@ -53,7 +53,7 @@ public
|
|
53
53
|
elsif i.is_a? Enumerable
|
54
54
|
self.stories i
|
55
55
|
else
|
56
|
-
raise TypeError
|
56
|
+
raise TypeError, "#{i.class} objects cannot be used to access stories"
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
data/test/test_hackernews.rb
CHANGED
@@ -45,6 +45,10 @@ class TestHackerNews < Test::Unit::TestCase
|
|
45
45
|
def test_comments
|
46
46
|
cpage = HackerNews::CommentPage.new File.dirname(__FILE__)+"/commentpage.html"
|
47
47
|
|
48
|
+
assert_equal 6640155, cpage.sort.first.id
|
49
|
+
assert_equal 6637431, cpage.sort.last.id
|
50
|
+
assert_equal 6638715, cpage.sort[20].id
|
51
|
+
|
48
52
|
first = cpage[0]
|
49
53
|
|
50
54
|
assert_equal 0, first.depth
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hnruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.01.
|
4
|
+
version: 0.01.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jem Orgun
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-terminfo
|
@@ -95,3 +95,4 @@ specification_version: 4
|
|
95
95
|
summary: Hacker News scraper
|
96
96
|
test_files:
|
97
97
|
- test/test_hackernews.rb
|
98
|
+
has_rdoc:
|