Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Commit

Permalink
set date on answer
Browse files Browse the repository at this point in the history
  • Loading branch information
robmckinnon committed Apr 23, 2008
1 parent 9323b3f commit 96c79cf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
10 changes: 9 additions & 1 deletion lib/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'open-uri'
require 'hpricot'
require 'morph'
require 'date'

class Answer
include Morph
Expand All @@ -17,6 +18,7 @@ def initialize attributes

def self.from_doc doc
title_elements = find_title_elements(doc)
date = find_date(doc)

title_elements.inject([]) do |answers, title_element|
title = find_title_text(title_element)
Expand All @@ -30,6 +32,7 @@ def self.from_doc doc

answer = Answer.new({
:title => title,
:date => date,
:major_title => find_major_title_text(title),
:minor_title => find_minor_title_text(title),
:answering_role => find_answering_role(answer_initial_paragraph),
Expand All @@ -45,6 +48,11 @@ def self.from_doc doc
end
end

def self.find_date doc
date = doc.at('meta[@name="Date"]')['content']
Date.parse(date)
end

def self.find_title_elements doc
(doc/'h3').delete_if {|h| h.inner_text.to_s[/written answers/i]}
end
Expand Down Expand Up @@ -125,7 +133,7 @@ def self.find_answer_paragraphs_text answer_initial_paragraph
private

def self.contains_named_anchor element, pattern
(a = element.at('a')) && (name = a.attributes['name']) && !name.to_s[pattern].nil?
(a = element.at('a')) && (name = a['name']) && !name.to_s[pattern].nil?
end

def self.find_answering_name element
Expand Down
4 changes: 2 additions & 2 deletions lib/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def self.find_question_texts element
element = element.next_sibling
paragraph = element ? element.at('p') : nil
texts = []
while (paragraph && (a = paragraph.at('a')) && (name = a.attributes['name']) && (!name[/wa_qnpa_\d+/].nil?) )
while (paragraph && (a = paragraph.at('a')) && (name = a['name']) && (!name[/wa_qnpa_\d+/].nil?) )
texts << paragraph.inner_text.to_s
element = element.next_sibling
paragraph = element ? element.at('p') : nil
Expand All @@ -59,7 +59,7 @@ def self.find_question_ids question_texts

private
def self.contains_named_anchor element, pattern
(a = element.at('a')) && (name = a.attributes['name']) && !name.to_s[pattern].nil?
(a = element.at('a')) && (name = a['name']) && !name.to_s[pattern].nil?
end

end
8 changes: 7 additions & 1 deletion spec/answer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
title_elements.first.inner_text.to_s.should == @title_text
end

it 'should find date' do
Answer.find_date(H(@date)).should == Date.parse(@date_text)
end

it 'should find title text' do
html = H(@title)
Answer.find_title_text(html).should == @title_text
Expand Down Expand Up @@ -102,7 +106,7 @@
it 'should create Answer instance from Hpricot document with two answers' do
answer = "#{@question_introduction}#{@question}#{@answer_initial_paragraph}#{@answer_2nd_paragraph}#{@answer_3rd_paragraph}"
second_answer = "#{@second_question_introduction}#{@second_question}#{@second_answer_initial_paragraph}#{@second_answer_2nd_paragraph}#{@second_answer_3rd_paragraph}"
html = H("<html>#{@title}#{answer}#{second_answer}</html>")
html = H("<html><head>#{@date}</head><body>#{@title}#{answer}#{second_answer}</body></html>")

questions1 = [mock('Question')]
Question.should_receive(:create_questions).with(html.at("a[@name='#{@question_css_name}']/..")).and_return questions1
Expand All @@ -114,6 +118,7 @@
answers.size.should == 2
answer = answers.first
answer.should be_an_instance_of(Answer)
answer.date.should == Date.parse(@date_text)
answer.title.should == @title_text
answer.major_title.should == @major_title
answer.minor_title.should == @minor_title
Expand All @@ -123,6 +128,7 @@
answer.answer_paragraphs.should == "<p>#{@answer_initial_paragraph_text}</p><p>#{@answer_2nd_paragraph_text}</p><p>#{@answer_3rd_paragraph_text}</p>"

answer2 = answers[1]
answer2.date.should == Date.parse(@date_text)
answer2.title.should == @title_text
answer2.questions.should == questions2
answer2.answering_member.should == @second_answering_member
Expand Down
7 changes: 3 additions & 4 deletions spec/spec_data.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
def load_spec_data
@written_answers_title = %Q|<h3 align="center"><a name="08040381000002"></a>Written Answers</h3>|

@date = %Q|<h2 align="center">
<a name="wa_date_0"></a>
<a name="08040381000001"></a>
<i>Thursday 3 April 2008</i></h2>|

@date_text = %Q|3 Apr 2008|
@date = %Q|<meta name="Date" content="#{@date_text}">|

@major_title = %Q|Agriculture|
@minor_title = %Q|Disease Testing|
Expand Down

0 comments on commit 96c79cf

Please sign in to comment.