Class: GovKit::SearchEngines::Bing

Inherits:
Object
  • Object
show all
Defined in:
lib/gov_kit/search_engines/bing.rb

Class Method Summary collapse

Class Method Details

.make_request(host, path) ⇒ Object



33
34
35
# File 'lib/gov_kit/search_engines/bing.rb', line 33

def self.make_request(host, path)
  Net::HTTP.get(host, path)
end

.search(query = [], options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gov_kit/search_engines/bing.rb', line 4

def self.search(query=[], options={})
  host = GovKit::configuration.bing_base_url
  query = [query, options[:geo]].compact.join('+')

  options['Sources'] ||= 'news'

  path = "/json.aspx?Query=#{URI::encode(query)}&AppId=#{GovKit::configuration.bing_appid}&Sources=#{options['Sources']}"

  doc = JSON.parse(make_request(host, path))

  mentions = []

  if news_items = doc['SearchResponse']['News']
    puts "#{news_items['Results'].size} from Bing"
    news_items['Results'].each do |i|
      mention = GovKit::Mention.new
      mention.title = i['Title']
      mention.search_source = 'Bing'
      mention.date = DateTime.parse(i['Date'])
      mention.excerpt = i['Snippet']
      mention.source = i['Source']
      mention.url = i['Url']

      mentions << mention
    end
  end
  mentions
end