Class: NytimesCli::CLI

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

Overview

cli controller

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#index(num) ⇒ Object

Returns the value of attribute index.



5
6
7
# File 'lib/nytimes_cli/cli.rb', line 5

def index
  @index
end

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nytimes_cli/cli.rb', line 7

def call
  puts ""
  puts ""
  puts "Welcome to Nytimes CLI"
  puts "All articles © The New York Times"
  puts "http://www.nytimes.com"
  puts ""
  puts ""
  front
  puts ""
  puts ""
  puts "There are currently " + num_articles + " articles on the front page of nytimes.com."
  puts ""
  num = get_articles.length
  index(num)
  interface_logic

end

#frontObject



43
44
45
# File 'lib/nytimes_cli/cli.rb', line 43

def front
  print_articles(0, 9)
end

#get_article(url) ⇒ Object



76
77
78
79
# File 'lib/nytimes_cli/cli.rb', line 76

def get_article(url)
  hash = Scraper.scrape_article(url)
  article = Article.create_article_from_hash(hash)
end

#get_article_url(num) ⇒ Object



70
71
72
73
74
# File 'lib/nytimes_cli/cli.rb', line 70

def get_article_url(num)
  i = input_to_index(num)
  a = get_articles[i]
  a.url
end

#get_articlesObject



66
67
68
# File 'lib/nytimes_cli/cli.rb', line 66

def get_articles
  Article.all
end

#get_articles_from_front_pageObject



29
30
31
32
33
# File 'lib/nytimes_cli/cli.rb', line 29

def get_articles_from_front_page
  array = Scraper.scrape_front_page
  articles = Article.create_articles_from_array(array)
  articles
end

#input_to_index(num) ⇒ Object



39
40
41
# File 'lib/nytimes_cli/cli.rb', line 39

def input_to_index(num)
  num.to_i - 1
end

#interface_logicObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/nytimes_cli/cli.rb', line 126

def interface_logic
   while true
    puts "Enter the number you would like to read or 'man' to see a list of commands."
    answer = gets.chomp
    if answer.upcase == "EXIT"
      puts ""
      puts "Ciao. Auf Wiedersehen. Goodbye."
      puts ""
      puts ""
      exit
    elsif answer.to_i.class == Fixnum && answer.to_i != 0
      print_article(answer)
    elsif answer.upcase == "MAN"
      show_manual
    elsif answer.upcase == "MORE"
      more
    elsif answer.upcase == "LESS"
      less
    elsif answer.upcase == "FRONT"
      print_articles(0, 9)
    else
      puts ""
      puts ""
      puts "That was not a valid entry. Please try again."
      puts ""
      puts ""
    end
       
  end

end

#lessObject



121
122
123
124
# File 'lib/nytimes_cli/cli.rb', line 121

def less
  @index.less
  print_articles(@index.start_index, @index.end_index)
end

#moreObject



116
117
118
119
# File 'lib/nytimes_cli/cli.rb', line 116

def more
  @index.more
  print_articles(@index.start_index, @index.end_index)
end

#num_articlesObject



35
36
37
# File 'lib/nytimes_cli/cli.rb', line 35

def num_articles
  get_articles.length.to_s
end

#print_article(num) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nytimes_cli/cli.rb', line 82

def print_article(num)
  url = get_article_url(num)
  a = get_article(url)
  puts ""
  puts ""
  puts a.title
  puts ""
  puts a.author
  puts ""
  puts a.story
  puts ""
  puts ""
  puts "Link to nytimes.com: " + a.url
  puts ""
  puts ""

end

TODO



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nytimes_cli/cli.rb', line 49

def print_articles(first_index, last_index )
  array = get_articles_from_front_page
  a = array[first_index..last_index]
  a.each do |article|
    all_articles = get_articles
    i = all_articles.index(article) + 1
    puts  i.to_s + ". " + article.title
  end
  puts ""
  puts ""
 
end

#show_manualObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/nytimes_cli/cli.rb', line 100

def show_manual

  puts "man   ->  displays manual"
  puts "front ->  shows first ten headlines"
  puts "more  ->  shows 10 more headlines"
  puts "less  ->  shows previous 10 headlines"
  puts "exit  ->  terminates program"
  puts ""
  puts ""
  puts "All articles © The New York Times"
  puts "http://www.nytimes.com"
  puts ""
  puts ""

end