Permalink
Browse files

rewrote the determine type, because I noticed it didn't work on job p…

…ostings and discussions that are older than 2 months (the reply form disapears)
  • Loading branch information...
1 parent 7ca48eb commit 8ba312d24229d3b0ecbe09d6fb588bd6fd044c89 @jcla1 committed Oct 9, 2012
Showing with 59 additions and 17 deletions.
  1. +20 −16 lib/hn2json/parser.rb
  2. +1 −1 lib/hn2json/version.rb
  3. +38 −0 type_test.rb
View
@@ -19,25 +19,29 @@ def initialize response
def determine_type
- title = @doc.css('.title a')
+ comhead = @doc.css('.comhead')[0]
- if title.length < 1 || title[0].content == "More"
- if @doc.css('td').length > 7
- return :comment
- else
- return :error
- end
+ if !!comhead
+
+ return :post if comhead.content.gsub(/\s\(.*\)\s/, '').length == 0
+
+ return :comment if comhead.content.count('|') >= 2
+
+ end
+
+ subtext = @doc.css('.subtext')[0]
+
+ return :error unless subtext
+
+ return :special if subtext.content.split.length <= 3
+
+ tr = subtext.xpath('..').xpath('..').css('tr')
+
+ if tr.length == 4 || tr.last.css('form').length == 1
+ return :discussion
else
- td = @doc.css('td')[12]
-
- if td.css('table').length > 0
- return :poll
- elsif td.css('form').length == 1
- return :discussion
- else
- return :post
- end
+ return :poll
end
end
View
@@ -1,4 +1,4 @@
module HN2JSON
# Public: String current version of HN2JSON
- VERSION = '0.0.5'
+ VERSION = '0.0.6'
end
View
@@ -1,4 +1,5 @@
require 'hn2json'
+=begin
a = HN2JSON.find 4592427 # post
b = HN2JSON.find 4586128 # post
@@ -31,4 +32,41 @@
raise RuntimeError, "Something wrong with the disscussions"
end
+=end
+
+
+
+dis1 = HN2JSON.find 4601640 # new, no comments
+dis2 = HN2JSON.find 4618637 # new, comments
+dis3 = HN2JSON.find 142923 # old, no comments
+dis4 = HN2JSON.find 139791 # old, comments
+raise RuntimeError, "Something wrong with the disscussions" unless dis1.type == :discussion && dis2.type == :discussion && dis3.type == :discussion && dis4.type == :discussion
+sleep(120)
+
+#poll1 = HN2JSON.find # new, no comments
+#poll2 = HN2JSON.find # new, comments
+poll3 = HN2JSON.find 1036120 # old, no comments
+poll4 = HN2JSON.find 163618 # old, comments
+raise RuntimeError, "Something wrong with the polls" unless poll3.type == :poll && poll4.type == :poll
+sleep(120)
+
+post1 = HN2JSON.find 4626533 # new, no comments
+post2 = HN2JSON.find 4624761 # new, comments
+post3 = HN2JSON.find 2310005 # old, no comments
+post4 = HN2JSON.find 41051 # old, comments
+raise RuntimeError, "Something wrong with the posts" unless post1.type == :post && post2.type == :post && post3.type == :post && post4.type == :post
+sleep(120)
+
+com1 = HN2JSON.find 4626015 # new, no comments
+com2 = HN2JSON.find 4627318 # new, comments
+com3 = HN2JSON.find 139798 # old, no comments
+com4 = HN2JSON.find 3746863 # old, comments
+raise RuntimeError, "Something wrong with the comments" unless com1.type == :comment && com2.type == :comment && com3.type == :comment && com4.type == :comment
+
+sleep(20)
+
+special = HN2JSON.find 4626238 # new, special
+raise RuntimeError, "Something wrong with the special" unless special.type == :special
+
+
puts "All test passed!"

0 comments on commit 8ba312d

Please sign in to comment.