Class: NuclearPowerReactors

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNuclearPowerReactors

Returns a new instance of NuclearPowerReactors.



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

def initialize
  @npr = NPRScraper.new
  @country_hash = @npr.scrape_available_countries
  @reactor_hash = @npr.scrape_available_reactors
end

Instance Attribute Details

#country_hashObject

country and reactor key hashes will link the country and reactor names together with their ids that are used to pull out the right page



10
11
12
# File 'lib/nuclear_power_reactors.rb', line 10

def country_hash
  @country_hash
end

#nprObject

country and reactor key hashes will link the country and reactor names together with their ids that are used to pull out the right page



10
11
12
# File 'lib/nuclear_power_reactors.rb', line 10

def npr
  @npr
end

#reactor_hashObject

country and reactor key hashes will link the country and reactor names together with their ids that are used to pull out the right page



10
11
12
# File 'lib/nuclear_power_reactors.rb', line 10

def reactor_hash
  @reactor_hash
end

Instance Method Details

#country_exists?(country_iso) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/nuclear_power_reactors.rb', line 70

def country_exists?(country_iso)
  @country_hash.has_key?(country_iso)
end

#create_country(country_iso) ⇒ Object

input is country iso code



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
# File 'lib/nuclear_power_reactors.rb', line 82

def create_country(country_iso)  #input is country iso code
  country_data = @npr.scrape_country_data(country_iso)
  country_data[:name] = @country_hash[country_iso]
  ####country_data[:reactors] now has the reactors as an array of names only, not objects. create reactor objects with theses before creating countries!
  ####get reactor ids based on the names, clear country_data[:reactors],
  ####create reactors based on those ids, and push them into country_data[:reactors]
  ####then create country :D

  #fetch reactor_ids in the country
  reactor_ids = country_data[:reactors].collect do |reactor_name|
    reactor_id = @reactor_hash.key(reactor_name)
  end
  number_of_reactors = country_data[:reactors].size
  puts "#{@country_hash[country_iso]}".colorize(:blue) + " has #{number_of_reactors} reactors. Gathering data..."
  #empty country_data[:reactors] array from strings
  country_data[:reactors].clear
  #create and shovel in reactors
  counter = 0
  country_data[:reactors] = reactor_ids.collect do |id|
    counter += 1
    if counter % 20 == 0
      print "#{counter} reactors read. #{number_of_reactors - counter} reactors to go. Please wait... "
    end
    reactor = create_reactor(id)
  end
  country = Country.new(country_data)
  #WOOT!
end

#create_reactor(reactor_id) ⇒ Object



112
113
114
115
116
117
# File 'lib/nuclear_power_reactors.rb', line 112

def create_reactor(reactor_id)
  reactor_data = @npr.scrape_reactor_data(reactor_id)
  reactor_data[:name] = @reactor_hash[reactor_id]
  reactor_data[:id] = reactor_id
  find_reactor(reactor_id).nil? ? reactor = Reactor.new(reactor_data) : find_reactor(reactor_id)
end

#find_country(country_iso) ⇒ Object



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

def find_country(country_iso)
  Country.all.detect { |country|  country.iso == country_iso }
end

#find_reactor(reactor_id) ⇒ Object



74
75
76
# File 'lib/nuclear_power_reactors.rb', line 74

def find_reactor(reactor_id)
  Reactor.all.detect {|reactor| reactor.id == reactor_id}
end

#format_name(name) ⇒ Object

Some country names are given in a comma-infested form, eg ‘Korea, Republic of’. This helper method reformats them.



20
21
22
# File 'lib/nuclear_power_reactors.rb', line 20

def format_name(name)
  name.match(/\,/).nil? ? name : "#{name.split(", ").reverse.join(" ")}"
end

#list_all_countriesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nuclear_power_reactors.rb', line 26

def list_all_countries
  column_width = @country_hash.values.max_by {|name| name.length}.length + 4
  columns = 3
  organizer = 1
  @country_hash.each do |key, value|
      name = "(#{key}) #{format_name(value)}"
    if organizer % columns == 0
      puts "#{name}".colorize(:blue)
    else
      (column_width - name.length).times do
        name << " "
      end
      print "#{name}".colorize(:blue)
    end
    organizer += 1
  end
  puts
end

#list_all_reactorsObject

Showing all reactors (there are over 660 of them currently) will require heavy formatting. method TBA



46
47
# File 'lib/nuclear_power_reactors.rb', line 46

def list_all_reactors
end

#list_country_data(country_iso) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/nuclear_power_reactors.rb', line 120

def list_country_data(country_iso)
   country = find_country(country_iso)
   header = "Country: #{country.name}".colorize(:blue)
   summary_line_2 = "Total Electricity Production: #{country.total_electricity}...Nuclear Electricity Production: #{country.nuclear_electricity}...Nuclear Electricity Share: #{country.nuclear_e_share}"
   summary_line_1 = "Reactors: Operational: #{country.operational}".colorize(:green) + "...Under Construction: #{country.under_construction}".colorize(:cyan) + "...In Long-term Shutdown: #{country.long_term}".colorize(:yellow) + "...In Permanent Shutdown: #{country.permanent_shutdown}".colorize(:red)
   puts header
   puts summary_line_1
   puts summary_line_2
   country.reactors.each_with_index do |reactor, i|
     puts "#{reactor.name}  (#{reactor.id})  #{reactor.status}".colorize(status_color(reactor.status))
   end
end

#reactor_exists?(reactor_id) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/nuclear_power_reactors.rb', line 78

def reactor_exists?(reactor_id)
  @reactor_hash.has_key?(reactor_id)
end

#show_reactor_details(reactor_id, property = "all") ⇒ Object

attribute ‘property’ reserved for later implementations to query only after 1 specific property



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/nuclear_power_reactors.rb', line 135

def show_reactor_details(reactor_id, property = "all")
  capacity_unit = "MW"
  reactor = find_reactor(reactor_id)
  #use colorize here for the status display, too?
  header = "Country: #{reactor.location}".colorize(:blue) + "...Reactor: #{reactor.name}..." + "Status: #{reactor.status}".colorize(status_color(reactor.status))
  puts header
  if property == "all"
    column_width = reactor.instance_variables.max_by {|name| name.length}.length
    data = reactor.instance_variables.each do |variable|
       field = "#{(variable.to_s).gsub(/@/,"")}"
       (column_width - field.length).times do
         field << "."
       end
       if !field.match(/Capacity/).nil?
         puts "#{field} #{reactor.instance_variable_get(variable)} #{capacity_unit}"
       else
         puts "#{field} #{reactor.instance_variable_get(variable)}" if !field.match(/^[A-Z]/).nil?
       end
    end
  end
end

#status_color(status) ⇒ Object



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

def status_color(status)
  color = :black
  case status
  when "Operational"
    color = :green
  when "Under Construction"
    color = :cyan
  when "Permanent Shutdown"
    color = :red
  when "Long-term Shutdown"
    color = :yellow
  else
    color = :black
  end
  color
end