yahoo 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright 2006 Eric Hodel, The Robot Co-op. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+ 3. Neither the names of the authors nor the names of their contributors
13
+ may be used to endorse or promote products derived from this software
14
+ without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
17
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
20
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22
+ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23
+ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ LICENSE
2
+ Manifest.txt
3
+ README
4
+ Rakefile
5
+ lib/yahoo.rb
6
+ test/test_yahoo.rb
7
+ test/uri_stub.rb
data/README ADDED
@@ -0,0 +1,25 @@
1
+ = yahoo
2
+
3
+ Rubyforge Project:
4
+
5
+ http://rubyforge.org/projects/rctools/
6
+
7
+ Documentation:
8
+
9
+ http://dev.robotcoop.com/Libraries/yahoo/
10
+
11
+ == About
12
+
13
+ This is an abstract class for implementing Yahoo's web services APIs. By
14
+ itself it isn't at all useful.
15
+
16
+ == Installing yahoo
17
+
18
+ Just install the gem:
19
+
20
+ $ sudo gem install yahoo
21
+
22
+ == Using yahoo
23
+
24
+ Yahoo is used by gems such as yahoo-geocode and yahoo-search. If you'd like to write bindings a yahoo web service see those gems for examples.
25
+
data/Rakefile ADDED
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+
7
+ $VERBOSE = nil
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'yahoo'
11
+ s.version = '1.0.0'
12
+ s.summary = 'Base for Yahoo web services'
13
+ s.description = 'This library makes it easy to implement Yahoo\'s web services APIs.'
14
+ s.author = 'Eric Hodel'
15
+ s.email = 'eric@robotcoop.com'
16
+
17
+ s.has_rdoc = true
18
+ s.files = File.read('Manifest.txt').split($/)
19
+ s.require_path = 'lib'
20
+ end
21
+
22
+ desc 'Run tests'
23
+ task :default => [ :test ]
24
+
25
+ Rake::TestTask.new('test') do |t|
26
+ t.libs << 'test'
27
+ t.pattern = 'test/test_*.rb'
28
+ t.verbose = true
29
+ end
30
+
31
+ desc 'Update Manifest.txt'
32
+ task :update_manifest do
33
+ sh "find . -type f | sed -e 's%./%%' | egrep -v 'svn|swp|~' | egrep -v '^(doc|pkg)/' | sort > Manifest.txt"
34
+ end
35
+
36
+ desc 'Generate RDoc'
37
+ Rake::RDocTask.new :rdoc do |rd|
38
+ rd.rdoc_dir = 'doc'
39
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
40
+ rd.main = 'README'
41
+ rd.options << '-d' if `which dot` =~ /\/dot/
42
+ rd.options << '-t Yahoo Web Services'
43
+ end
44
+
45
+ desc 'Generate RDoc for dev.robotcoop.com'
46
+ Rake::RDocTask.new :dev_rdoc do |rd|
47
+ rd.rdoc_dir = '../../../www/trunk/dev/html/Libraries/yahoo'
48
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
49
+ rd.main = 'README'
50
+ rd.options << '-d' if `which dot` =~ /\/dot/
51
+ rd.options << '-t Yahoo Web Services'
52
+ end
53
+
54
+ desc 'Build Gem'
55
+ Rake::GemPackageTask.new spec do |pkg|
56
+ pkg.need_tar = true
57
+ end
58
+
59
+ desc 'Clean up'
60
+ task :clean => [ :clobber_rdoc, :clobber_package ]
61
+
62
+ desc 'Clean up'
63
+ task :clobber => [ :clean ]
64
+
65
+ # vim: syntax=Ruby
66
+
data/lib/yahoo.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'open-uri'
2
+ require 'rexml/document'
3
+
4
+ ##
5
+ # Abstract class for implementing Yahoo APIs.
6
+ #
7
+ # http://developer.yahoo.com/
8
+
9
+ class Yahoo
10
+
11
+ ##
12
+ # Yahoo error class.
13
+
14
+ class Error < RuntimeError; end
15
+
16
+ ##
17
+ # Web services initializer.
18
+ #
19
+ # The +appid+ is the Application ID that uniquely identifies your
20
+ # application. See: http://developer.yahoo.com/faq/index.html#appid
21
+ #
22
+ # Concrete web services implementations need to set the following instance
23
+ # variables then call super:
24
+ #
25
+ # +host+:: API endpoint hostname
26
+ # +service_name+:: service name
27
+ # +version+:: service name version number
28
+ # +method+:: service method call
29
+ #
30
+ # See http://developer.yahoo.com/search/rest.html
31
+
32
+ def initialize(appid)
33
+ @appid = appid
34
+ @url = URI.parse "http://#{@host}/#{@service_name}/#{@version}/#{@method}"
35
+ end
36
+
37
+ ##
38
+ # Extracts and raises an error from +obj+. Returns if no error could be
39
+ # found.
40
+
41
+ def check_error(obj)
42
+ obj = REXML::Document.new obj.read unless REXML::Document === obj
43
+
44
+ err = obj.elements['Error']
45
+ raise Error, err.elements['Message'].text if err
46
+ end
47
+
48
+ ##
49
+ # Performs a GET request with +params+. Calls the parse_response method on
50
+ # the concrete class with an REXML::Document instance and returns its
51
+ # result.
52
+
53
+ def get(params)
54
+ url = make_url params
55
+
56
+ url.open do |xml|
57
+ res = REXML::Document.new xml.read
58
+
59
+ check_error res
60
+
61
+ return parse_response(res)
62
+ end
63
+ rescue OpenURI::HTTPError => e
64
+ check_error e.io
65
+ raise
66
+ end
67
+
68
+ ##
69
+ # Creates a URL from the Hash +params+. Automatically adds the appid and
70
+ # sets the output type to 'xml'.
71
+
72
+ def make_url(params)
73
+ params[:appid] = @appid
74
+ params[:output] = 'xml'
75
+
76
+ escaped_params = params.sort_by { |k,v| k.to_s }.map do |k,v|
77
+ "#{URI.escape k.to_s}=#{URI.escape v.to_s}"
78
+ end
79
+
80
+ url = @url.dup
81
+ url.query = escaped_params.join '&'
82
+ return url
83
+ end
84
+
85
+ end
86
+
@@ -0,0 +1,93 @@
1
+ require 'test/unit'
2
+ require 'test/uri_stub'
3
+ require 'yahoo'
4
+
5
+ class Yahoo::Test < Yahoo
6
+
7
+ def initialize(*args)
8
+ @host = 'api.test.yahoo.com'
9
+ @service_name = 'TestService'
10
+ @version = 'Vtest'
11
+ @method = 'test'
12
+ super
13
+ end
14
+
15
+ def test
16
+ get :test_param => 5
17
+ end
18
+
19
+ def parse_response(xml)
20
+ return xml
21
+ end
22
+
23
+ end
24
+
25
+ class TestYahoo < Test::Unit::TestCase
26
+
27
+ def setup
28
+ URI::HTTP.responses = []
29
+ URI::HTTP.uris = []
30
+
31
+ @t = Yahoo::Test.new 'APP_ID'
32
+ end
33
+
34
+ def test_check_error_IO
35
+ io = StringIO.new '<Error><Message>you broked it</Message></Error>'
36
+ @t.check_error io
37
+
38
+ rescue Yahoo::Error => e
39
+ assert_equal 'you broked it', e.message
40
+
41
+ else
42
+ flunk 'expected an error'
43
+ end
44
+
45
+ def test_check_error_REXML__Document
46
+ xml = REXML::Document.new '<Error><Message>you broked it</Message></Error>'
47
+ @t.check_error xml
48
+
49
+ rescue Yahoo::Error => e
50
+ assert_equal 'you broked it', e.message
51
+
52
+ else
53
+ flunk 'expected an error'
54
+ end
55
+
56
+ def test_get
57
+ xml = '<Result>stuff</Result>'
58
+ URI::HTTP.responses << xml
59
+
60
+ result = @t.test
61
+
62
+ assert_equal xml, result.to_s
63
+ end
64
+
65
+ def test_get_error
66
+ def @t.make_url(*args) # HACK extend uri_stub with error raising ability
67
+ u = Object.new
68
+ def u.open
69
+ xml = '<Error><Message>you did the bad thing</Message></Error>'
70
+ raise OpenURI::HTTPError.new('400 Bad Request', StringIO.new(xml))
71
+ end
72
+ return u
73
+ end
74
+
75
+ assert_raise Yahoo::Error do @t.test end
76
+ end
77
+
78
+ def test_initialize
79
+ assert_equal 'http://api.test.yahoo.com/TestService/Vtest/test',
80
+ @t.instance_variable_get(:@url).to_s
81
+ end
82
+
83
+ def test_make_url
84
+ url = @t.make_url :test_param_1 => 'test test',
85
+ :test_param_2 => 'tset tset'
86
+
87
+ expected = 'http://api.test.yahoo.com/TestService/Vtest/test?appid=APP_ID&output=xml&test_param_1=test%20test&test_param_2=tset%20tset'
88
+
89
+ assert_equal expected, url.to_s
90
+ end
91
+
92
+ end
93
+
data/test/uri_stub.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'open-uri'
2
+
3
+ class URI::HTTP
4
+
5
+ class << self
6
+ attr_accessor :responses, :uris
7
+ end
8
+
9
+ alias original_open open
10
+
11
+ def open
12
+ self.class.uris << self.to_s
13
+ yield StringIO.new(self.class.responses.shift)
14
+ end
15
+
16
+ end
17
+
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11.15
3
+ specification_version: 1
4
+ name: yahoo
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-06-10 00:00:00 -07:00
8
+ summary: Base for Yahoo web services
9
+ require_paths:
10
+ - lib
11
+ email: eric@robotcoop.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: This library makes it easy to implement Yahoo's web services APIs.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Eric Hodel
31
+ files:
32
+ - LICENSE
33
+ - Manifest.txt
34
+ - README
35
+ - Rakefile
36
+ - lib/yahoo.rb
37
+ - test/test_yahoo.rb
38
+ - test/uri_stub.rb
39
+ test_files: []
40
+
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ requirements: []
50
+
51
+ dependencies: []
52
+