Class: NPRCLI

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

Instance Method Summary collapse

Instance Method Details

#choose_countryObject



21
22
23
24
25
26
27
28
29
# File 'lib/npr_cli.rb', line 21

def choose_country
  cmd = "none"
  until valid_iso_format?(cmd) || cmd == "exit"
    puts "Choose a country you want more information on by entering the corresponding two-letter ISO-code."
    puts "For example, enter FI for Finland, or enter exit to quit the program:"
    cmd = gets.chomp
  end
  cmd.upcase
end

#choose_reactorObject



31
32
33
34
35
36
37
38
# File 'lib/npr_cli.rb', line 31

def choose_reactor
  cmd = "none"
  until valid_reactor_id?(cmd) || cmd == "exit"
    puts "Choose a reactor you want more information on by entering the corresponding id number (number in parenthesis), or enter exit to quit the program:"
    cmd = gets.chomp
  end
  cmd
end

#get_inputObject



12
13
14
15
# File 'lib/npr_cli.rb', line 12

def get_input
  puts "Enter a command:"
  input = gets.chomp
end

#goodbyeObject



17
18
19
# File 'lib/npr_cli.rb', line 17

def goodbye
  puts "Thank you for using Nuclear Power Reactors CLI! Have an energetic day! :D"
end

#greetObject



5
6
7
8
9
10
# File 'lib/npr_cli.rb', line 5

def greet
  puts "Welcome to Nuclear Power Reactors Command Line!"
  puts "This gem allows you to browse data on nuclear power reactors in multiple countries."
  puts "The data presented is scraped from IAEA's Power Reactor Information System site at https://www.iaea.org/PRIS/home.aspx"
  puts "Data is currently available for the following countries:"
end

#list_optionsObject



40
41
42
43
# File 'lib/npr_cli.rb', line 40

def list_options
  puts "Available commandline options: 'help' for this info, 'list' to list all countries, 'country' to choose another country, 'reactor' to choose another reactor, or 'exit' to exit the program."
  get_input
end

#runObject

run sequence here



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/npr_cli.rb', line 54

def run
  greet
  npr = NuclearPowerReactors.new
  npr.list_all_countries
  cmd = "country"
  until cmd == "exit" do
    case cmd
    when "country"
      cmd = choose_country
      if cmd == "EXIT"
        goodbye
        exit
      end
      if npr.country_exists?(cmd)
        npr.find_country(cmd).nil? ? npr.create_country(cmd) : npr.find_country(cmd)
        npr.list_country_data(cmd)
        cmd = "reactor"
      else
        puts "No country with code = #{cmd} exists."
        cmd = "list"
      end
    when "reactor"
      cmd = choose_reactor
      if cmd == "exit"
        goodbye
        exit
      end
      if npr.find_reactor(cmd).nil? && npr.reactor_exists?(cmd)
        puts "Reactor #{cmd} not located in this country. Find and display data anyway? (y/n)"
        if get_input.downcase == "y"
          npr.create_reactor(cmd)
          npr.show_reactor_details(cmd)
        end
      elsif npr.find_reactor(cmd).nil? && !npr.reactor_exists?(cmd)
        puts "No reactor with id = #{cmd} exists."
      else
        npr.show_reactor_details(cmd)
      end
      cmd = list_options
    when "list"
      npr.list_all_countries
      cmd = "country"
    when "exit"
      exit
    else
      cmd = list_options
    end
  end
  goodbye
end

#valid_iso_format?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/npr_cli.rb', line 45

def valid_iso_format?(cmd)
  !cmd.match(/\b\w{2}\b/).nil? && cmd.match(/\d+/).nil?
end

#valid_reactor_id?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/npr_cli.rb', line 49

def valid_reactor_id?(cmd)
  cmd.match(/\D/).nil?  #this input should only consist of digits
end