Class: CognitiveBingNews
- Inherits:
-
Object
- Object
- CognitiveBingNews
- Defined in:
- lib/cognitivebing.rb
Instance Attribute Summary collapse
-
#account_key ⇒ Object
Returns the value of attribute account_key.
Instance Method Summary collapse
- #category(category_term, params = {}) ⇒ Object
-
#initialize(account_key, params = {}) ⇒ CognitiveBingNews
constructor
A new instance of CognitiveBingNews.
- #search(search_term) ⇒ Object
- #trending ⇒ Object
Constructor Details
#initialize(account_key, params = {}) ⇒ CognitiveBingNews
85 86 87 88 |
# File 'lib/cognitivebing.rb', line 85 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.
83 84 85 |
# File 'lib/cognitivebing.rb', line 83 def account_key @account_key end |
Instance Method Details
#category(category_term, params = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cognitivebing.rb', line 125 def category(category_term, params = {}) web_search_url = URI('https://api.cognitive.microsoft.com/bing/v5.0/news/') web_search_url.query = URI.encode_www_form({ # Request parameters 'category' => '#{category_term}' }) uri = URI(web_search_url) 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 |
#search(search_term) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cognitivebing.rb', line 90 def search(search_term) query_string = '?q=' query_portion = URI.encode_www_form_component('\'' + search_term + '\'') paramsbuilder = "&Ocp-Apim-Subscription-Key=#{@account_key}" @params.each do |k,v| paramsbuilder << "&#{k.to_s}=#{v.to_s}" end web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/news/search" full_address = web_search_url + query_string + query_portion + paramsbuilder puts full_address 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 |
#trending ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/cognitivebing.rb', line 151 def trending web_search_url = "https://api.cognitive.microsoft.com/bing/v5.0/news/trendingtopics" uri = URI(web_search_url) 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 |