Class: CLI

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



9
10
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 9

def initialize
end

Instance Attribute Details

#current_itemObject

Returns the value of attribute current_item.



7
8
9
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 7

def current_item
  @current_item
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 6

def time
  @time
end

Class Method Details

.display_timeObject



207
208
209
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 207

def self.display_time
  puts Time.new
end

Instance Method Details

#article_options_menu(article) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 166

def article_options_menu(article)
  #takes article object as an argument
  #automatically displays article headline, network name, and article metadata (i.e. author, date & time posted, number of comments, tags etc.)

  #gives the option for the user to either go to the article in browser or scrape the contents of the article
  self.select_scrape_method(article)

  puts "_____________________________________"
  puts ""
  puts article.network_name
  puts article.headline
  puts article.date
  puts ""
  puts article.summary
  puts ""
  puts "---------------"
  puts ""

  puts "What would you like to do? Enter a number."
  puts "1. View article in browser."
  puts "2. Return to previous menu."
  puts "Or type 'exit'."
  input = gets.strip.upcase
  case input
  when "1"
    Launchy.open(article.url)
  when "2"
    self.display_menu
    self.respond_to_selection(self.select_item)
  when "EXIT"
    self.exit_CLI
  else
    puts "Invalid Selection"
    self.article_options_menu(article)
  end
end

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 12

def call
  puts "Initializing..."
  Scraper.msnbc_homepage
  Scraper.fox_homepage
  Scraper.reuters_homepage
  puts("done")
  puts("")

  self.greet
  self.display_menu #initial menu selection of what you want to see
  self.respond_to_selection(self.select_item)

end

#display_menuObject



34
35
36
37
38
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 34

def display_menu
  self.class.display_time
  puts ""
  self.print_group_headlines
end

#exit_CLIObject



113
114
115
116
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 113

def exit_CLI
  puts "Goodbye!"
  exit
end

#greetObject



26
27
28
29
30
31
32
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 26

def greet
  puts "Welcome to Headline Scraper"
  sleep(1)
  puts "Please select which of the following articles you would like to view:"
  sleep(1.5)
  puts ""
end


41
42
43
44
45
46
47
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 41

def print_group_headlines
  Network.all.each do |network|
    puts network.name #prints network name once
    network.print_headlines # prints network headlines in numbered list
    puts "" #for spacing
  end
end

#respond_to_selection(selection) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 118

def respond_to_selection(selection)
    if selection.length == 1
      the_network = Network.find_by_name(selection[0])
      the_network.go_to_homepage
    elsif selection.length == 2
      the_network = Network.find_by_name(selection[0])
      the_article = the_network.articles[selection[1]-1]
      self.article_options_menu(the_article)
    end
end

#retrieve_articleObject



204
205
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 204

def retrieve_article
end

#select_itemObject

returns an array where arr is the network name and arr is the article number.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 80

def select_item #returns an array where arr[0] is the network name and arr[1] is the article number.
  #currently accepts all entries that do not contain a colon.  Later make it so it checks whether the network entered exists.
  selection = nil
  until selection_exists?(selection) || selection == 'EXIT'
    puts "To go to a network homepage, just type the name of that network."
    puts "To go to a specific story, type the network name and then the article number, separated by a colon (e.g., BBC : 2)"
    puts "To exit at any time, type 'exit'."

    selection = gets.strip
    selection = selection.split(":") if selection != nil #turns the entered data into an array so ti can be processed
    if valid_selection?(selection)
      selection[0].strip!
      selection[0] = selection[0].upcase
      if selection.length == 1
        if selection[0] == 'EXIT'
          self.exit_CLI
        end
      elsif selection.length == 2
        selection[1].strip!
        selection[1] = selection[1].to_i
      end

      if !selection_exists?(selection)
        puts "Selection not found"
      end
    else
      puts "Invalid Entry"
    end

  end
  selection
end

#select_scrape_method(article) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 153

def select_scrape_method(article)

  case article.network_name

  when "REUTERS"
    Scraper.reuters_article(article)
  when "FOX NEWS"
    Scraper.fox_article(article)
  when "MSNBC"
    Scraper.msnbc_article(article)
  end
end

#selection_exists?(selection) ⇒ Boolean

post-screens entries to make sure the valid entry actually refers to an existing item

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 129

def selection_exists?(selection) #post-screens entries to make sure the valid entry actually refers to an existing item
  if self.valid_selection?(selection)
    if selection.length == 1
      if Network.find_by_name(selection[0])
        true
      else
        false
      end
    elsif selection.length == 2
      if Network.find_by_name(selection[0])
        if selection[1] > Network.find_by_name(selection[0]).articles.length || selection[1] <= 0
          false
        else
          true
        end
      else
        false
      end
    end
  else
    false
  end
end

#valid_selection?(selection) ⇒ Boolean

pre-screens nonsensical entries. DOES NOT check whether the item entered exists

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/CLI_Headline_Scraper/CLI.rb', line 49

def valid_selection?(selection) #pre-screens nonsensical entries. DOES NOT check whether the item entered exists
  if selection == nil #
    false
  elsif selection.length == 0
    false
  elsif selection.length == 1
    if selection[0].to_i != 0 #makes furst first item isnt Integer
      false
    else
      true
    end
  elsif selection.length == 2
    if selection[0].to_i != 0 #makes sure first item isnt Integer
      false
    else
      if selection[1].to_i == 0 #makes sure second item IS integer
        false
      elsif selection[1].to_i > 3 #makes sure there are not >3 entries
        false
      else
        true
      end
    end
  elsif selection.length > 2 #makes sure entry isnt longer than 3
    false
  else
    true
  end
end