Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added more exceptions. Improved type checking. Added more ATTRs to Poll.

  • Loading branch information...
commit 365569643fb7b42b24423052e5b8f77b7a344b44 1 parent 1126241
@jcla1 authored
View
15 lib/hn2json.rb
@@ -5,19 +5,22 @@
module HN2JSON
extend HN2JSON
- autoload :Request, 'hn2json/request'
+ autoload :Request, 'hn2json/request'
- autoload :Parser, 'hn2json/parser'
+ autoload :Parser, 'hn2json/parser'
- autoload :Entity, 'hn2json/entity'
+ autoload :Entity, 'hn2json/entity'
autoload :InvalidIdError, 'hn2json/exceptions'
+ autoload :RequestError, 'hn2json/exceptions'
+ autoload :ParseError, 'hn2json/exceptions'
+
+ autoload :VERSION, 'hn2json/version'
- autoload :VERSION, 'hn2json/version'
def find id
- check_for_falsy_id id
- Entity.new id
+ check_for_falsy_id id
+ Entity.new id
end
private
View
4 lib/hn2json/exceptions.rb
@@ -1,4 +1,8 @@
module HN2JSON
class InvalidIdError < StandardError
end
+ class RequestError < StandardError
+ end
+ class ParseError < StandardError
+ end
end
View
36 lib/hn2json/parser.rb
@@ -7,7 +7,11 @@ def initialize response
html = response.html
html.force_encoding "UTF-8"
- @doc = Nokogiri::HTML::DocumentFragment.parse html
+ begin
+ @doc = Nokogiri::HTML::DocumentFragment.parse html
+ rescue
+ raise ParseError, "there was an error parsing the page"
+ end
end
@@ -21,11 +25,11 @@ def determine_type
return :error
end
else
- td = @doc.css('td')[10]
+ td = @doc.css('td')[12]
if td.css('table').length > 0
return :poll
- elsif td.content != ''
+ elsif td.css('form').length == 1
return :discussion
else
return :post
@@ -122,14 +126,32 @@ def get_attrs_poll entity
voting_on = voting_on_from_table @doc.css('td')[12].css('table')[0]
end
+ subtext = @doc.css('.subtext')[0]
+
+ date_regex = /.*\s(.*\s.*\sago)/
+ ago = date_regex.match(subtext.content)[1]
+ date_posted = Chronic.parse(ago).to_s
+
+ posted_by = subtext.css('a')[0].content
+
+ votes = subtext.css('span')[0].content.to_i
+
+ comments = []
+
+ full_comments = @doc.css('td > img[width="0"]').xpath("..").xpath("..").css('.default')
+
+ full_comments.each do |comment|
+ comment_id = comment.css('span a')[1]['href'].gsub("item?id=", '')
+ comments.push comment_id
+ end
entity.add_attrs do |e|
e.title = title
e.fulltext = fulltext
- #e.date_posted = date_posted
- #e.posted_by = posted_by
- #e.votes = votes
- #e.comments = comments
+ e.date_posted = date_posted
+ e.posted_by = posted_by
+ e.votes = votes
+ e.comments = comments
#e.voting_on = voting_on
end
end
View
9 lib/hn2json/request.rb
@@ -15,8 +15,13 @@ def initialize id
def request_page
begin
@html = RestClient.get @complete_url
- rescue Exception
- @html = ""
+ rescue
+ raise RequestError, "there was an error requesting the page, check your connection"
+ end
+
+ if @html == "No such item." || @html == "Unknown."
+ id = @complete_url.gsub(/^.*id\=/, '')
+ raise RequestError, "no such item or id, #{id}"
end
end
View
34 type_test.rb
@@ -0,0 +1,34 @@
+require 'hn2json'
+
+a = HN2JSON.find 4592427 # post
+b = HN2JSON.find 4586128 # post
+
+c = HN2JSON.find 3002492 # poll
+d = HN2JSON.find 2186138 # poll
+e = HN2JSON.find 3746692 # poll
+
+f = HN2JSON.find 3747766 # comment
+g = HN2JSON.find 3748189 # comment
+h = HN2JSON.find 3747404 # comment
+
+i = HN2JSON.find 4591602 # discussion
+j = HN2JSON.find 4590869 # discussion
+k = HN2JSON.find 4540136 # discussion
+
+if a.type != :post && b.type != :post
+ raise RuntimeError, "Something wrong with the posts"
+end
+
+if c.type != :poll && d.type != :poll && e.type != :poll
+ raise RuntimeError, "Something wrong with the polls"
+end
+
+if f.type != :comment && g.type != :comment && h.type != :comment
+ raise RuntimeError, "Something wrong with the comments"
+end
+
+if i.type != :discussion && j.type != :discussion && k.type != :discussion
+ raise RuntimeError, "Something wrong with the disscussions"
+end
+
+puts "All test passed!"
Please sign in to comment.
Something went wrong with that request. Please try again.