hnposts 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hnposts/fetcher.rb +39 -35
- data/lib/hnposts/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6efac427f24d1a4408cb79efb8df017f806f636f
|
4
|
+
data.tar.gz: c8fcdd42c307d8beadb2d8279fb86b28a648318b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a19a144f7781967260b796493554dd3264dcbd5259cf6051e910527402d78b7a4463c8e65f0e0ab3d95944ea5a0a422294c4c51c9830d49fa291fd2c049b2d00
|
7
|
+
data.tar.gz: c3776341b87d9bdd3455a7708880ac55be6287cf1ed710d146dab3cc28c1a900cb871ff2620d241074557f0ea9e4faaac447dc2a1e4ead489a86e039cb4d79ed
|
data/lib/hnposts/fetcher.rb
CHANGED
@@ -4,59 +4,63 @@ module HNPosts
|
|
4
4
|
|
5
5
|
HN_URL = "https://news.ycombinator.com/"
|
6
6
|
|
7
|
-
def initialize
|
8
|
-
@hn_doc = Nokogiri::HTML(_contents)
|
9
|
-
end
|
10
|
-
|
11
7
|
def fetch_posts
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
8
|
+
posts = []
|
9
|
+
post_nodes = Nokogiri::HTML(contents).css('td[class=title]:nth-of-type(3)').map {|n| n.parent}
|
10
|
+
|
11
|
+
post_nodes.each_with_index do | node, indx |
|
12
|
+
posts << Post.new({
|
13
|
+
post_id: (post_id node),
|
14
|
+
title: (title node),
|
15
|
+
url: (url node),
|
16
|
+
points: (points node),
|
17
|
+
comments: (comments node),
|
18
|
+
position: (indx + 1)
|
19
|
+
})
|
23
20
|
end
|
21
|
+
|
22
|
+
posts
|
24
23
|
end
|
25
24
|
|
26
25
|
private
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
def post_id tr
|
28
|
+
vote_node = tr.css('td > center > a')[0]
|
29
|
+
title_node = tr.css('td[class=title] > a')[0]
|
30
|
+
if vote_node
|
31
|
+
/^up_(\d+)$/.match(vote_node.attributes['id'])[1]
|
32
|
+
else
|
33
|
+
/^item\?id=(\d+)$/.match(title_node.attributes['href'])[1]
|
34
|
+
end
|
32
35
|
end
|
33
36
|
|
34
|
-
def
|
35
|
-
|
37
|
+
def title tr
|
38
|
+
tr.css('td[class=title] > a')[0].text.strip
|
36
39
|
end
|
37
40
|
|
38
|
-
def
|
39
|
-
|
41
|
+
def url tr
|
42
|
+
href = tr.css('td[class=title] > a')[0].attributes['href'].value
|
43
|
+
/^item\?id=\d+$/ =~ href ? HN_URL + href : href
|
40
44
|
end
|
41
45
|
|
42
|
-
def
|
43
|
-
|
46
|
+
def points tr
|
47
|
+
result = tr.next_sibling.css('span')[0]
|
48
|
+
result &&= /^(\d+) points$/.match(result.text)
|
49
|
+
result &&= result[1]
|
50
|
+
result.to_i
|
44
51
|
end
|
45
52
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
cntr = cntr.next
|
52
|
-
end
|
53
|
-
end
|
53
|
+
def comments tr
|
54
|
+
result = tr.next_sibling.css('a')[1]
|
55
|
+
result &&= /^(\d+) comments$/.match(result.text)
|
56
|
+
result &&= result[1]
|
57
|
+
result.to_i
|
54
58
|
end
|
55
59
|
|
56
|
-
def
|
60
|
+
def contents
|
57
61
|
open(HN_URL)
|
58
62
|
end
|
59
|
-
|
63
|
+
|
60
64
|
end
|
61
65
|
|
62
66
|
end
|
data/lib/hnposts/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hnposts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sankaranarayanan Viswanathan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|