Long time ago fixes.. #5

Merged
merged 4 commits into from Nov 13, 2011
View
@@ -1,5 +1,14 @@
# BOSSMan -- Gem for interaction with the Yahoo BOSS web service
+## Fork info
+
+This is a fork of the original BOSSMan gem. The form enables SearchMonkey usage. To make this work it parses
+the XML response coming from Yahoo BOSS (since the JSON response doesn't include the SearchMonkey RDF).
+
+You can now do something like:
+
+ boss = BOSSMan::Search.web("yelp restaurant", :count => 5, :filter => "-hate", :view => "searchmonkey_rdf", :format => "xml")
+
## Description
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/.
View
@@ -6,17 +6,18 @@ task :default => [:spec]
spec = Gem::Specification.new do |s|
s.name = "bossman"
- s.version = "0.4.1"
- s.date = "2009-11-01"
+ s.version = "0.4.2"
+ s.date = "2010-02-15"
s.summary = "Gem for interaction with the Yahoo BOSS web service."
s.email = "john.pignata@gmail.com"
s.homepage = "http://github.com/jpignata/bossman-gem"
s.authors = ["John Pignata"]
s.files = ['lib/bossman.rb', 'lib/bossman/base_value_object.rb',
'lib/bossman/boss.rb', 'lib/bossman/result.rb', 'lib/bossman/result_set.rb',
- 'lib/bossman/search.rb', 'README.md', 'LICENSE', 'lib/extensions/http_success.rb']
+ 'lib/bossman/search.rb', 'README.md', 'LICENSE', 'lib/extensions/net/http_success.rb']
s.has_rdoc = false
s.add_dependency("activesupport", [">= 2.1.0"])
+ s.add_dependency("xml-simple", [">= 1.0.12"])
end
Spec::Rake::SpecTask.new(:spec) do |t|
View
@@ -1,5 +1,9 @@
$: << File.join(File.dirname(__FILE__))
+# Added to parse XML
+# require 'active_resource'
+require 'xmlsimple'
+
require 'active_support'
require 'net/http'
require 'uri'
View
@@ -24,7 +24,11 @@ def query_api
def parse_response
case @response
when Net::HTTPSuccess
- ResultSet.new(ActiveSupport::JSON.decode(@response.body))
+ if xml_format?
+ ResultSet.new(XmlSimple.xml_in(@response.body, { 'ForceArray' => false }))
+ else
+ ResultSet.new(ActiveSupport::JSON.decode(@response.body))
+ end
else
raise BOSSError, "Error occurred while querying API: #{@response.body}"
end
@@ -36,6 +40,10 @@ def validate_parameters
end
@options[:count] = 10 unless @options.include?(:count) && @options[:count] > 0
- end
+ end
+
+ def xml_format?
+ @options[:format] =~ /^xml$/i
+ end
end
end
@@ -3,7 +3,7 @@ class ResultSet < BaseValueObject
def initialize(response)
@response = response
- @ysearchresponse = response["ysearchresponse"]
+ @ysearchresponse = response["ysearchresponse"] || response # JSON uses ysearchresponse, XML not
process_response
end
@@ -41,8 +41,20 @@ def process_spelling_result
end
def process_resultset(key)
- results = @ysearchresponse[key].map { |result| Result.new(result) }
- set_parameter("results", results)
+ if @ysearchresponse[key].is_a?(Array)
+ # JSON
+ resultset = @ysearchresponse[key]
+ results = resultset.map { |result| Result.new(result) }
+ set_parameter("results", results)
+ else
+ resultset = @ysearchresponse[key]['result'] # XML puts them inside 'result'
+ results = resultset.map { |result| Result.new(result) }
+ set_parameter("results", results)
+ set_parameter("count", @ysearchresponse[key]['count'])
+ set_parameter("deephits", @ysearchresponse[key]['deephits'])
+ set_parameter("totalhits", @ysearchresponse[key]['totalhits'])
+ set_parameter("start", @ysearchresponse[key]['start'])
+ end
end
end
end
View
@@ -50,6 +50,46 @@
ActiveSupport::JSON.decode(@search.to_json)["ysearchresponse"].should be_an_instance_of(Hash)
end
end
+
+ context "Search results in XML" do
+ before(:all) do
+ include BOSSMan
+ set_boss_api_key
+ @search = boss_search("web", "yelp", :view => "searchmonkey_rdf", :format => 'xml', :count => 5, :start => 0)
+ @result = @search.results.first
+ end
+
+ it "contains the HTTP response code" do
+ @search.responsecode.should == "200"
+ end
+
+ it "contains the count of results returned in the search" do
+ @search.count.should == @search.results.length.to_s
+ end
+
+ it "contains SearchMonkey RDF for second result" do
+ @search.results[1].searchmonkey_rdf.should_not == {}
+ @search.results[1].searchmonkey_rdf.should_not == "Search monkey data not supported in JSON"
+ end
+
+ it "contains no SearchMonkey RDF for first result" do
+ @search.results[0].searchmonkey_rdf.should == {}
+ end
+
+ it "contains the number of total hits returned in the search" do
+ @search.totalhits.should == "19868"
+ @search.deephits.should == "178000"
+ end
+
+ it "contains the number of the first search result requests" do
+ @search.start.should == "0"
+ end
+
+ it "contains the URL to the next page of search results" do
+ @search.nextpage.should == "/ysearch/web/v1/restaurant%20cinnamon%20yelp?format=xml&filter=-hate&count=5&appid=#{BOSSMan.application_id}&view=searchmonkey_rdf&start=5"
+ end
+
+ end
context "Spelling suggestion search" do
before(:all) do
View
@@ -1,5 +1,6 @@
$: << File.join(File.dirname(__FILE__), "/../lib")
+require 'rubygems'
require 'uri'
require 'spec'
require 'fakeweb'
@@ -18,15 +19,18 @@ def set_boss_api_key
def register_fakeweb(method, query, options = {})
uri = boss_url(method, query, options)
+ puts "Regging: #{uri}"
FakeWeb.register_uri(:any, uri, :body => fakeweb_file(method, query))
end
def boss_url(method, query, options = {})
base_uri = "#{BOSSMan::API_BASEURI}/#{method}/#{BOSSMan::API_VERSION}/#{query}"
app_id = "appid=#{BOSSMan.application_id}"
- count = options.include?(:count) ? "count=#{options[:count]}" : "count=10"
- start = options.include?(:start) ? "start=#{options[:start]}" : "start=0"
- query = "#{app_id}&#{count}&#{start}"
+ count = options.include?(:count) ? "&count=#{options[:count]}" : "&count=10"
+ format = options.include?(:format) ? "&format=#{options[:format]}" : ""
+ start = options.include?(:start) ? "&start=#{options[:start]}" : "&start=0"
+ view = options.include?(:view) ? "&view=#{options[:view]}" : ""
+ query = "#{app_id}#{count}#{format}#{start}#{view}"
URI.escape("#{base_uri}?#{query}")
end
Oops, something went wrong. Retry.