Skip to content

Commit 5cac746

Browse files
committed
adding Bing Image Search ruby sample
1 parent e747460 commit 5cac746

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

ruby/Search/BingImageSearchv7.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
ruby
2+
require 'net/https'
3+
require 'uri'
4+
require 'json'
5+
6+
# **********************************************
7+
# *** Update or verify the following values. ***
8+
# **********************************************
9+
10+
# Replace the accessKey string value with your valid access key.
11+
accessKey = "enter key here"
12+
13+
# Verify the endpoint URI. At this writing, only one endpoint is used for Bing
14+
# search APIs. In the future, regional endpoints may be available. If you
15+
# encounter unexpected authorization errors, double-check this value against
16+
# the endpoint for your Bing Search instance in your Azure dashboard.
17+
18+
uri = "https://api.cognitive.microsoft.com"
19+
path = "/bing/v7.0/images/search"
20+
21+
term = "puppies"
22+
23+
if accessKey.length != 32 then
24+
puts "Invalid Bing Search API subscription key!"
25+
puts "Please paste yours into the source code."
26+
abort
27+
end
28+
29+
uri = URI(uri + path + "?q=" + URI.escape(term))
30+
31+
puts "Searching images for: " + term
32+
33+
request = Net::HTTP::Get.new(uri)
34+
request['Ocp-Apim-Subscription-Key'] = accessKey
35+
36+
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
37+
http.request(request)
38+
end
39+
40+
puts "\nRelevant Headers:\n\n"
41+
response.each_header do |key, value|
42+
# header names are coerced to lowercase
43+
if key.start_with?("bingapis-") or key.start_with?("x-msedge-") then
44+
puts key + ": " + value
45+
end
46+
end
47+
48+
puts "\nJSON Response:\n\n"
49+
puts JSON::pretty_generate(JSON(response.body))

0 commit comments

Comments
 (0)