nuclear-power-reactors-cli-gem 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +51 -0
- data/LICENSE +21 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/bin/console +15 -0
- data/bin/nuclear-power-reactors +5 -0
- data/bin/setup +8 -0
- data/lib/country.rb +24 -0
- data/lib/npr_cli.rb +105 -0
- data/lib/npr_scraper.rb +89 -0
- data/lib/nuclear_power_reactors.rb +168 -0
- data/lib/reactor.rb +21 -0
- data/lib/version.rb +3 -0
- data/nuclear-power-reactors-cli-gem.gemspec +24 -0
- data/pkg/nuclear-power-reactors-cli-gem-0.1.0.gem +0 -0
- data/pkg/nuclear-power-reactors-cli-gem-0.1.1.gem +0 -0
- data/pkg/nuclear-power-reactors-cli-gem-0.1.2.gem +0 -0
- data/pkg/nuclear-power-reactors-cli-gem-0.1.3.gem +0 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b79235530e505d0ea7e3bc8c50663928d7e9b186
|
4
|
+
data.tar.gz: f839dff20b36518ab6ad9cce0cd6195d651bbb0a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6bd16ec160924567be7ab0d5ae2aeb1d85f9cddea65a0943894bdeaf3ede73519fd0d6e0769d850c042a2e049ee551863e8c383e4547deac3a05f435c958899b
|
7
|
+
data.tar.gz: 15500f9e8d2c6ffa9145564ee8a796b7afa7a20251dcff717cb99572da8890df02106446de0281fc4a7f18ea9947fa37dfed1b32a3cb6f53b932a8c98cd7d225
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nuclear-power-reactors-cli-gem (0.1.3)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.1)
|
10
|
+
colorize (0.8.1)
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
method_source (0.8.2)
|
13
|
+
mini_portile2 (2.1.0)
|
14
|
+
nokogiri (1.6.8)
|
15
|
+
mini_portile2 (~> 2.1.0)
|
16
|
+
pkg-config (~> 1.1.7)
|
17
|
+
pkg-config (1.1.7)
|
18
|
+
pry (0.10.4)
|
19
|
+
coderay (~> 1.1.0)
|
20
|
+
method_source (~> 0.8.1)
|
21
|
+
slop (~> 3.4)
|
22
|
+
rake (10.5.0)
|
23
|
+
rspec (3.5.0)
|
24
|
+
rspec-core (~> 3.5.0)
|
25
|
+
rspec-expectations (~> 3.5.0)
|
26
|
+
rspec-mocks (~> 3.5.0)
|
27
|
+
rspec-core (3.5.1)
|
28
|
+
rspec-support (~> 3.5.0)
|
29
|
+
rspec-expectations (3.5.0)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.5.0)
|
32
|
+
rspec-mocks (3.5.0)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.5.0)
|
35
|
+
rspec-support (3.5.0)
|
36
|
+
slop (3.6.0)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
bundler (~> 1.12)
|
43
|
+
colorize
|
44
|
+
nokogiri
|
45
|
+
nuclear-power-reactors-cli-gem!
|
46
|
+
pry
|
47
|
+
rake
|
48
|
+
rspec
|
49
|
+
|
50
|
+
BUNDLED WITH
|
51
|
+
1.12.5
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Satu Barnhill
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# nuclear-power-reactors-cli-gem
|
2
|
+
CLI GEM for listing nuclear reactor data from information provided on IAEA.org public resources
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem "nuclear-power-reactors-cli-gem"
|
10
|
+
```
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install from command line
|
17
|
+
|
18
|
+
$ gem install nuclear-power-reactors-cli-gem
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
After installation, type this on your command line to run the program:
|
23
|
+
|
24
|
+
$ nuclear-power-reactors
|
25
|
+
|
26
|
+
The program will print a welcome message and list all the countries where nuclear power reactor data is available at https://www.iaea.org/PRIS/ . Follow the prompts to display data in the country of your choice or to display reactor details.
|
27
|
+
|
28
|
+
|
29
|
+
## Development
|
30
|
+
|
31
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
32
|
+
|
33
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/satub/nuclear-power-reactors-cli-gem.
|
38
|
+
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require 'nuclear-power-reactors-cli-gem'
|
5
|
+
# require_relative "../lib/nuclear_power_reactors.rb"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/country.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Country
|
2
|
+
attr_accessor :name, :iso, :under_construction, :operational,
|
3
|
+
:permanent_shutdown, :long_term, :total_electricity, :nuclear_electricity,
|
4
|
+
:nuclear_e_share, :reactors
|
5
|
+
|
6
|
+
@@all = []
|
7
|
+
|
8
|
+
def initialize(country_data) #country_data is a hash that NuclearPowerReactors class will produce by scraping data
|
9
|
+
@reactors = [] #an array of all the reactors in the country
|
10
|
+
country_data.each do |attribute, value|
|
11
|
+
self.send(("#{attribute}="), value)
|
12
|
+
end
|
13
|
+
@@all << self
|
14
|
+
end
|
15
|
+
|
16
|
+
def reactors
|
17
|
+
@reactors
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.all
|
21
|
+
@@all
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/lib/npr_cli.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require_relative 'nuclear_power_reactors.rb'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
class NPRCLI
|
5
|
+
def greet
|
6
|
+
puts "Welcome to Nuclear Power Reactors Command Line!"
|
7
|
+
puts "This gem allows you to browse data on nuclear power reactors in multiple countries."
|
8
|
+
puts "The data presented is scraped from IAEA's Power Reactor Information System site at https://www.iaea.org/PRIS/home.aspx"
|
9
|
+
puts "Data is currently available for the following countries:"
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_input
|
13
|
+
puts "Enter a command:"
|
14
|
+
input = gets.chomp
|
15
|
+
end
|
16
|
+
|
17
|
+
def goodbye
|
18
|
+
puts "Thank you for using Nuclear Power Reactors CLI! Have an energetic day! :D"
|
19
|
+
end
|
20
|
+
|
21
|
+
def choose_country
|
22
|
+
cmd = "none"
|
23
|
+
until valid_iso_format?(cmd) || cmd == "exit"
|
24
|
+
puts "Choose a country you want more information on by entering the corresponding two-letter ISO-code."
|
25
|
+
puts "For example, enter FI for Finland, or enter exit to quit the program:"
|
26
|
+
cmd = gets.chomp
|
27
|
+
end
|
28
|
+
cmd.upcase
|
29
|
+
end
|
30
|
+
|
31
|
+
def choose_reactor
|
32
|
+
cmd = "none"
|
33
|
+
until valid_reactor_id?(cmd) || cmd == "exit"
|
34
|
+
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:"
|
35
|
+
cmd = gets.chomp
|
36
|
+
end
|
37
|
+
cmd
|
38
|
+
end
|
39
|
+
|
40
|
+
def list_options
|
41
|
+
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."
|
42
|
+
get_input
|
43
|
+
end
|
44
|
+
|
45
|
+
def valid_iso_format?(cmd)
|
46
|
+
!cmd.match(/\b\w{2}\b/).nil? && cmd.match(/\d+/).nil?
|
47
|
+
end
|
48
|
+
|
49
|
+
def valid_reactor_id?(cmd)
|
50
|
+
cmd.match(/\D/).nil? #this input should only consist of digits
|
51
|
+
end
|
52
|
+
|
53
|
+
#run sequence here
|
54
|
+
def run
|
55
|
+
greet
|
56
|
+
npr = NuclearPowerReactors.new
|
57
|
+
npr.list_all_countries
|
58
|
+
cmd = "country"
|
59
|
+
until cmd == "exit" do
|
60
|
+
case cmd
|
61
|
+
when "country"
|
62
|
+
cmd = choose_country
|
63
|
+
if cmd == "EXIT"
|
64
|
+
goodbye
|
65
|
+
exit
|
66
|
+
end
|
67
|
+
if npr.country_exists?(cmd)
|
68
|
+
npr.find_country(cmd).nil? ? npr.create_country(cmd) : npr.find_country(cmd)
|
69
|
+
npr.list_country_data(cmd)
|
70
|
+
cmd = "reactor"
|
71
|
+
else
|
72
|
+
puts "No country with code = #{cmd} exists."
|
73
|
+
cmd = "list"
|
74
|
+
end
|
75
|
+
when "reactor"
|
76
|
+
cmd = choose_reactor
|
77
|
+
if cmd == "exit"
|
78
|
+
goodbye
|
79
|
+
exit
|
80
|
+
end
|
81
|
+
if npr.find_reactor(cmd).nil? && npr.reactor_exists?(cmd)
|
82
|
+
puts "Reactor #{cmd} not located in this country. Find and display data anyway? (y/n)"
|
83
|
+
if get_input.downcase == "y"
|
84
|
+
npr.create_reactor(cmd)
|
85
|
+
npr.show_reactor_details(cmd)
|
86
|
+
end
|
87
|
+
elsif npr.find_reactor(cmd).nil? && !npr.reactor_exists?(cmd)
|
88
|
+
puts "No reactor with id = #{cmd} exists."
|
89
|
+
else
|
90
|
+
npr.show_reactor_details(cmd)
|
91
|
+
end
|
92
|
+
cmd = list_options
|
93
|
+
when "list"
|
94
|
+
npr.list_all_countries
|
95
|
+
cmd = "country"
|
96
|
+
when "exit"
|
97
|
+
exit
|
98
|
+
else
|
99
|
+
cmd = list_options
|
100
|
+
end
|
101
|
+
end
|
102
|
+
goodbye
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
data/lib/npr_scraper.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
class NPRScraper
|
6
|
+
attr_accessor :country_page, :reactor_page
|
7
|
+
attr_reader :home_page, :pris_home, :path_to_country_data, :path_to_reactor_data
|
8
|
+
|
9
|
+
def initialize #default country set to FI, default reactor to LOVIISA-1 -- maybe remove the defaults?
|
10
|
+
@home_page = "https://www.iaea.org"
|
11
|
+
@pris_home = "#{@home_page}/PRIS/home.aspx" #page to draw available countries and reactors from together with their codes
|
12
|
+
@path_to_country_data = "/PRIS/CountryStatistics/CountryDetails.aspx?current="
|
13
|
+
@path_to_reactor_data = "/PRIS/CountryStatistics/ReactorDetails.aspx?current="
|
14
|
+
# @country_page = "#{@home_page}#{@path_to_country_data}FI"
|
15
|
+
# @reactor_page = "#{@home_page}#{@path_to_reactor_data}157"
|
16
|
+
end
|
17
|
+
|
18
|
+
def scrape_available_countries
|
19
|
+
#scrapes the PRIS home page and returns a hash of country data that has the name & iso code for all available countries
|
20
|
+
raw_text = Nokogiri::HTML(open(@pris_home))
|
21
|
+
selection_list = raw_text.css(".box-content.shortCutBox").css("#MainContent_ddlCountry").css("option")
|
22
|
+
selection_list.each_with_object({}) do |country, scraped_country_ids|
|
23
|
+
scraped_country_ids[country.values[0]] = country.text unless country.text == ""
|
24
|
+
#Builds a hash: scraped_country_ids = {country1_name => iso1, country2_name => iso2, ...}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def scrape_available_reactors
|
29
|
+
#scrapes the PRIS home page and returns a hash of reactor data that has the name and id for all available reactors
|
30
|
+
raw_text = Nokogiri::HTML(open(@pris_home))
|
31
|
+
selection_list = raw_text.css(".box-content.shortCutBox").css("#MainContent_ddlReactors").css("option")
|
32
|
+
selection_list.each_with_object({}) do |reactor, scraped_reactor_ids|
|
33
|
+
scraped_reactor_ids[reactor.values[0]] = reactor.text unless reactor.text == ""
|
34
|
+
#Builds a hash: scraped_reactor_ids = {reactor1_name => id1, reactor2_name => id2, ...}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def scrape_country_data(country_iso)
|
39
|
+
#scrapes the PRIS country_page and returns a hash of country data that lists the energy production and the names of reactors in that country
|
40
|
+
@country_page = "#{@home_page}#{@path_to_country_data}#{country_iso}"
|
41
|
+
raw_text = Nokogiri::HTML(open(@country_page))
|
42
|
+
|
43
|
+
summary_data_keys = raw_text.css(".box-content").css("td").css("label")
|
44
|
+
summary_data = raw_text.css(".box-content").css("td").css("h2")
|
45
|
+
reactor_table = raw_text.css(".tablesorter").css("td").css("a")
|
46
|
+
|
47
|
+
country = {}
|
48
|
+
country[:iso] = country_iso
|
49
|
+
summary_data_keys.each_with_index do |key, i|
|
50
|
+
key_string = key.text.strip!.downcase!.match(/\b"?(\w+)\-?\s?(\w+)?\b/).captures
|
51
|
+
if key_string[1].nil?
|
52
|
+
country[key_string[0].to_sym] = summary_data[i].text.strip!
|
53
|
+
else
|
54
|
+
country[key_string.join("_").to_sym] = summary_data[i].text.strip!
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
#calculate the share of energy produced with nuclear power vs total energy produced
|
59
|
+
nep = country[:nuclear_electricity].gsub(/\sGW\.h/, "").to_f
|
60
|
+
tep = country[:total_electricity].gsub(/\sGW\.h/, "").to_f
|
61
|
+
country[:nuclear_e_share] = "#{((nep/tep)*100).round(2)}%"
|
62
|
+
|
63
|
+
#find reactors
|
64
|
+
country[:reactors] = reactor_table.collect {|reactor| reactor.text}
|
65
|
+
country
|
66
|
+
end
|
67
|
+
|
68
|
+
def scrape_reactor_data(reactor_id)
|
69
|
+
#scrapes the PRIS reactor_page and returns a hash of reactor data
|
70
|
+
@reactor_page = "#{@home_page}#{@path_to_reactor_data}#{reactor_id}"
|
71
|
+
raw_text = Nokogiri::HTML(open(@reactor_page))
|
72
|
+
country_name = raw_text.css(".sidebar").css("#MainContent_litCaption").text.strip!
|
73
|
+
reactor_data = raw_text.css(".box-content").css("span")
|
74
|
+
|
75
|
+
reactor = {}
|
76
|
+
reactor[:location] = country_name
|
77
|
+
reactor[:status] = raw_text.css("#MainContent_MainContent_lblReactorStatus").text
|
78
|
+
#add rest of the data with keys
|
79
|
+
reactor_data.each do |data|
|
80
|
+
reactor[data.values[0].match(/MainContent_MainContent_lbl(\w*)/).captures[0].to_sym] = data.text.strip
|
81
|
+
end
|
82
|
+
reactor
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
# test this
|
87
|
+
# npr = NPRScraper.new
|
88
|
+
# country = npr.scrape_country_data("FI")
|
89
|
+
# puts reactor
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require_relative "npr_scraper.rb"
|
2
|
+
require_relative "country.rb"
|
3
|
+
require_relative "reactor.rb"
|
4
|
+
require 'colorize'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
class NuclearPowerReactors
|
8
|
+
|
9
|
+
# 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
|
+
attr_accessor :country_hash, :reactor_hash, :npr
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@npr = NPRScraper.new
|
14
|
+
@country_hash = @npr.scrape_available_countries
|
15
|
+
@reactor_hash = @npr.scrape_available_reactors
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
#Some country names are given in a comma-infested form, eg 'Korea, Republic of'. This helper method reformats them.
|
20
|
+
def format_name(name)
|
21
|
+
name.match(/\,/).nil? ? name : "#{name.split(", ").reverse.join(" ")}"
|
22
|
+
end
|
23
|
+
######TBA !!!write a helper method for choosing color
|
24
|
+
|
25
|
+
|
26
|
+
def list_all_countries
|
27
|
+
column_width = @country_hash.values.max_by {|name| name.length}.length + 4
|
28
|
+
columns = 3
|
29
|
+
organizer = 1
|
30
|
+
@country_hash.each do |key, value|
|
31
|
+
name = "(#{key}) #{format_name(value)}"
|
32
|
+
if organizer % columns == 0
|
33
|
+
puts "#{name}".colorize(:blue)
|
34
|
+
else
|
35
|
+
(column_width - name.length).times do
|
36
|
+
name << " "
|
37
|
+
end
|
38
|
+
print "#{name}".colorize(:blue)
|
39
|
+
end
|
40
|
+
organizer += 1
|
41
|
+
end
|
42
|
+
puts
|
43
|
+
end
|
44
|
+
|
45
|
+
#Showing all reactors (there are over 660 of them currently) will require heavy formatting. method TBA
|
46
|
+
def list_all_reactors
|
47
|
+
end
|
48
|
+
|
49
|
+
def status_color(status)
|
50
|
+
color = :black
|
51
|
+
case status
|
52
|
+
when "Operational"
|
53
|
+
color = :green
|
54
|
+
when "Under Construction"
|
55
|
+
color = :cyan
|
56
|
+
when "Permanent Shutdown"
|
57
|
+
color = :red
|
58
|
+
when "Long-term Shutdown"
|
59
|
+
color = :yellow
|
60
|
+
else
|
61
|
+
color = :black
|
62
|
+
end
|
63
|
+
color
|
64
|
+
end
|
65
|
+
|
66
|
+
def find_country(country_iso)
|
67
|
+
Country.all.detect { |country| country.iso == country_iso }
|
68
|
+
end
|
69
|
+
|
70
|
+
def country_exists?(country_iso)
|
71
|
+
@country_hash.has_key?(country_iso)
|
72
|
+
end
|
73
|
+
|
74
|
+
def find_reactor(reactor_id)
|
75
|
+
Reactor.all.detect {|reactor| reactor.id == reactor_id}
|
76
|
+
end
|
77
|
+
|
78
|
+
def reactor_exists?(reactor_id)
|
79
|
+
@reactor_hash.has_key?(reactor_id)
|
80
|
+
end
|
81
|
+
|
82
|
+
def create_country(country_iso) #input is country iso code
|
83
|
+
country_data = @npr.scrape_country_data(country_iso)
|
84
|
+
country_data[:name] = @country_hash[country_iso]
|
85
|
+
####country_data[:reactors] now has the reactors as an array of names only, not objects. create reactor objects with theses before creating countries!
|
86
|
+
####get reactor ids based on the names, clear country_data[:reactors],
|
87
|
+
####create reactors based on those ids, and push them into country_data[:reactors]
|
88
|
+
####then create country :D
|
89
|
+
|
90
|
+
#fetch reactor_ids in the country
|
91
|
+
reactor_ids = country_data[:reactors].collect do |reactor_name|
|
92
|
+
reactor_id = @reactor_hash.key(reactor_name)
|
93
|
+
end
|
94
|
+
number_of_reactors = country_data[:reactors].size
|
95
|
+
puts "#{@country_hash[country_iso]}".colorize(:blue) + " has #{number_of_reactors} reactors. Gathering data..."
|
96
|
+
#empty country_data[:reactors] array from strings
|
97
|
+
country_data[:reactors].clear
|
98
|
+
#create and shovel in reactors
|
99
|
+
counter = 0
|
100
|
+
country_data[:reactors] = reactor_ids.collect do |id|
|
101
|
+
counter += 1
|
102
|
+
if counter % 20 == 0
|
103
|
+
print "#{counter} reactors read. #{number_of_reactors - counter} reactors to go. Please wait... "
|
104
|
+
end
|
105
|
+
reactor = create_reactor(id)
|
106
|
+
end
|
107
|
+
country = Country.new(country_data)
|
108
|
+
#WOOT!
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
def create_reactor(reactor_id)
|
113
|
+
reactor_data = @npr.scrape_reactor_data(reactor_id)
|
114
|
+
reactor_data[:name] = @reactor_hash[reactor_id]
|
115
|
+
reactor_data[:id] = reactor_id
|
116
|
+
find_reactor(reactor_id).nil? ? reactor = Reactor.new(reactor_data) : find_reactor(reactor_id)
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
def list_country_data(country_iso)
|
121
|
+
country = find_country(country_iso)
|
122
|
+
header = "Country: #{country.name}".colorize(:blue)
|
123
|
+
summary_line_2 = "Total Electricity Production: #{country.total_electricity}...Nuclear Electricity Production: #{country.nuclear_electricity}...Nuclear Electricity Share: #{country.nuclear_e_share}"
|
124
|
+
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)
|
125
|
+
puts header
|
126
|
+
puts summary_line_1
|
127
|
+
puts summary_line_2
|
128
|
+
country.reactors.each_with_index do |reactor, i|
|
129
|
+
puts "#{reactor.name} (#{reactor.id}) #{reactor.status}".colorize(status_color(reactor.status))
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
#attribute 'property' reserved for later implementations to query only after 1 specific property
|
135
|
+
def show_reactor_details(reactor_id, property = "all")
|
136
|
+
capacity_unit = "MW"
|
137
|
+
reactor = find_reactor(reactor_id)
|
138
|
+
#use colorize here for the status display, too?
|
139
|
+
header = "Country: #{reactor.location}".colorize(:blue) + "...Reactor: #{reactor.name}..." + "Status: #{reactor.status}".colorize(status_color(reactor.status))
|
140
|
+
puts header
|
141
|
+
if property == "all"
|
142
|
+
column_width = reactor.instance_variables.max_by {|name| name.length}.length
|
143
|
+
data = reactor.instance_variables.each do |variable|
|
144
|
+
field = "#{(variable.to_s).gsub(/@/,"")}"
|
145
|
+
(column_width - field.length).times do
|
146
|
+
field << "."
|
147
|
+
end
|
148
|
+
if !field.match(/Capacity/).nil?
|
149
|
+
puts "#{field} #{reactor.instance_variable_get(variable)} #{capacity_unit}"
|
150
|
+
else
|
151
|
+
puts "#{field} #{reactor.instance_variable_get(variable)}" if !field.match(/^[A-Z]/).nil?
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
#Test this!
|
160
|
+
# reactors = NuclearPowerReactors.new
|
161
|
+
#reactors.list_all_countries
|
162
|
+
# country = reactors.create_country("FI")
|
163
|
+
# binding.pry
|
164
|
+
# reactors.find_country("FI")
|
165
|
+
# reactors.find_country("DE")
|
166
|
+
# reactors.list_country_data("FI")
|
167
|
+
# reactors.show_reactor_details("157")
|
168
|
+
# binding.pry
|
data/lib/reactor.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class Reactor
|
2
|
+
attr_accessor :name, :id, :location, :status, :Type, :Model, :NetCapacity,
|
3
|
+
:DesignNetCapacity, :GrossCapacity, :ThermalCapacity, :ConstructionStartDate,
|
4
|
+
:FirstCriticality, :ConstrSuspendedDate, :ConstrRestartDate, :GridConnectionDate,
|
5
|
+
:CommercialOperationDate, :LongTermShutdownDate, :RestartDate, :PermanentShutdownDate,
|
6
|
+
:Generation, :EAF, :OperatingFactor, :EUL, :LoadFactor, :LifetimePerformanceYear
|
7
|
+
|
8
|
+
@@all = []
|
9
|
+
|
10
|
+
def initialize(reactor_data) #reactor_data is a hash that NuclearPowerReactors class will produce by scraping data
|
11
|
+
reactor_data.each do |attribute, value|
|
12
|
+
self.send(("#{attribute}="), value)
|
13
|
+
end
|
14
|
+
@@all << self
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.all
|
18
|
+
@@all
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/lib/version.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nuclear-power-reactors-cli-gem"
|
8
|
+
spec.version = Nuclear_Power_Reactors_CLI_GEM::VERSION
|
9
|
+
spec.authors = ["Satu Barnhill"]
|
10
|
+
spec.email = ["satu.barnhill@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{CLI GEM for listing nuclear reactor data from information provided on IAEA.org public resources.}
|
13
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
14
|
+
spec.homepage = "https://github.com/satub/nuclear-power-reactors-cli-gem"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "bin"
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| f[3..-1] }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nuclear-power-reactors-cli-gem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Satu Barnhill
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- satu.barnhill@gmail.com
|
58
|
+
executables:
|
59
|
+
- "/console"
|
60
|
+
- "/nuclear-power-reactors"
|
61
|
+
- "/setup"
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".rspec"
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/nuclear-power-reactors
|
73
|
+
- bin/setup
|
74
|
+
- lib/country.rb
|
75
|
+
- lib/npr_cli.rb
|
76
|
+
- lib/npr_scraper.rb
|
77
|
+
- lib/nuclear_power_reactors.rb
|
78
|
+
- lib/reactor.rb
|
79
|
+
- lib/version.rb
|
80
|
+
- nuclear-power-reactors-cli-gem.gemspec
|
81
|
+
- pkg/nuclear-power-reactors-cli-gem-0.1.0.gem
|
82
|
+
- pkg/nuclear-power-reactors-cli-gem-0.1.1.gem
|
83
|
+
- pkg/nuclear-power-reactors-cli-gem-0.1.2.gem
|
84
|
+
- pkg/nuclear-power-reactors-cli-gem-0.1.3.gem
|
85
|
+
homepage: https://github.com/satub/nuclear-power-reactors-cli-gem
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.4.5.1
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: CLI GEM for listing nuclear reactor data from information provided on IAEA.org
|
109
|
+
public resources.
|
110
|
+
test_files: []
|