Skip to content

Commit

Permalink
big commit heres the details:
Browse files Browse the repository at this point in the history
base_uri is different
added reserved options for skip and top
made error handling info more useful
instead of converting query to a string to send to httparty, I'm now sending a has to let it deal with it.
password is no longer hard coded and will accept direct input when calling new, or read it from your .rbing_app_id file in the $HOME directory
  • Loading branch information
KellyMahan committed Aug 20, 2012
1 parent f6dd51e commit 5d64f8f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
41 changes: 25 additions & 16 deletions lib/rbing.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,16 +81,15 @@ def method_missing(*args)
include HTTParty include HTTParty


attr_accessor :instance_options attr_accessor :instance_options

base_uri "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web"
base_uri "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web"
format :json format :json


BASE_OPTIONS = [:version, :market, :adult, :query, :appid] BASE_OPTIONS = [:version, :market, :adult, :query, :appid]


# Query Keywords: <http://help.live.com/help.aspx?project=wl_searchv1&market=en-US&querytype=keyword&query=redliub&tmt=&domain=www.bing.com:80> # Query Keywords: <http://help.live.com/help.aspx?project=wl_searchv1&market=en-US&querytype=keyword&query=redliub&tmt=&domain=www.bing.com:80>
# #
QUERY_KEYWORDS = [:site, :language, :contains, :filetype, :inanchor, :inbody, :intitle, :ip, :loc, :location, :prefer, :feed, :hasfeed, :url] QUERY_KEYWORDS = [:site, :language, :contains, :filetype, :inanchor, :inbody, :intitle, :ip, :loc, :location, :prefer, :feed, :hasfeed, :url]

RESERVED_OPTIONS = [:top, :skip]
# Source Types: <http://msdn.microsoft.com/en-us/library/dd250847.aspx> # Source Types: <http://msdn.microsoft.com/en-us/library/dd250847.aspx>
# #
SOURCES = %w(Web) SOURCES = %w(Web)
Expand All @@ -112,11 +111,14 @@ def method_missing(*args)
# issues a search for +query+ in +source+ # issues a search for +query+ in +source+
# #
def search(source, query, options={}) def search(source, query, options={})
rsp = self.class.get('', options_for(source, query, options)) rsp = self.class.get("", options_for(source, query, options))
if rsp.response.is_a?(Net::HTTPOK) if rsp.response.is_a?(Net::HTTPOK)
ResponseData.new(rsp['d']) if rsp ResponseData.new(rsp['d']) if rsp
else else
raise RBing::APIError.new(rsp.response.inspect) raise RBing::APIError.new(
rsp.request.to_yaml + "\n" +
rsp.response.to_yaml
)
end end
end end


Expand All @@ -126,23 +128,25 @@ def search(source, query, options={})
# +options+ can contain values to be passed with each query. # +options+ can contain values to be passed with each query.
# #
def initialize(app_id=nil, options={}) def initialize(app_id=nil, options={})
@instance_options = options # options.merge(:AppId => (app_id || user_app_id)) @instance_options = options.merge(:AppId => (app_id || user_app_id))
end end
# constructs a query string for the given # constructs a query string for the given
# +query+ and the optional query +options+ # +query+ and the optional query +options+
# #
def build_query(query, options={}) def build_query(query, options={})
queries = [] queries = {}
queries[:Query] = "'#{query}'"

QUERY_KEYWORDS.each do |kw| QUERY_KEYWORDS.each do |kw|
next unless options[kw] next unless options[kw]
if options[kw].is_a? Array if options[kw].is_a? Array
kw_query = options[kw].map {|s| "#{kw}:#{s}".strip }.join(" OR ") kw_query = options[kw].map {|s| "#{kw}:#{s}".strip }.join(" OR ")
queries << " (#{kw_query})" queries[kw.to_sym] = "(#{kw_query})"
else else
queries << " #{kw}:#{options[kw]}" queries[kw.to_sym] = options[kw]
end end
end end
"'#{query} #{queries.join(' ')}'".strip queries
end end


# returns +options+ with its keys converted to # returns +options+ with its keys converted to
Expand All @@ -158,18 +162,23 @@ def filter_hash(options, exclude=[])
# #
def options_for(type, query, options={}) def options_for(type, query, options={})
opts = instance_options.merge(filter_hash(options, BASE_OPTIONS)) opts = instance_options.merge(filter_hash(options, BASE_OPTIONS))
opts.merge!(:Query => build_query(query, options)) opts.merge!(build_query(query, options))


source_options = filter_hash(options, [:http] + BASE_OPTIONS + QUERY_KEYWORDS) source_options = filter_hash(options, [:http] + BASE_OPTIONS + QUERY_KEYWORDS)
opts.merge!(scope_source_options(type, source_options)) opts.merge!(scope_source_options(type, source_options))

RESERVED_OPTIONS.each do |reserved_option|
opts.merge!('$format' => 'json') next unless options[reserved_option]

opts.merge!("$#{reserved_option}" => options[reserved_option])
opts.delete(reserved_option)
opts.delete('Web.' + "#{reserved_option}") # Why is this needed? What is causing it to set this?
end
opts.merge!('$format' => 'JSON')

authentication_options = {:basic_auth => { authentication_options = {:basic_auth => {
:username => '', :username => '',
:password => "/yjW8ZAu5eW2+JTS0RbYvZk4V2lOivQVBK360EP3kk0=" :password => "#{@instance_options[:AppId]}"
}} }}

opts.delete(:AppId)
http_options = options[:http] || {} http_options = options[:http] || {}
http_options.merge!(authentication_options) http_options.merge!(authentication_options)
http_options.merge(:query => opts) http_options.merge(:query => opts)
Expand Down
8 changes: 6 additions & 2 deletions spec/rbing_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
@bing = RBing.new @bing = RBing.new
end end


it "should format a query properly with a hash" do
@bing.send(:build_query, "ruby").should eq({:Query=>"'ruby'"})
end

it "should provide a web search method" do it "should provide a web search method" do
@bing.should respond_to(:web) @bing.should respond_to(:web)
end end


it "should return a ResponseData object" do it "should return a ResponseData object" do
@bing.web("ruby").should be_a(RBing::ResponseData) @bing.web("ruby", top: 10, skip: 10).should be_a(RBing::ResponseData)
end end


it "should return search results" do it "should return search results" do
@bing.web("ruby").results.should_not be_empty @bing.web("ruby", top: 10, skip: 10).results.should_not be_empty
end end
end end

0 comments on commit 5d64f8f

Please sign in to comment.