Class: CognitiveBing
- Inherits:
-
Object
- Object
- CognitiveBing
- Defined in:
- lib/cognitivebing.rb
Defined Under Namespace
Classes: BingUnavailableException
Instance Attribute Summary collapse
-
#account_key ⇒ Object
Returns the value of attribute account_key.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
-
#initialize(account_key, params = {}) ⇒ CognitiveBing
constructor
A new instance of CognitiveBing.
- #search(search_term, type = 'web') ⇒ Object
- #suggestions(search_term) ⇒ Object
Constructor Details
#initialize(account_key, params = {}) ⇒ CognitiveBing
Returns a new instance of CognitiveBing.
12 13 14 15 |
# File 'lib/cognitivebing.rb', line 12 def initialize(account_key, params = {}) @account_key = account_key @params = params end |
Instance Attribute Details
#account_key ⇒ Object
Returns the value of attribute account_key.
10 11 12 |
# File 'lib/cognitivebing.rb', line 10 def account_key @account_key end |
#params ⇒ Object
Returns the value of attribute params.
10 11 12 |
# File 'lib/cognitivebing.rb', line 10 def params @params end |
Instance Method Details
#search(search_term, type = 'web') ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cognitivebing.rb', line 18 def search(search_term, type = 'web') query_string = '?q=' query_portion = URI.encode_www_form_component(search_term) params = "" @params.each do |k, v| params << "&#{k.to_s}=#{v.to_s}" end web_search_url = "" if type == "videos" web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/videos/search" elsif type == "image" web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/images/search" else web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/search" end full_address = web_search_url + query_string + query_portion + params uri = URI(full_address) req = Net::HTTP::Get.new(uri.request_uri) req.add_field("Ocp-Apim-Subscription-Key", @account_key) res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http| http.request(req) } body = JSON.parse(res.body, :symbolize_names => true) rescue => e if e..include?("Bing services aren't available right now") raise(BingUnavailableException) else raise(e) end end |
#suggestions(search_term) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cognitivebing.rb', line 60 def suggestions(search_term) query_string = '?q=' query_portion = URI.encode_www_form_component(search_term) web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/suggestions" full_address = web_search_url + query_string + query_portion uri = URI(full_address) req = Net::HTTP::Get.new(uri.request_uri) req.add_field("Ocp-Apim-Subscription-Key", @account_key) res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') { |http| http.request(req) } body = JSON.parse(res.body, :symbolize_names => true) return body end |