pablobm-bossman 0.1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Jay Pignata
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 NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,176 @@
1
+ = BOSSMan -- Gem for interaction with the Yahoo BOSS web service
2
+
3
+ == Description
4
+
5
+ BOSSMan can be used to perform image, web and news searches against Yahoo's index. For more information about BOSS (Build your Own Search Service) please refer to http://developer.yahoo.com/search/boss/.
6
+
7
+ == Bugs, Features, Feedback
8
+
9
+ Feedback would be really appreciated. You can send it to me at john.pignata@gmail.com -- tickets can
10
+ be submitted by using Lighthouse: http://pignata.lighthouseapp.com/projects/15761-bossman
11
+
12
+ == Example Usage
13
+
14
+ === Web
15
+
16
+ require 'rubygems'
17
+ require 'bossman'
18
+ include BOSSMan
19
+
20
+ BOSSMan.application_id = <Your Application ID>
21
+
22
+ boss = BOSSMan::Search.web("prospect park", { :count => 5, :filter => "-hate" })
23
+
24
+ puts "Number of results: #{boss.totalhits}"
25
+ puts
26
+
27
+ boss.results.each do |result|
28
+ puts "#{result.title}"
29
+ puts "-" * 80
30
+ puts "#{result.abstract}"
31
+ puts
32
+ end
33
+
34
+ === News
35
+
36
+ require 'rubygems'
37
+ require 'bossman'
38
+ include BOSSMan
39
+
40
+ BOSSMan.application_id = <Your Application ID>
41
+
42
+ boss = BOSSMan::Search.news("brooklyn new york", { :age => "7d" })
43
+
44
+ boss.results.each do |result|
45
+ puts "#{result.title} [from #{result.source}]"
46
+ puts "-" * 80
47
+ puts "#{result.abstract}"
48
+ puts
49
+
50
+ === Images
51
+
52
+ require 'rubygems'
53
+ require 'bossman'
54
+ include BOSSMan
55
+
56
+ BOSSMan.application_id = <Your Application ID>
57
+
58
+ boss = BOSSMan::Search.images("brooklyn dumbo", { :dimensions => "large" })
59
+
60
+ boss.results.each do |result|
61
+ puts "#{result.url}: #{result.abstract}"
62
+ end
63
+
64
+ === Spelling
65
+
66
+ require 'rubygems'
67
+ require 'bossman'
68
+ include BOSSMan
69
+
70
+ BOSSMan.application_id = <Your Application ID>
71
+
72
+ boss = BOSSMan::Search.spelling("Diabretes")
73
+
74
+ puts boss.suggestion
75
+
76
+ == Output Objects
77
+
78
+ Assuming an object bossed called search:
79
+
80
+ search = BOSSMan::Search.web("staten island", 0, 20)
81
+
82
+ === Common
83
+
84
+ search.responsecode : HTTP response code
85
+ search.nextpage : REST URL to next page of search results
86
+ search.count : Number of search results contained in response
87
+ search.start : Search result from which output starts
88
+ search.totalhits : De-duplicated total number of results search yielded
89
+ search.deephits : Total number of results search yielded
90
+ search.results[].clickurl : Tracking URL of result; must be used in user-facing
91
+ anchor tags by BOSS TOU
92
+ search.results[].url : URL of result
93
+
94
+ === Web
95
+
96
+ search.results[].abstract : Description of result with keywords emboldened
97
+ search.results[].title : Document title with keywords emboldened
98
+ search.results[].dispurl : Display URL of result
99
+ search.results[].size : Size of result in bytes
100
+ search.results[].date : Date result was indexed
101
+
102
+ === News
103
+
104
+ search.results[].abstract : Description of news story
105
+ search.results[].title : Title of news story
106
+ search.results[].language : Language of news story
107
+ search.results[].date : Last publication date of news story
108
+ search.results[].time : Last publication time of news story
109
+ search.results[].source : Source of news story
110
+ search.results[].sourceurl : URL of source publication
111
+
112
+ === Images
113
+
114
+ search.results[].abstract : Description of image
115
+ search.results[].filename : Filename of image
116
+ search.results[].size : Size of image
117
+ search.results[].format : Format of image
118
+ search.results[].height : Height of full-size image
119
+ search.results[].date : Last modification date of image (YYYY/MM/DD)
120
+ search.results[].mimetype : MIME type of image
121
+ search.results[].refererclickurl : Link to page where image was found
122
+ search.results[].refererurl : Link to page where image was found
123
+ search.results[].title : Title of image (usually the filename)
124
+ search.results[].width : Width of full-size image
125
+ search.results[].thumbnailurl : URL of thumbnail image
126
+ search.results[].thumbnail_height : Height of thumbnail image
127
+ search.results[].thumbnail_width : Width of thumbnail image
128
+
129
+ === Spelling
130
+
131
+ search.suggestion : Returns spelling suggestion from BOSS
132
+
133
+ === Raw dump formats
134
+
135
+ Resultsets are returnable in JSON, XML and YAML by use of to_json, to_xml (or to_s), to_yaml respectively.
136
+
137
+ search : dumps XML of entire resultset
138
+ search.to_json : dumps JSON of entire resultset
139
+ search.to_yaml : dumps YAML of entire resultset
140
+ search.results[3] : dumps XML of one search result
141
+ search.results[3].to_json : dumps JSON of one search result
142
+ search.results[3].to_yaml : dumps YAML of one search result
143
+
144
+ == Installation
145
+
146
+ jp@populuxe:~/code/ruby$ gem sources -a http://gems.github.com
147
+ jp@populuxe:~/code/ruby$ gem install jpignata-bossman
148
+
149
+ == Requirements
150
+
151
+ * Active Support >= 2.1
152
+
153
+ == LICENSE:
154
+
155
+ (The MIT License)
156
+
157
+ Copyright (c) 2008 Jay Pignata
158
+
159
+ Permission is hereby granted, free of charge, to any person obtaining
160
+ a copy of this software and associated documentation files (the
161
+ 'Software'), to deal in the Software without restriction, including
162
+ without limitation the rights to use, copy, modify, merge, publish,
163
+ distribute, sublicense, and/or sell copies of the Software, and to
164
+ permit persons to whom the Software is furnished to do so, subject to
165
+ the following conditions:
166
+
167
+ The above copyright notice and this permission notice shall be
168
+ included in all copies or substantial portions of the Software.
169
+
170
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
171
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
172
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
173
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
174
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
175
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
176
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "bossman"
3
+ s.version = "0.1.2.1"
4
+ s.date = "2008-08-28"
5
+ s.summary = "Class for interaction with the Yahoo BOSS web service."
6
+ s.email = "jpignata@waterfrontmedia.com"
7
+ s.homepage = "http://github.com/jpignata/bossman-gem"
8
+ s.authors = ["Jay Pignata"]
9
+ s.files = ['bossman.gemspec', 'lib/bossman.rb', 'lib/bossman/basevalueobject.rb',
10
+ 'lib/bossman/rest.rb', 'lib/bossman/result.rb', 'lib/bossman/resultset.rb',
11
+ 'lib/bossman/search.rb', 'README', 'LICENSE']
12
+ s.add_dependency("activesupport", [">= 2.1.0"])
13
+ s.has_rdoc = false
14
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+
4
+ Dir["#{File.dirname(__FILE__)}/bossman/*.rb"].sort.each { |bossman_lib| require bossman_lib }
5
+
6
+ module BOSSMan
7
+ API_VERSION = :v1
8
+ API_BASEURI = "http://boss.yahooapis.com/ysearch"
9
+
10
+ %w( application_id proxy_host proxy_port proxy_user proxy_pass ).each do |att|
11
+ attr_accessor att.to_sym
12
+ module_function att.to_sym, "#{att}=".to_sym
13
+ end
14
+
15
+ class MissingConfiguration < StandardError; end
16
+ class InvalidParameter < StandardError; end
17
+ class BOSSError < StandardError; end
18
+ end
@@ -0,0 +1,19 @@
1
+ module BOSSMan
2
+ class BaseValueObject
3
+
4
+ def set_parameter(key, value)
5
+ value = value.gsub(/\\u(\d+)/, '&#x\1;') if value.kind_of? String
6
+ instance_variable_set("@#{key}", value)
7
+ instance_eval("def #{key}; @#{key}; end")
8
+ end
9
+
10
+ def to_yaml
11
+ @response.to_yaml
12
+ end
13
+
14
+ def to_json
15
+ ActiveSupport::JSON.encode(@response)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module BOSSMan
5
+ class REST
6
+
7
+ def self.get(method, query, options)
8
+ validate_parameters(options)
9
+ uri = URI.parse(URI.encode("#{API_BASEURI}/#{method}/#{API_VERSION}/#{query}"))
10
+ uri.query = options.to_query
11
+ request = Net::HTTP::Get.new(uri.request_uri)
12
+ response = Net::HTTP.new(uri.host).request(request)
13
+
14
+ case response
15
+ when Net::HTTPSuccess
16
+ return ResultSet.new(ActiveSupport::JSON.decode(response.body))
17
+ else
18
+ raise BOSSError, parse_error(response.body)
19
+ end
20
+ end
21
+
22
+ def self.parse_error(response_string)
23
+ response_document = REXML::Document.new(response_string)
24
+ error_code = REXML::XPath.first(response_document, '//yahoo:code').text
25
+ error_description = REXML::XPath.first(response_document, '//yahoo:description').text
26
+ error_detail = REXML::XPath.first(response_document, '//yahoo:detail').text
27
+ return "#{error_code} #{error_description}: #{error_detail}"
28
+ end
29
+
30
+ def self.validate_parameters(options)
31
+ unless BOSSMan.application_id
32
+ raise MissingConfiguration, "Application ID must be set prior to making a service call."
33
+ end
34
+
35
+ unless options[:count] > 0
36
+ raise InvalidParameter, "Invalid count. Count must be > 0."
37
+ end
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ module BOSSMan
2
+ class Result < BaseValueObject
3
+
4
+ def initialize(response)
5
+ @response = response
6
+ response.each { |key, value| set_parameter(key, value) }
7
+ end
8
+
9
+ def to_xml
10
+ @response.to_xml(:root => 'result')
11
+ end
12
+
13
+ alias to_s to_xml
14
+ end
15
+ end
@@ -0,0 +1,42 @@
1
+ module BOSSMan
2
+ class ResultSet < BaseValueObject
3
+
4
+ def initialize(response)
5
+ @response = response
6
+
7
+ @response["ysearchresponse"].each do |key, value|
8
+ if key.include? "resultset_spell"
9
+ set_parameter("suggestion", @response["ysearchresponse"]["resultset_spell"][0]["suggestion"])
10
+ elsif key.include? "resultset"
11
+ results = Array.new
12
+ response["ysearchresponse"][key].each { |result|
13
+ begin
14
+ result = Result.new(result)
15
+ rescue
16
+ next
17
+ else
18
+ results << result
19
+ end
20
+ }
21
+ set_parameter("results", results)
22
+ else
23
+ set_parameter(key, value)
24
+ end
25
+ end
26
+ end
27
+
28
+ def to_xml
29
+ @response['ysearchresponse'].to_xml(:root => 'resultset')
30
+ end
31
+
32
+ def _dump(level)
33
+ @response.to_json
34
+ end
35
+
36
+ def self._load(string)
37
+ ResultSet.new(ActiveSupport::JSON.decode(string))
38
+ end
39
+
40
+ alias to_s to_xml
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ module BOSSMan
2
+ class Search
3
+ class << self
4
+ DEFAULT_COUNT = 10
5
+ DEFAULT_START = 0
6
+
7
+ def web(query, options = {})
8
+ method = "web"
9
+ options.merge!(return_options(options))
10
+ return REST.get(method, query, options)
11
+ end
12
+
13
+ def images(query, options = {})
14
+ method = "images"
15
+ options.merge!(return_options(options))
16
+ return REST.get(method, query, options)
17
+ end
18
+
19
+ def news(query, options = {})
20
+ method = "news"
21
+ options.merge!(return_options(options))
22
+ return REST.get(method, query, options)
23
+ end
24
+
25
+ def spelling(query, options = {})
26
+ method = "spelling"
27
+ options.merge!(return_options(options))
28
+ return REST.get(method, query, options)
29
+ end
30
+
31
+ private
32
+ def return_options(options)
33
+ count = options[:count] ? options[:count] : DEFAULT_COUNT
34
+ start = options[:start] ? options[:start] : DEFAULT_START
35
+ return {:appid => BOSSMan.application_id, :count => count, :start => start }
36
+ end
37
+
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pablobm-bossman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Jay Pignata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-28 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.0
23
+ version:
24
+ description:
25
+ email: jpignata@waterfrontmedia.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - bossman.gemspec
34
+ - lib/bossman.rb
35
+ - lib/bossman/basevalueobject.rb
36
+ - lib/bossman/rest.rb
37
+ - lib/bossman/result.rb
38
+ - lib/bossman/resultset.rb
39
+ - lib/bossman/search.rb
40
+ - README
41
+ - LICENSE
42
+ has_rdoc: false
43
+ homepage: http://github.com/jpignata/bossman-gem
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: Class for interaction with the Yahoo BOSS web service.
68
+ test_files: []
69
+