class NuclearPowerReactors

Attributes

country_hash[RW]

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

npr[RW]

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

reactor_hash[RW]

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

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

country_exists?(country_iso) click to toggle source
# File lib/nuclear_power_reactors.rb, line 70
def country_exists?(country_iso)
  @country_hash.has_key?(country_iso)
end
create_country(country_iso) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# File lib/nuclear_power_reactors.rb, line 20
def format_name(name)
  name.match(/\,/).nil? ? name : "#{name.split(", ").reverse.join(" ")}"
end
list_all_countries() click to toggle source

TBA !!!write a helper method for choosing color

# 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_reactors() click to toggle source

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

# File lib/nuclear_power_reactors.rb, line 46
def list_all_reactors
end
list_country_data(country_iso) click to toggle source
# 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) click to toggle source
# 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") click to toggle source

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

# 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) click to toggle source
# 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