diff --git a/lib/answer.rb b/lib/answer.rb index fcd27810..44fb2ca8 100644 --- a/lib/answer.rb +++ b/lib/answer.rb @@ -2,6 +2,7 @@ require 'open-uri' require 'hpricot' require 'morph' +require 'date' class Answer include Morph @@ -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) @@ -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), @@ -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 @@ -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 diff --git a/lib/question.rb b/lib/question.rb index ad73092f..b4813fb2 100644 --- a/lib/question.rb +++ b/lib/question.rb @@ -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 @@ -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 diff --git a/spec/answer_spec.rb b/spec/answer_spec.rb index ea476191..315d3910 100644 --- a/spec/answer_spec.rb +++ b/spec/answer_spec.rb @@ -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 @@ -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("#{@title}#{answer}#{second_answer}") + html = H("
#{@date}#{@title}#{answer}#{second_answer}") questions1 = [mock('Question')] Question.should_receive(:create_questions).with(html.at("a[@name='#{@question_css_name}']/..")).and_return questions1 @@ -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 @@ -123,6 +128,7 @@ answer.answer_paragraphs.should == "#{@answer_initial_paragraph_text}
#{@answer_2nd_paragraph_text}
#{@answer_3rd_paragraph_text}
" 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 diff --git a/spec/spec_data.rb b/spec/spec_data.rb index 0fc12b7b..25da37bb 100644 --- a/spec/spec_data.rb +++ b/spec/spec_data.rb @@ -1,10 +1,9 @@ def load_spec_data @written_answers_title = %Q|