Class: GovKit::SearchEngines::GoogleNews

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

Overview

Class to wrap access to Google News.

Class Method Summary collapse

Class Method Details

.make_request(host, path) ⇒ Object



42
43
44
# File 'lib/gov_kit/search_engines/google_news.rb', line 42

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

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

Fetches stories about a topic from google news. Returns an array of GovKit::Mention objects.

query: The query wanted For example: mentions = GoogleNews.search(“Nancy Pelosi”)

options: Any additional parameters to the search. eg.: :geo => ‘Texas’ will add &geo=Texas to the URL. :num => 100 will show 100 results per page.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gov_kit/search_engines/google_news.rb', line 17

def self.search(query=[], options={})
  query = Array(query).join('+')
  host = GovKit::configuration.google_news_base_url
  options[:num] ||= 50

  path = "/news?q=#{URI::encode(query)}&output=rss" + '&' + options.map { |k, v| URI::encode(k.to_s) + '=' + URI::encode(v.to_s) }.join('&')

  doc = Nokogiri::XML(make_request(host, path))

  mentions = []

  doc.xpath('//item').each do |i|
    mention = GovKit::Mention.new
    mention.title = i.xpath('title').inner_text.split(" - ").first
    mention.date = i.xpath('pubDate').inner_text
    mention.excerpt = i.xpath('description').inner_text
    mention.source = i.xpath('title').inner_text.split(" - ").last
    mention.url = i.xpath('link').inner_text

    mentions << mention
  end

  mentions
end