class CognitiveBingNews
Attributes
account_key[RW]
Public Class Methods
new(account_key, params = {})
click to toggle source
# File lib/cognitivebing.rb, line 92 def initialize(account_key, params = {}) @account_key = account_key @params = params end
Public Instance Methods
category(category_term, params = {})
click to toggle source
# File lib/cognitivebing.rb, line 131 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.to_s }) 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)
click to toggle source
# File lib/cognitivebing.rb, line 97 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 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()
click to toggle source
# File lib/cognitivebing.rb, line 156 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