Class: Headlines

Inherits:
Object
  • Object
show all
Defined in:
lib/times-terminal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeadlines

Returns a new instance of Headlines.



8
9
10
11
# File 'lib/times-terminal.rb', line 8

def initialize
	@doc = Nokogiri::HTML(open("http://www.nytimes.com/pages/todayspaper/index.html"))
	@list = @doc.css("div.first h3 a")
end

Instance Attribute Details

#docObject

Returns the value of attribute doc.



6
7
8
# File 'lib/times-terminal.rb', line 6

def doc
  @doc
end

#listObject

Returns the value of attribute list.



6
7
8
# File 'lib/times-terminal.rb', line 6

def list
  @list
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
# File 'lib/times-terminal.rb', line 24

def call
	puts "TODAY'S NEW YORK TIMES HEADLINES"
	headlines
	puts "\nWould you like to open a story? (Enter index)"
	i = gets.strip
	open_story(i)
end

#headlinesObject



13
14
15
# File 'lib/times-terminal.rb', line 13

def headlines
	@list.each_with_index { |story, i| puts "#{i+1}. #{story.text.strip}" }
end

#open_story(index) ⇒ Object



17
18
19
20
21
22
# File 'lib/times-terminal.rb', line 17

def open_story(index)
	if index.to_i > 0 && index.to_i <= list.length
		link = list[index.to_i-1].attribute("href").value
		system "open #{link}"
	end
end