Class: Yahoo::Answers

Inherits:
Yahoo
  • Object
show all
Defined in:
lib/yahoo/answers.rb

Overview

Defined Under Namespace

Classes: Question

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Answers

:nodoc:



14
15
16
17
18
19
# File 'lib/yahoo/answers.rb', line 14

def initialize(*args) # :nodoc:
  @host = 'answers.yahooapis.com'
  @service_name = 'AnswersService'
  @version = 'V1'
  super
end

Instance Method Details

#parse_response(xml) ⇒ Object

:nodoc:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yahoo/answers.rb', line 33

def parse_response(xml) # :nodoc:
  results = []

  xml.xpath('//xmlns:Question').each do |r|
    result = Question.new

    result.id = r.attribute('id').content
    result.type = r.attribute('type').content
    result.subject = r.at_xpath('xmlns:Subject').content
    result.content = r.at_xpath('xmlns:Content').content
    result.date = r.at_xpath('xmlns:Date').content
    result.timestamp = r.at_xpath('xmlns:Timestamp').content.to_i
    result.link = r.at_xpath('xmlns:Link').content
    result.category_id = r.at_xpath('xmlns:Category').attribute('id').content.to_i
    result.category = r.at_xpath('xmlns:Category').content
    result.user_id = r.at_xpath('xmlns:UserId').content
    result.user_nick = r.at_xpath('xmlns:UserNick').content
    result.user_photo_url = r.at_xpath('xmlns:UserPhotoURL').content
    result.num_answers = r.at_xpath('xmlns:NumAnswers').content.to_i
    result.num_comments = r.at_xpath('xmlns:NumComments').content.to_i
    result.answer = r.at_xpath('xmlns:ChosenAnswer').content
    result.answerer_id = r.at_xpath('xmlns:ChosenAnswererId').content
    result.answerer_nick = r.at_xpath('xmlns:ChosenAnswererNick').content
    result.answer_timestamp = r.at_xpath('xmlns:ChosenAnswerTimestamp').content.to_i
    result.answer_award_timestamp = r.at_xpath('xmlns:ChosenAnswerAwardTimestamp').content.to_i

    results << result
  end

  return results
end

#search(options) ⇒ Object

Searches the web for :query and returns up to :results results

:results defaults to 10 questions, maximum per Yahoo! Answers API is 50

All options on developer.yahoo.com/answers/V1/questionSearch.html can be used



28
29
30
31
# File 'lib/yahoo/answers.rb', line 28

def search(options)
  params = { :results => 10 }.merge(options)
  get :questionSearch, params
end