tangzero-ryboss 0.8.5

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/README.textile ADDED
@@ -0,0 +1,32 @@
1
+ h2. INSTALLATION
2
+
3
+ @sudo gem install tangzero-ryboss -s http://gems.github.com@
4
+
5
+ h2. USAGE
6
+
7
+ @coming soon@
8
+
9
+ h2. LICENSE:
10
+
11
+ (The MIT License)
12
+
13
+ Copyright (c) 2008 TangZero
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining
16
+ a copy of this software and associated documentation files (the
17
+ 'Software'), to deal in the Software without restriction, including
18
+ without limitation the rights to use, copy, modify, merge, publish,
19
+ distribute, sublicense, and/or sell copies of the Software, and to
20
+ permit persons to whom the Software is furnished to do so, subject to
21
+ the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be
24
+ included in all copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
27
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
30
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rubygems/specification'
3
+ require 'rake'
4
+ require 'rake/gempackagetask'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM = "ryboss"
8
+ GEM_VERSION = "0.8.5"
9
+ SUMMARY = "Ruby Yahoo BOSS Mashup Framework"
10
+ AUTHOR = "TangZero"
11
+ EMAIL = "t4ngz3r0@gmail.com"
12
+ HOMEPAGE = "http://github.com/tangzero/ryboss"
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.summary = SUMMARY
19
+ s.require_paths = ['lib']
20
+ s.files = FileList['lib/**/*.rb', '[A-Z]*'].to_a
21
+
22
+ s.add_dependency(%q<json>, [">= 1.1.3"])
23
+ s.has_rdoc = false
24
+
25
+ s.author = AUTHOR
26
+ s.email = EMAIL
27
+ s.homepage = HOMEPAGE
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new do |t|
31
+ t.spec_files = FileList['spec/**/*_spec.rb']
32
+ t.spec_opts = %w(-fs --color)
33
+ end
34
+
35
+ Rake::GemPackageTask.new(spec) do |pkg|
36
+ pkg.gem_spec = spec
37
+ end
38
+
39
+ desc "Install the gem locally"
40
+ task :install => [:package] do
41
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
42
+ end
43
+
44
+ desc "Create a gemspec file"
45
+ task :make_spec do
46
+ File.open("#{GEM}.gemspec", "w") do |file|
47
+ file.puts spec.to_ruby
48
+ end
49
+ end
data/lib/ryboss.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+ require 'rexml/document'
6
+
7
+ Dir["#{File.dirname(__FILE__)}/ryboss/*.rb"].sort.each { |lib| require lib }
8
+
9
+ module RyBOSS
10
+
11
+ API_VERSION = 'v1'
12
+ API_URL = "http://boss.yahooapis.com/ysearch"
13
+
14
+ %w(app_id proxy_host proxy_port proxy_user proxy_pass).each do |att|
15
+ attr_accessor att.to_sym
16
+ module_function att.to_sym, "#{att}=".to_sym
17
+ end
18
+
19
+ end
@@ -0,0 +1,12 @@
1
+ module RyBOSS
2
+
3
+ class BaseResult
4
+
5
+ def set_parameter(key, value)
6
+ instance_variable_set("@#{key}", value)
7
+ instance_eval("def #{key}; @#{key}; end")
8
+ end
9
+
10
+ end
11
+
12
+ end
@@ -0,0 +1,17 @@
1
+ module RyBOSS
2
+
3
+ class RyBOSSException < Exception
4
+
5
+ alias to_s message
6
+
7
+ def initialize(message)
8
+ @message = message
9
+ end
10
+
11
+ def message
12
+ "RyBOSS Error: #{@message}"
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,45 @@
1
+ module RyBOSS
2
+
3
+ class Request
4
+
5
+ def self.get(url, options = {})
6
+ uri = URI.parse URI.encode(url)
7
+ uri.query = to_query options
8
+ request = Net::HTTP::Get.new uri.request_uri
9
+ response = connection.start(uri.host) { |http| http.request request }
10
+ evaluate_response response
11
+ rescue Exception => e
12
+ raise RyBOSSException, e.message
13
+ end
14
+
15
+ private
16
+ def self.connection()
17
+ Net::HTTP.Proxy RyBOSS.proxy_host, RyBOSS.proxy_port,
18
+ RyBOSS.proxy_user, RyBOSS.proxy_pass
19
+ end
20
+
21
+ def self.to_query(options = {})
22
+ query = []
23
+ options.each { |k, v| query << "#{k}=#{v}" }
24
+ URI.encode query.join('&')
25
+ end
26
+
27
+ def self.evaluate_response(response)
28
+ case response
29
+ when Net::HTTPSuccess
30
+ response.body
31
+ else
32
+ parse_error response
33
+ end
34
+ end
35
+
36
+ def self.parse_error(response)
37
+ data = {}
38
+ doc = REXML::Document.new response.body
39
+ doc.root.elements.each { |el| data[el.name.to_sym] = el.text }
40
+ JSON.generate data
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,16 @@
1
+ module RyBOSS
2
+
3
+ class Result < BaseResult
4
+
5
+ def initialize(data)
6
+ @data = data
7
+ @data.each { |k, v| set_parameter(k, v) }
8
+ end
9
+
10
+ def to_s
11
+ @data.inspect
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,30 @@
1
+ module RyBOSS
2
+
3
+ class ResultSet < BaseResult
4
+
5
+ attr_reader :results
6
+
7
+ def initialize(data)
8
+ @results = []
9
+ if data.has_key? 'ysearchresponse'
10
+ data['ysearchresponse'].each do |k, v|
11
+ if k.include? 'result'
12
+ v.each do |result|
13
+ @results << Result.new(result)
14
+ end
15
+ else
16
+ set_parameter(k, v)
17
+ end
18
+ end
19
+ else
20
+ data.each { |k, v| set_parameter(k, v) }
21
+ end
22
+ end
23
+
24
+ def to_s
25
+ @results.inspect
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,39 @@
1
+ module RyBOSS
2
+
3
+ class Search
4
+
5
+ def self.web(query, options = {})
6
+ find 'web', query, options
7
+ end
8
+
9
+ def self.images(query, options = {})
10
+ find 'images', query, options
11
+ end
12
+
13
+ def self.news(query, options = {})
14
+ find 'news', query, options
15
+ end
16
+
17
+ def self.spelling(query, options = {})
18
+ find 'spelling', query, options
19
+ end
20
+
21
+ private
22
+ def self.find(engine, query, options)
23
+ url = URI.encode "#{API_URL}/#{engine}/#{API_VERSION}/#{query}"
24
+ options = validate options
25
+ data = Request.get(url, options)
26
+ ResultSet.new JSON.parse(data)
27
+ end
28
+
29
+ def self.validate(options)
30
+ raise RyBOSSException, 'appid is not specified' unless RyBOSS.app_id
31
+ raise RyBOSSException, 'start is invalid' if options[:start] and options[:start] < 0
32
+ raise RyBOSSException, 'count is invalid' if options[:count] and options[:count] < 1
33
+ options[:appid] = RyBOSS.app_id
34
+ options
35
+ end
36
+
37
+ end
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tangzero-ryboss
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.5
5
+ platform: ruby
6
+ authors:
7
+ - TangZero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-07 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.3
23
+ version:
24
+ description:
25
+ email: t4ngz3r0@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/ryboss/base_result.rb
34
+ - lib/ryboss/exception.rb
35
+ - lib/ryboss/request.rb
36
+ - lib/ryboss/result.rb
37
+ - lib/ryboss/resultset.rb
38
+ - lib/ryboss/search.rb
39
+ - lib/ryboss.rb
40
+ - Rakefile
41
+ - README.textile
42
+ has_rdoc: false
43
+ homepage: http://github.com/tangzero/ryboss
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Ruby Yahoo BOSS Mashup Framework
68
+ test_files: []
69
+