Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Added XML parsing quick hack. Also now returns SearchMonkey results.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joost Hietbrink committed Feb 16, 2010
1 parent 2e951c0 commit 183786c
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/bossman.rb
@@ -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'
Expand Down
12 changes: 10 additions & 2 deletions lib/bossman/boss.rb
Expand Up @@ -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
Expand All @@ -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
18 changes: 15 additions & 3 deletions lib/bossman/result_set.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
39 changes: 39 additions & 0 deletions spec/bossman_spec.rb
Expand Up @@ -50,6 +50,45 @@
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 == {}
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
Expand Down
10 changes: 7 additions & 3 deletions spec/spec_helper.rb
@@ -1,5 +1,6 @@
$: << File.join(File.dirname(__FILE__), "/../lib")

require 'rubygems'
require 'uri'
require 'spec'
require 'fakeweb'
Expand All @@ -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
Expand Down

0 comments on commit 183786c

Please sign in to comment.