yahoo-answers 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .idea
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Felipe Koch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = yahoo-answers
2
+
3
+ Github Project:
4
+ http://github.com/felipekk/yahoo-answers-gem
5
+
6
+ == Requirements
7
+
8
+ This gem requires Eric Hodel and David N. Welton's yahoo gem:
9
+
10
+ http://github.com/davidw/yahoo-gem
11
+
12
+ Also check out their Yahoo Web Search and Yahoo Location Search gem:
13
+
14
+ http://github.com/davidw/yahoo-search-gem
15
+
16
+ == Description
17
+
18
+ This is a simple implementation of version 1 of Yahoo! Answers API. It allows you to search Yahoo! Answers, returning
19
+ the questions found and it's answers.
20
+
21
+ == Using Yahoo::Answers
22
+
23
+ First you'll need a Yahoo Application ID. You can register for one here:
24
+
25
+ http://api.search.yahoo.com/webservices/register_application
26
+
27
+ Then simply create a Yahoo::Answers object with your API Key and start searching for answers:
28
+
29
+ require 'rubygems'
30
+ require 'yahoo/answers'
31
+
32
+ ya = Yahoo::Answers.new api_key
33
+
34
+ results = ys.search(:query => 'cars')
35
+
36
+ results.each do |result|
37
+ puts result.inspect
38
+ end
39
+
40
+ == Note on Patches/Pull Requests
41
+
42
+ * Fork the project.
43
+ * Make your feature addition or bug fix.
44
+ * Add tests for it. This is important so I don't break it in a
45
+ future version unintentionally.
46
+ * Commit, do not mess with rakefile, version, or history.
47
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
48
+ * Send me a pull request. Bonus points for topic branches.
49
+
50
+ == Copyright
51
+
52
+ Copyright (c) 2010 Felipe Koch. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "yahoo-answers"
8
+ gem.summary = "A Ruby Yahoo! Answers API Implementation"
9
+ gem.description = "An interface to Yahoo! Answers service"
10
+ gem.email = "felipekk@gmail.com"
11
+ gem.homepage = "http://github.com/felipekk/yahoo-answers-gem"
12
+ gem.authors = ["Felipe Koch"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ gem.add_dependency "yahoo", ">= 2.0.0"
15
+ gem.files.add 'lib/yahoo/answers.rb'
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "yahoo-answers #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,89 @@
1
+ require 'yahoo'
2
+
3
+ ##
4
+ # Yahoo! Answers API
5
+ #
6
+ # http://developer.yahoo.com/answers/V1/questionSearch.html
7
+
8
+ class Yahoo::Answers < Yahoo
9
+
10
+ Question = Struct.new :id, :type, :subject, :content, :date, :timestamp, :link, :category_id, :category, :user_id,
11
+ :user_nick, :user_photo_url, :num_answers, :num_comments, :answer, :answerer_id,
12
+ :answerer_nick, :answer_timestamp, :answer_award_timestamp
13
+
14
+ def initialize(*args) # :nodoc:
15
+ @host = 'answers.yahooapis.com'
16
+ @service_name = 'AnswersService'
17
+ @version = 'V1'
18
+ super
19
+ end
20
+
21
+ ##
22
+ # Searches the web for <tt>:query</tt> and returns up to <tt>:results</tt> results
23
+ #
24
+ # <tt>:results</tt> defaults to 10 questions, maximum per Yahoo! Answers API is 50
25
+ #
26
+ # All options on http://developer.yahoo.com/answers/V1/questionSearch.html can be used
27
+
28
+ def search(options)
29
+ params = { :results => 10 }.merge(options)
30
+ get :questionSearch, params
31
+ end
32
+
33
+ def parse_response(xml) # :nodoc:
34
+ results = []
35
+
36
+ xml.xpath('//xmlns:Question').each do |r|
37
+ result = Question.new
38
+
39
+ result.id = r.attribute('id').content
40
+ result.type = r.attribute('type').content
41
+ result.subject = r.at_xpath('xmlns:Subject').content
42
+ result.content = r.at_xpath('xmlns:Content').content
43
+ result.date = r.at_xpath('xmlns:Date').content
44
+ result.timestamp = r.at_xpath('xmlns:Timestamp').content.to_i
45
+ result.link = r.at_xpath('xmlns:Link').content
46
+ result.category_id = r.at_xpath('xmlns:Category').attribute('id').content.to_i
47
+ result.category = r.at_xpath('xmlns:Category').content
48
+ result.user_id = r.at_xpath('xmlns:UserId').content
49
+ result.user_nick = r.at_xpath('xmlns:UserNick').content
50
+ result.user_photo_url = r.at_xpath('xmlns:UserPhotoURL').content
51
+ result.num_answers = r.at_xpath('xmlns:NumAnswers').content.to_i
52
+ result.num_comments = r.at_xpath('xmlns:NumComments').content.to_i
53
+ result.answer = r.at_xpath('xmlns:ChosenAnswer').content
54
+ result.answerer_id = r.at_xpath('xmlns:ChosenAnswererId').content
55
+ result.answerer_nick = r.at_xpath('xmlns:ChosenAnswererNick').content
56
+ result.answer_timestamp = r.at_xpath('xmlns:ChosenAnswerTimestamp').content.to_i
57
+ result.answer_award_timestamp = r.at_xpath('xmlns:ChosenAnswerAwardTimestamp').content.to_i
58
+
59
+ results << result
60
+ end
61
+
62
+ return results
63
+ end
64
+ end
65
+
66
+ ##
67
+ # A Result contains the following fields
68
+ #
69
+ # +id+:: unique id of the question
70
+ # +type+:: state of the question (answered, open, ...)
71
+ # +subject+:: question subject
72
+ # +content+:: question text
73
+ # +date+:: pre-formatted date of when the question was submitted
74
+ # +timestamp+:: unix timestamp of when the question was submitted
75
+ # +link+:: link to the question
76
+ # +category+:: category that the question is listed under
77
+ # +category_id+:: unique id of the category
78
+ # +user_id+:: unique id of the user who posted the question
79
+ # +user_nick+:: nickname of the user who posted the question
80
+ # +user_photo_url+:: URL of the user's photo
81
+ # +num_answers+:: number of answers to the question
82
+ # +num_comments+:: number of comments to the question
83
+ # +answer+:: the text of the chosen answer
84
+ # +answer_timestamp+:: unix time of when the answer was submitted
85
+ # +answer_award_timestamp+:: unix time of when the answer was given an award
86
+ # +answerer_id+:: unique id of the user who submitted the answer
87
+ # +answerer_nick+:: nickname of the user who submitted the answer
88
+
89
+ class Yahoo::Answers::Result; end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rc_rest/uri_stub'
4
+ require 'shoulda'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'yahoo/answers'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,99 @@
1
+ require 'helper'
2
+
3
+ class Yahoo::TestAnswers < Test::Unit::TestCase
4
+
5
+ context "given a response" do
6
+ setup do
7
+ URI::HTTP.responses = []
8
+ URI::HTTP.responses << <<-EOF.strip
9
+ <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
10
+ <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:answers" xsi:schemaLocation="urn:yahoo:answers http://answers.yahooapis.com/AnswersService/V1/QuestionResponse.xsd">
11
+ <Question id="20080217202924AAkuKbJ" type="Answered">
12
+ <Subject>***cars?***?</Subject>
13
+ <Content>has anyone seen the new go smart cars? i dont like them. to me it looks like a car that got cut in half. my mom wants to buy one for me. wat do u think of them?</Content>
14
+ <Date>2008-02-17 20:29:24</Date>
15
+ <Timestamp>1203308964</Timestamp>
16
+ <Link>http://answers.yahoo.com/question/?qid=20080217202924AAkuKbJ</Link>
17
+ <Category id="396545312">Buying and Selling</Category>
18
+ <UserId>xryGBiRVaa</UserId>
19
+ <UserNick>@13x</UserNick>
20
+ <UserPhotoURL/>
21
+ <NumAnswers>3</NumAnswers>
22
+ <NumComments>6</NumComments>
23
+ <ChosenAnswer>I think there an oxymoron. Smart people do not buy them. You would be better off buying Barbies Corvette.</ChosenAnswer>
24
+ <ChosenAnswererId>4e22efbfa0c39c853547002dfd47db5faa</ChosenAnswererId>
25
+ <ChosenAnswererNick>Just Me</ChosenAnswererNick>
26
+ <ChosenAnswerTimestamp>1203310721</ChosenAnswerTimestamp>
27
+ <ChosenAnswerAwardTimestamp>1203558774</ChosenAnswerAwardTimestamp>
28
+ </Question>
29
+ <Question id="20100110090837AACoHlh" type="Open">
30
+ <Subject>How can i determine the speed of the cars?</Subject>
31
+ <Content>I just wanted to know, generally, how can i figure out what the speed of two cars was before they got into an accident, to determine which car was speeding. I know skid marks were left and the conditions were dry, level and smooth.</Content>
32
+ <Date>2010-01-10 09:08:37</Date>
33
+ <Timestamp>1263143317</Timestamp>
34
+ <Link>http://answers.yahoo.com/question/?qid=20100110090837AACoHlh</Link>
35
+ <Category id="396545317">Other - Cars and Transportation</Category>
36
+ <UserId>EkIFD4K4aa</UserId>
37
+ <UserNick>nhh</UserNick>
38
+ <UserPhotoURL>http://l.yimg.com/a/i/identity/nopic_48.gif</UserPhotoURL>
39
+ <NumAnswers>1</NumAnswers>
40
+ <NumComments>0</NumComments>
41
+ <ChosenAnswer/>
42
+ <ChosenAnswererId/>
43
+ <ChosenAnswererNick/>
44
+ <ChosenAnswerTimestamp/>
45
+ <ChosenAnswerAwardTimestamp/>
46
+ </Question>
47
+ </ResultSet>
48
+ <!-- ws13.ydn.ac4.yahoo.com compressed/chunked Wed Jan 27 08:50:07 PST 2010 -->
49
+ EOF
50
+ URI::HTTP.uris = []
51
+
52
+ @answer = Yahoo::Answers.new 'APP_ID'
53
+ end
54
+
55
+ context "when doing a search" do
56
+ setup do
57
+ @results = @answer.search(:query => 'cars')
58
+ end
59
+
60
+ should("have two results") { assert_equal 2, @results.size }
61
+
62
+ context "the first result" do
63
+ setup do
64
+ @result = @results.first
65
+ end
66
+
67
+ should("have correct id") { assert_equal "20080217202924AAkuKbJ", @result.id }
68
+ should("have correct type") { assert_equal "Answered", @result.type }
69
+ should("have correct subject") { assert_equal "***cars?***?", @result.subject }
70
+ should("have correct content") { assert_equal "has anyone seen the new go smart cars? i dont like them. to me it looks like a car that got cut in half. my mom wants to buy one for me. wat do u think of them?", @result.content }
71
+ should("have correct date") { assert_equal "2008-02-17 20:29:24", @result.date }
72
+ should("have correct timestamp") { assert_equal 1203308964, @result.timestamp }
73
+ should("have correct link") { assert_equal "http://answers.yahoo.com/question/?qid=20080217202924AAkuKbJ", @result.link }
74
+ should("have correct category id") { assert_equal 396545312, @result.category_id }
75
+ should("have correct category") { assert_equal "Buying and Selling", @result.category }
76
+ should("have correct user id") { assert_equal "xryGBiRVaa", @result.user_id }
77
+ should("have correct user nick") { assert_equal "@13x", @result.user_nick }
78
+ should("have blank user photo url") { assert_equal "", @result.user_photo_url }
79
+ should("have correct num answers") { assert_equal 3, @result.num_answers }
80
+ should("have correct num comments") { assert_equal 6, @result.num_comments }
81
+ should("have correct answer") { assert_equal "I think there an oxymoron. Smart people do not buy them. You would be better off buying Barbies Corvette.", @result.answer }
82
+ should("have correct answer timestamp") { assert_equal 1203310721, @result.answer_timestamp }
83
+ should("have correct answerer id") { assert_equal "4e22efbfa0c39c853547002dfd47db5faa", @result.answerer_id }
84
+ should("have correct answerer nick") { assert_equal "Just Me", @result.answerer_nick }
85
+ end
86
+
87
+ context "the last result" do
88
+ setup do
89
+ @result = @results.last
90
+ end
91
+
92
+ should("have correct user photo url") { assert_equal "http://l.yimg.com/a/i/identity/nopic_48.gif", @result.user_photo_url }
93
+ should("have blank answer") { assert_equal "", @result.answer }
94
+ should("have correct answer timestamp") { assert_equal 0, @result.answer_timestamp }
95
+ should("have correct answer award timestamp") { assert_equal 0, @result.answer_award_timestamp }
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{yahoo-answers}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Felipe Koch"]
12
+ s.date = %q{2010-01-27}
13
+ s.description = %q{An interface to Yahoo! Answers service}
14
+ s.email = %q{felipekk@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/yahoo/answers.rb",
27
+ "test/helper.rb",
28
+ "test/test_answers.rb",
29
+ "yahoo-answers.gemspec"
30
+ ]
31
+ s.homepage = %q{http://github.com/felipekk/yahoo-answers-gem}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{A Ruby Yahoo! Answers API Implementation}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_answers.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
47
+ s.add_runtime_dependency(%q<yahoo>, [">= 2.0.0"])
48
+ else
49
+ s.add_dependency(%q<shoulda>, [">= 0"])
50
+ s.add_dependency(%q<yahoo>, [">= 2.0.0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<shoulda>, [">= 0"])
54
+ s.add_dependency(%q<yahoo>, [">= 2.0.0"])
55
+ end
56
+ end
57
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yahoo-answers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Felipe Koch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-27 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yahoo
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ version:
35
+ description: An interface to Yahoo! Answers service
36
+ email: felipekk@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/yahoo/answers.rb
52
+ - test/helper.rb
53
+ - test/test_answers.rb
54
+ - yahoo-answers.gemspec
55
+ has_rdoc: true
56
+ homepage: http://github.com/felipekk/yahoo-answers-gem
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: A Ruby Yahoo! Answers API Implementation
83
+ test_files:
84
+ - test/helper.rb
85
+ - test/test_answers.rb