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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e82d7afe6c2a5c9fc820bb04b2b052a946f638b7
4
- data.tar.gz: 298908d01dc037d5a236d310edb4ae60d34822ec
3
+ metadata.gz: 6efac427f24d1a4408cb79efb8df017f806f636f
4
+ data.tar.gz: c8fcdd42c307d8beadb2d8279fb86b28a648318b
5
5
  SHA512:
6
- metadata.gz: e30d29de112fcef550d19e4d3597719381b8dcb4c9213290ec87d15f01854c72454200598c5b0e1babb09bafb50a16521e2c56b4b5da5ceab627d1f092919983
7
- data.tar.gz: 580dd518c0f95e2be4bc9e19517552f0e7cb2c8c7fa0af24d0db63530961679a154ef1f5794a0788d38d507d453b70777e2062ebbff5d17cee86a27c3ad00b48
6
+ metadata.gz: a19a144f7781967260b796493554dd3264dcbd5259cf6051e910527402d78b7a4463c8e65f0e0ab3d95944ea5a0a422294c4c51c9830d49fa291fd2c049b2d00
7
+ data.tar.gz: c3776341b87d9bdd3455a7708880ac55be6287cf1ed710d146dab3cc28c1a900cb871ff2620d241074557f0ea9e4faaac447dc2a1e4ead489a86e039cb4d79ed
@@ -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
- return @posts if @posts
13
-
14
- post_ids, titles, urls, points, comments = _post_ids, _titles, _urls, _points, _comments
15
- post_count = post_ids.size
16
- @posts = (0...post_count).collect do | indx |
17
- Post.new post_id: post_ids[indx],
18
- title: titles[indx],
19
- url: urls[indx],
20
- points: points[indx],
21
- comments: comments[indx],
22
- position: (indx + 1)
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
- attr_reader :hn_doc
29
-
30
- def _post_ids
31
- hn_doc.css('td > center > a').collect { |tag| /up_(\d+)/.match(tag.attributes['id'].value)[1] }
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 _titles
35
- hn_doc.css('td[class=title] a').collect { |tag| tag.text.strip }
37
+ def title tr
38
+ tr.css('td[class=title] > a')[0].text.strip
36
39
  end
37
40
 
38
- def _urls
39
- hn_doc.css('td[class=title] a').collect { |tag| tag.attributes['href'].value }
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 _points
43
- hn_doc.css('td[class=subtext] span').collect { |tag| tag.text[/\d+/].to_i }
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 _comments
47
- cntr = 0
48
- hn_doc.css('td[class=subtext] a').reduce([]) do | acc, tag |
49
- acc.tap do |acc|
50
- acc << tag.text[/\d+/].to_i if cntr.odd?
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 _contents
60
+ def contents
57
61
  open(HN_URL)
58
62
  end
59
-
63
+
60
64
  end
61
65
 
62
66
  end
@@ -1,3 +1,3 @@
1
1
  module HNPosts
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
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-15 00:00:00.000000000 Z
11
+ date: 2014-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler