Skip to content

Commit

Permalink
Fix entry's second line parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
p-lambert committed Apr 27, 2015
1 parent 33be108 commit aa54c5d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
27 changes: 18 additions & 9 deletions lib/ruby-hackernews/services/parsers/comments_info_parser.rb
Expand Up @@ -2,18 +2,27 @@ module RubyHackernews

class CommentsInfoParser

def initialize(comments_element)
@element = comments_element.search("a")[1]
def initialize(second_line)
@second_line = second_line
end

def parse
comments_info = nil
if @element && @element['href'] =~ /id/
comments = @element.inner_html.split[0].to_i
comments_page = @element['href']
comments_info = CommentsInfo.new(comments, comments_page)
end
return comments_info
return unless comments_link

comments = comments_link.text.split[0].to_i
comments_page = comments_link['href']

CommentsInfo.new(comments, comments_page)
end

private

def comments_link
links.find { |link| link.text =~ /comment|discuss/ }
end

def links
@second_line.css('a')
end

end
Expand Down
8 changes: 4 additions & 4 deletions lib/ruby-hackernews/services/parsers/entry_parser.rb
Expand Up @@ -20,12 +20,12 @@ def parse
number = number_segment.inner_html.sub(".","").to_i if number_segment
link = LinkInfoParser.new(link_segment).parse
voting = VotingInfoParser.new(@first_line.search("td/center/a"), @second_line.search("[@class='subtext']")[0]).parse
user = UserInfoParser.new(@second_line.search("[@class='subtext']")[0]).parse
comments = CommentsInfoParser.new(@second_line.search("[@class='subtext']")[0]).parse
time = TimeInfoParser.new(@second_line.search("[@class='subtext']").children[3]).parse
user = UserInfoParser.new(@second_line).parse
comments = CommentsInfoParser.new(@second_line).parse
time = TimeInfoParser.new(@second_line).parse
return Entry.new(number, link, voting, user, comments, time)
end

end

end
24 changes: 17 additions & 7 deletions lib/ruby-hackernews/services/parsers/time_info_parser.rb
Expand Up @@ -2,18 +2,28 @@ module RubyHackernews

class TimeInfoParser

def initialize(time_element)
@element = time_element
def initialize(second_line)
@second_line = second_line
end

def parse
if @element
value = @element.text.strip.split[0].to_i
unit_of_measure = @element.text.strip.split[1]
end
return TimeInfo.new(value, unit_of_measure)
return unless time_link

value = time_link.text.strip.split[0].to_i
unit_of_measure = time_link.text.strip.split[1]

TimeInfo.new(value, unit_of_measure)
end

private

def time_link
links.find { |link| link.text =~ /\sago/ }
end

def links
@second_line.css('a')
end
end

end
26 changes: 18 additions & 8 deletions lib/ruby-hackernews/services/parsers/user_info_parser.rb
Expand Up @@ -2,17 +2,27 @@ module RubyHackernews

class UserInfoParser

def initialize(user_element)
@element = user_element
def initialize(second_line)
@second_line = second_line
end

def parse
user_element = @element.search("a")[0]
if user_element
user_name = user_element.inner_html
user_page = user_element['href']
end
return UserInfo.new(user_name, user_page)
return unless user_link

user_name = user_link.inner_html
user_page = user_link['href']

UserInfo.new(user_name, user_page)
end

private

def user_link
links.find { |link| link['href'] =~ /user\?id=/ }
end

def links
@second_line.css('a')
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/HNAPI/services/entries/parsers/time_info_parser_spec.rb
@@ -1,10 +1,11 @@
require 'spec_helper'
require File.join(File.dirname(__FILE__), 'parser_helper')

module RubyHackernews
describe TimeInfoParser do

before :each do
@parser = TimeInfoParser.new(stub(:text => " 4 hours ago | "))
@parser = TimeInfoParser.new(ParserHelper.second_line)
end

describe :parse do
Expand All @@ -22,4 +23,3 @@ module RubyHackernews
end
end
end

0 comments on commit aa54c5d

Please sign in to comment.