Class: GetNews::Main

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

Instance Method Summary collapse

Instance Method Details

#get_news(search_word, news_count) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/get_news.rb', line 8

def get_news(search_word, news_count)
  if news_count < 1
    return
  end
  
  url = URI.escape('https://news.google.com/news?ned=us&ie=UTF-8&oe=UTF-8&q=' + search_word + '&output=atom&num=' + news_count.to_s + '&hl=ja')

  charset = nil
  xml = open(url) do |f|
    charset = f.charset
    f.read
  end

  doc = Nokogiri::XML(xml) do |config|
    config.strict.nonet
  end

  title_array = []
  doc.xpath('//xmlns:title').each_with_index do |val, index|
    if index != 0
      title_array.push(val.text)
    end
  end

  title_array
end