Class: YahooAnswersScraper::Question

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo_answers_scraper/question.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Question

Returns a new instance of Question.



5
6
7
8
9
10
11
12
13
# File 'lib/yahoo_answers_scraper/question.rb', line 5

def initialize(attributes={})
  @question = attributes[:question]
  @link = attributes[:link]

  @question_body = nil

  @answer_texts = []
  @answer_htmls = []
end

Instance Attribute Details

Returns the value of attribute link.



3
4
5
# File 'lib/yahoo_answers_scraper/question.rb', line 3

def link
  @link
end

#questionObject (readonly)

Returns the value of attribute question.



3
4
5
# File 'lib/yahoo_answers_scraper/question.rb', line 3

def question
  @question
end

Instance Method Details

#answers(mode = :text) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/yahoo_answers_scraper/question.rb', line 42

def answers(mode=:text)
  case mode
  when :text
    @answer_texts
  when :html
    @answer_htmls
  else
    fail
  end
end

#fetchObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yahoo_answers_scraper/question.rb', line 15

def fetch
  doc = Nokogiri::HTML.parse(open(@link))
  contents = doc.css('div.content').to_a

  @question_body = contents[0]

  @answers = contents[1..-1]
  @answer_texts = @answers.map(&:text)
  @answer_htmls = @answers.map(&:to_s)

  @answer_texts.map(&:strip!)

  self
end

#inspectObject



53
54
55
# File 'lib/yahoo_answers_scraper/question.rb', line 53

def inspect
  "#<YahooAnswersScraper::Question question: #{@question.inspect}>"
end

#question_body(mode = :text) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yahoo_answers_scraper/question.rb', line 30

def question_body(mode=:text)
  return nil unless @question_body
  case mode
  when :text
    @question_body.text.strip
  when :html
    @question_body.to_s
  else
    fail
  end
end