Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bing Search API in Ruby

I read the "Bing Search API - Quick Start" but I don't know how to make this http request in Ruby (Weary)

How to translate "Stream_context_create()" in Ruby? And What does it mean?

"Bing Search API - Quick Start" enter image description here

I would want to use a Ruby sdk but those I found are deprecated ex (Rbing) https://github.com/mikedemers/rbing Do you know a up-to-date Wrapper for Bing Search API (Web only results)?

like image 790
sparkle Avatar asked Dec 01 '12 14:12

sparkle


3 Answers

Okay, after an hour of frustration I figured out a way to do it. This code is awful because it's the first version I got working. Basically, ignore everything about the base64 encode because it was giving me an error that only oAuth and basic authentication was supported. Turns out Microsoft's documentation was wrong and you're supposed to just use your account key as the password in the request instead of the encoded string.

require 'net/http'

accountKey = 'KEY'

url = 'https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query=%27xbox%27&$top=50&$format=json'

uri = URI(url)

req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth '', accountKey

res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https'){|http|
  http.request(req)
}
puts res.body
like image 148
Chris Bui Avatar answered Oct 05 '22 15:10

Chris Bui


Try the bing-search gem:

require 'bing-search'

BingSearch.account_key = <your key>
BingSearch.web_only = true
results = BingSearch.web('stack overflow')

Documentation is here and source is on GitHub. (Disclaimer: I wrote the bing-search gem.)

like image 39
jonahb Avatar answered Oct 05 '22 16:10

jonahb


Wow, microsoft docs eh, something so simple and I've spent 30 minutes trawling the net to find out how to use it. Anyway, here's another take on Chris Bui's answer, using RestClient:

class BingSearch
    def self.for(account_key, query)
        puts RestClient.get("https://:#{account_key}@api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query='#{CGI::escape(query)}'&$format=json")
    end
end
like image 24
opsb Avatar answered Oct 05 '22 15:10

opsb