Class: CognitiveBing

Inherits:
Object
  • Object
show all
Defined in:
lib/cognitivebing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_key, params = {}) ⇒ CognitiveBing



9
10
11
12
# File 'lib/cognitivebing.rb', line 9

def initialize(, params = {})
  @account_key =     
  @params = params
end

Instance Attribute Details

#account_keyObject

Returns the value of attribute account_key.



7
8
9
# File 'lib/cognitivebing.rb', line 7

def 
  @account_key
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/cognitivebing.rb', line 7

def params
  @params
end

Instance Method Details

#search(search_term, type = 'web') ⇒ Object



15
16
17
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
# File 'lib/cognitivebing.rb', line 15

def search(search_term, type = 'web')
  
  
  query_string = '?q='
  query_portion = URI.encode_www_form_component('\'' + search_term + '\'')
  params = "&Ocp-Apim-Subscription-Key=#{@account_key}"
  @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)
      
  
  return body
end

#suggestions(search_term) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cognitivebing.rb', line 53

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