Class: NationalParks::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



3
4
5
6
# File 'lib/national_parks/cli.rb', line 3

def initialize
  welcome
  NationalParks::Scraper.scrape_find_park_page
end

Instance Method Details

#callObject



8
9
10
11
12
13
# File 'lib/national_parks/cli.rb', line 8

def call
  list_states
  state_selection
  list_parks
  menu
end

#list_parksObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/national_parks/cli.rb', line 51

def list_parks
  NationalParks::Scraper.scrape_state_page(@state) if @state.parks.empty? # conditional modifier to prevent redundant scraping and instantiation of state's park objects if that state was previously selected by the user
  system("clear")
  puts "\nNational Parks in #{@state.name}:"
  @state.parks.each.with_index(1) do |park, index|
    sleep(0.25)
    puts "\n----------------------------------------------".colorize(:green)
    puts "#{index}. #{park.name}".colorize(:blue)
    puts ""
    puts "Type:         #{park.type}" if park.type
    puts "Location:     #{park.location}" if park.location
    puts "Description:  #{park.description}"
  end
end

#list_statesObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/national_parks/cli.rb', line 25

def list_states
  # states/territories displayed in table format with 2 columns
  all_rows = []
  row = []
  NationalParks::State.all.each.with_index(1) do |state, index|
    row = [] if index.odd?
    row << "#{index}. #{state.name}"
    all_rows << row if index.even? || index == NationalParks::State.all.length
  end
  table = Terminal::Table.new :title => "U.S. States and Territories", :rows => all_rows
  puts "\n#{table}"
end


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/national_parks/cli.rb', line 66

def menu
  puts "\nTo see more information in your web browser for a national park in #{@state.name}, enter the " + "number".underline + " of the park.\nType " + "list".underline +  " to see the states and territories again or type " + "exit".underline + "."
  input = gets.strip
  if input.to_i.between?(1, @state.parks.length)
    system("open #{@state.find_park(input).more_info_url}")
    menu
  elsif input.downcase == "list"
    system("clear")
    call
  elsif input.downcase == "exit"
    puts "\nGet out and enjoy the national parks! Goodbye!\n".colorize(:green)
  else
    puts "\nInvalid entry.".colorize(:red)
    menu
  end
end

#state_selectionObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/national_parks/cli.rb', line 38

def state_selection
  puts "\nSelect a state or territory by " + "number".underline + " or " + "name".underline + ":"
  state_input = gets.strip
  if state_input.to_i.between?(1, NationalParks::State.all.length)
    @state = NationalParks::State.find_state(state_input)
  elsif NationalParks::State.find_state_by_name(state_input)
    @state = NationalParks::State.find_state_by_name(state_input)
  else
    puts "Invalid entry.".colorize(:red)
    state_selection
  end
end

#welcomeObject



15
16
17
18
19
20
21
22
23
# File 'lib/national_parks/cli.rb', line 15

def welcome
  system("clear")
  puts " ----------------------------------------"
  puts "|                                         |"
  puts "|        Welcome to National Parks        |"
  puts "|                                         |"
  puts " ----------------------------------------"
  puts "\nFind information about the national parks located in any of the U.S. states or territories"
end