Module: StackOverflow::Questions
- Extended by:
- Engine
- Defined in:
- lib/rstackoverflow/question.rb
Constant Summary
Constants included
from Engine
Engine::Engines, Engine::Github, Engine::Native
Class Method Summary
collapse
Class Method Details
.[](q_index) ⇒ Object
Also known as:
at
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/rstackoverflow/question.rb', line 17
def [](q_index)
return nil unless q_index.is_a? Integer
html = Utils.http_get("/questions/#{q_index}")
href = html.css('a').first.attr('href')
arr = href.split('/')
return nil unless q_index == arr[2].to_i
html = Utils.http_get(href)
q_title = html.css('div[id=question-header]').first.css('a').text
question = html.css('div[class=question]').first
q_detail = question.css('div[class=post-text]').text.strip
q_vote = question.css('span[class~=vote-count-post]').text.to_i
list = question.css('div[class=post-taglist]')
q_labels = list.first.children.css('a').map {|e| e.text}
= question.css('td[class=comment-text]').map {|e| e.text.strip}
answers = html.css('div[class~=answer]').map do |e|
a_index = e.attr('id')[7..-1].to_i
a_detail = e.css('div[class=post-text]').text.strip
a_accepted = e.attr('class').index('accepted-answer') ? true : false
a_vote = e.css('span[class~=vote-count-post]').text.to_i
= e.css('td[class=comment-text]').map {|t| t.text.strip}
Answer.new(a_index, a_detail, a_accepted, a_vote, )
end
Question.new(q_index, "#{Utils::DOMAIN}#{href}",
q_title, q_detail, q_vote, q_labels, ,
answers)
end
|
.add_class(klass, *args) ⇒ Object
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/rstackoverflow/question.rb', line 6
def add_class(klass, *args)
eval <<-init
class #{klass.capitalize}
attr_reader :#{args.join(', :')}
def initialize(#{args.join(', ')})
#{args.map {|e| "@#{e} = #{e};"}.join}
end
end
init
end
|
.search(cond, engine = Native) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/rstackoverflow/question.rb', line 47
def search(cond, engine = Native)
return nil unless cond.is_a? String
case engine
when Native
html = Utils.http_get("/search?q=#{cond.escape!}")
results = html.css('div[class~=search-result]')
results.map do |r|
r_index = r.attr('id')[17..-1].to_i
r_title = r.css('div[class=result-link]').text.strip
r_abstract = r.css('div[class=excerpt]').text.strip
r_vote = r.css('span[class~=vote-count-post]').text.to_i
r_answer_count = r.css('div[class~=answered-accepted]').text.to_i
Result.new(r_index, r_title, r_abstract, r_vote, r_answer_count)
end
when Github
fail InvalidSearchEngine
else
fail InvalidSearchEngine
end
end
|