air_quality_index 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/NOTES.md +50 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/air_quality_index.gemspec +26 -0
- data/bin/air-quality-index +5 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/air_quality_index/aqi_info.rb +24 -0
- data/lib/air_quality_index/cli.rb +67 -0
- data/lib/air_quality_index/local_aqi.rb +152 -0
- data/lib/air_quality_index/nationwide_aqi.rb +122 -0
- data/lib/air_quality_index/scraper.rb +18 -0
- data/lib/air_quality_index/version.rb +3 -0
- data/lib/air_quality_index.rb +14 -0
- data/spec.md +6 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 412f573ac84675d71e5b64ce569322a749973e87
|
4
|
+
data.tar.gz: 03f86b91687ee455c6c624040be2c51d1b932c0b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 515f2009cc76b05359c910f17a5b7034678c490396950545027b44dfc6823aeac2c1187d1a9e360403e0b1bd85f85b0ef01b978dc38e21e64f49737b12340ecb
|
7
|
+
data.tar.gz: 13857101f9c9efe756db83d2e624ede8d423e010fac309fac1158c5be80045540f3be1870e9d3db70b358b72573523dd6e14a48d48abd4072dde2412a6fb7bae
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 'Jameson Bass'
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/NOTES.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Air Quality Index Notes
|
2
|
+
|
3
|
+
How to build a CLI Gem
|
4
|
+
|
5
|
+
1. Plan your gem, imagine your interface
|
6
|
+
2. Start with the project structure - google
|
7
|
+
3. Start with the entry point - the file run
|
8
|
+
4. Force that to build the CLI Interface
|
9
|
+
5. Stub out the interface
|
10
|
+
6. Stat making things real
|
11
|
+
7. Discover objects
|
12
|
+
8. Program
|
13
|
+
|
14
|
+
- Build a command line interface to pull Air Quality Index for inputted zip code
|
15
|
+
|
16
|
+
user types air_quality_index
|
17
|
+
|
18
|
+
CLI welcomes user.
|
19
|
+
|
20
|
+
Asks for user if they would like to get local AQI index information, nationwide AQI information (highest AQI areas in the United States) or enter "More Information" to get overview about air quality indexes
|
21
|
+
|
22
|
+
Desired Output -
|
23
|
+
|
24
|
+
- If user types in nationwide AQI information, application retrieves top 5 AQI indexed areas from https://airnow.gov/index.cfm?action=airnow.main
|
25
|
+
|
26
|
+
1. Victorville, CA - 253 (Very Unhealthy)
|
27
|
+
2. Shady Cove, OR - 177 (Unhealthy)
|
28
|
+
3. Applegate Valley, OR - 166 (Unhealthy)
|
29
|
+
4. Medford, OR - 164 (Unhealthy)
|
30
|
+
5. Prineville, OR - 158 (Unhealthy)
|
31
|
+
|
32
|
+
- If user enters local index, CLI asks user to enter a desired zip code (ie. 97211 (Portland, OR))
|
33
|
+
|
34
|
+
Current Conditions in Portland (AQI Observed at 10:00 PDT):
|
35
|
+
|
36
|
+
AQI - 135 (Unhealthy for Sensitive Groups)
|
37
|
+
Ozone - 13 (Good)
|
38
|
+
Particles (PM2.5) - 136 (Unhealthy for Sensitive Groups)
|
39
|
+
|
40
|
+
Today's Forecast in Portland
|
41
|
+
|
42
|
+
AQI - Good
|
43
|
+
Health Message - None
|
44
|
+
|
45
|
+
Tomorrow's Forecast in Portland
|
46
|
+
|
47
|
+
AQI - Good
|
48
|
+
Health Message - None
|
49
|
+
|
50
|
+
- More information puts information from AQI Index Basics Page (https://airnow.gov/index.cfm?action=aqibasics.aqi#good)
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# AirQualityIndex
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/air_quality_index`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'air_quality_index'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install air_quality_index
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/'jamesonbass-88057'/air_quality_index.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'air_quality_index/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "air_quality_index"
|
8
|
+
spec.version = AirQualityIndex::VERSION
|
9
|
+
spec.authors = ["'Jameson Bass'"]
|
10
|
+
spec.email = ["'jamesonbass@gmail.com'"]
|
11
|
+
|
12
|
+
spec.summary = %q{Air quality index grabber}
|
13
|
+
spec.description = %q{This gem pulls local Air Quality Index measurements and forecasts based on Zip Code. Users can also view the top 5 national rankings for AQI Indexes (most polluted areas)}
|
14
|
+
spec.homepage = "https://github.com/jamesonbass1987/air_quality_index"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "air_quality_index"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class AirQualityIndex::AQI_Information
|
2
|
+
|
3
|
+
#prints general AQI information
|
4
|
+
def call
|
5
|
+
|
6
|
+
puts ''
|
7
|
+
puts "Each category corresponds to a different level of health concern. The six levels of health concern and what they mean are:"
|
8
|
+
puts ''
|
9
|
+
puts '"Good" AQI is 0 to 50. Air quality is considered satisfactory, and air pollution poses little or no risk.'
|
10
|
+
puts ''
|
11
|
+
puts '"Moderate" AQI is 51 to 100. Air quality is acceptable; however, for some pollutants there may be a moderate health concern for a very small number of people. For example, people who are unusually sensitive to ozone may experience respiratory symptoms.'
|
12
|
+
puts ''
|
13
|
+
puts '"Unhealthy for Sensitive Groups" AQI is 101 to 150. Although general public is not likely to be affected at this AQI range, people with lung disease, older adults and children are at a greater risk from exposure to ozone, whereas persons with heart and lung disease, older adults and children are at greater risk from the presence of particles in the air.'
|
14
|
+
puts ''
|
15
|
+
puts '"Unhealthy" AQI is 151 to 200. Everyone may begin to experience some adverse health effects, and members of the sensitive groups may experience more serious effects.'
|
16
|
+
puts ''
|
17
|
+
puts '"Very Unhealthy" AQI is 201 to 300. This would trigger a health alert signifying that everyone may experience more serious health effects.'
|
18
|
+
puts ''
|
19
|
+
puts '"Hazardous" AQI greater than 300. This would trigger a health warnings of emergency conditions. The entire population is more likely to be affected.'
|
20
|
+
puts ''
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
class AirQualityIndex::CLI
|
2
|
+
|
3
|
+
#instantiates a new CLI instance for the program
|
4
|
+
def call
|
5
|
+
|
6
|
+
puts ''
|
7
|
+
puts "Welcome to Air Quality Index (AQI) Grabber"
|
8
|
+
puts ''
|
9
|
+
|
10
|
+
self.list_options
|
11
|
+
self.menu
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
#lists menu options for user to select from
|
16
|
+
def list_options
|
17
|
+
puts <<-DOC.gsub /^\s*/, ''
|
18
|
+
|
19
|
+
1. Local AQI Information
|
20
|
+
2. Top 5 Nationwide AQI Rankings
|
21
|
+
3. General AQI Information
|
22
|
+
|
23
|
+
DOC
|
24
|
+
end
|
25
|
+
|
26
|
+
#grabs user selection from menu and instantiates appropriate method based on that selection
|
27
|
+
def menu
|
28
|
+
|
29
|
+
puts ''
|
30
|
+
puts "Please enter a numeric selection (1-3), or type exit."
|
31
|
+
puts ''
|
32
|
+
|
33
|
+
user_input = nil
|
34
|
+
|
35
|
+
while user_input != 'exit'
|
36
|
+
|
37
|
+
#gets user input
|
38
|
+
user_input = gets.strip.downcase
|
39
|
+
#based on user input, call appropriate class method
|
40
|
+
case user_input
|
41
|
+
when '1'
|
42
|
+
AirQualityIndex::LocalAQI.new.call_from_zip_code
|
43
|
+
self.return_message
|
44
|
+
when '2'
|
45
|
+
AirQualityIndex::NationwideAQI.new.call
|
46
|
+
self.return_message
|
47
|
+
when '3'
|
48
|
+
AirQualityIndex::AQI_Information.new.call
|
49
|
+
self.return_message
|
50
|
+
when 'return'
|
51
|
+
self.list_options
|
52
|
+
self.menu
|
53
|
+
when 'exit'
|
54
|
+
exit!
|
55
|
+
else
|
56
|
+
puts "I'm sorry. I didn't understand what you meant. Please enter a numeric selection (1-3), or type exit."
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
#return message displayed after each menu selection call
|
62
|
+
def return_message
|
63
|
+
puts "Type 'return' to go back to previous menu, or type 'exit'."
|
64
|
+
puts ""
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
class AirQualityIndex::LocalAQI
|
2
|
+
|
3
|
+
attr_accessor :zip_code, :todays_index, :current_location_city, :current_location_state, :current_aqi_index, :current_health_msg, :current_pm, :current_pm_msg, :current_ozone, :current_ozone_msg, :current_aqi_timestamp, :today_aqi, :today_aqi_msg, :today_ozone, :tomorrow_aqi, :tomorrow_aqi_msg, :tomorrow_ozone, :doc
|
4
|
+
|
5
|
+
#instantiates new instance based on the Local AQI option in the CLI menu
|
6
|
+
def call_from_zip_code
|
7
|
+
|
8
|
+
#grab user's zip code,
|
9
|
+
self.zip_code_grabber
|
10
|
+
|
11
|
+
#call scraper class method to access html of page based on zip code
|
12
|
+
@doc = AirQualityIndex::Scraper.new.local_aqi_scraper(self.zip_code)
|
13
|
+
|
14
|
+
#return aqi information, unless unavailable for that zip code
|
15
|
+
self.aqi_info_validation_return(self.doc)
|
16
|
+
end
|
17
|
+
|
18
|
+
#instantiates new instance from the Nationwide rankings based on user selection
|
19
|
+
def call_from_ranking(link)
|
20
|
+
|
21
|
+
#call scraper class method to access html of page based ranking
|
22
|
+
@doc = AirQualityIndex::Scraper.new.nationwide_scraper_more_info(link)
|
23
|
+
|
24
|
+
#return aqi information for ranked city
|
25
|
+
self.aqi_info_validation_return(self.doc)
|
26
|
+
end
|
27
|
+
|
28
|
+
# grab zip code from user for local aqi instance
|
29
|
+
def zip_code_grabber
|
30
|
+
|
31
|
+
puts ''
|
32
|
+
puts "Please enter your 5-digit zip code:"
|
33
|
+
puts ''
|
34
|
+
|
35
|
+
@zip_code = nil
|
36
|
+
|
37
|
+
# checks for zip code validation utilzing is_zip_code? method
|
38
|
+
while !self.is_zip_code?(self.zip_code)
|
39
|
+
|
40
|
+
self.zip_code = gets.chomp
|
41
|
+
|
42
|
+
if !self.is_zip_code?(self.zip_code)
|
43
|
+
puts ""
|
44
|
+
puts "I'm sorry. That entry was invalid. Please enter a valid 5 digit zip code."
|
45
|
+
puts ""
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
#return zip_code
|
51
|
+
self.zip_code.to_i
|
52
|
+
end
|
53
|
+
|
54
|
+
#check to see if zip code is valid
|
55
|
+
def is_zip_code?(user_zip)
|
56
|
+
if user_zip.nil?
|
57
|
+
false
|
58
|
+
else
|
59
|
+
user_zip.size == 5 && user_zip.to_i.to_s == user_zip && !user_zip.nil?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
#check to see if zip code page information is available based on scraped web site data, otherwise proceed with aqi info pull
|
64
|
+
def aqi_info_validation_return(html)
|
65
|
+
|
66
|
+
page_message = self.doc.search("h2").text.strip
|
67
|
+
|
68
|
+
if page_message.include? "does not currently have Air Quality data"
|
69
|
+
puts ""
|
70
|
+
puts page_message
|
71
|
+
else
|
72
|
+
#store information as local instance variables
|
73
|
+
self.local_aqi_index
|
74
|
+
#return aqi index information
|
75
|
+
self.local_aqi_return
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
#grabs timestamp of aqi measurement
|
80
|
+
def timestamp
|
81
|
+
timestamp = self.doc.search("td.AQDataSectionTitle").css("small").text.split(" ")
|
82
|
+
timestamp[0].capitalize!
|
83
|
+
timestamp = timestamp.join(" ")
|
84
|
+
timestamp
|
85
|
+
end
|
86
|
+
|
87
|
+
#assign scraped information to instance variables
|
88
|
+
def local_aqi_index
|
89
|
+
|
90
|
+
#Store 'Data Unavailable Message' as variable. Each method below checks for a nil return and sets message if found.
|
91
|
+
unavailable_msg = "Data Not Currently Available"
|
92
|
+
|
93
|
+
#store location information
|
94
|
+
self.current_location_city = self.doc.search(".ActiveCity").text.strip
|
95
|
+
self.current_location_state = self.doc.search("a.Breadcrumb")[1].text.strip
|
96
|
+
|
97
|
+
#store aqi index
|
98
|
+
self.doc.at('.TblInvisible').css('tr td').children[1].nil?? self.current_aqi_index = unavailable_msg : self.current_aqi_index = self.doc.at('.TblInvisible').css('tr td').children[1].text.strip
|
99
|
+
self.doc.search("td.HealthMessage")[0].nil?? self.current_health_msg = unavailable_msg : self.current_health_msg = self.doc.search("td.HealthMessage")[0].text.strip[/(?<=Health Message: ).*/]
|
100
|
+
|
101
|
+
#store current aqi/ozone data
|
102
|
+
self.doc.search("table")[14].children.css("td")[4].nil?? self.current_pm = unavailable_msg : self.current_pm = self.doc.search("table")[14].children.css("td")[4].text.strip
|
103
|
+
self.doc.search("td.AQDataPollDetails")[3].nil?? self.current_pm_msg = unavailable_msg : self.current_pm_msg = self.doc.search("td.AQDataPollDetails")[3].text.strip
|
104
|
+
self.doc.search("table")[14].children.css("td")[1].nil?? self.current_ozone = unavailable_msg : self.current_ozone = self.doc.search("table")[14].children.css("td")[1].text.strip
|
105
|
+
self.doc.search("td.AQDataPollDetails")[1].nil?? self.current_ozone_msg = unavailable_msg : self.current_ozone_msg = self.doc.search("td.AQDataPollDetails")[1].text.strip
|
106
|
+
|
107
|
+
#Extract time from site
|
108
|
+
self.current_aqi_timestamp = self.timestamp
|
109
|
+
|
110
|
+
#store todays forecast data
|
111
|
+
self.doc.search("td.AQDataPollDetails")[7].nil?? self.today_aqi = unavailable_msg : self.today_aqi = self.doc.search("td.AQDataPollDetails")[7].text.strip
|
112
|
+
self.doc.search("td.HealthMessage")[1].nil?? self.today_aqi_msg = unavailable_msg : self.today_aqi_msg = self.doc.search("td.HealthMessage")[1].text.strip[/(?<=Health Message: ).*/]
|
113
|
+
self.doc.search("td.AQDataPollDetails")[5].nil?? self.today_ozone = unavailable_msg : self.today_ozone = self.doc.search("td.AQDataPollDetails")[5].text.strip
|
114
|
+
|
115
|
+
#store tomorrows forecast data
|
116
|
+
self.doc.search("td.AQDataPollDetails")[11].nil?? self.tomorrow_aqi = unavailable_msg : self.tomorrow_aqi = self.doc.search("td.AQDataPollDetails")[11].text.strip
|
117
|
+
self.doc.search("td.HealthMessage")[2].nil?? self.tomorrow_aqi_msg = unavailable_msg : self.tomorrow_aqi_msg = self.doc.search("td.HealthMessage")[2].text.strip[/(?<=Health Message: ).*/]
|
118
|
+
self.doc.search("td.AQDataPollDetails")[9].nil?? self.tomorrow_ozone = unavailable_msg : self.tomorrow_ozone = self.doc.search("td.AQDataPollDetails")[9].text.strip
|
119
|
+
|
120
|
+
#return self
|
121
|
+
self
|
122
|
+
end
|
123
|
+
|
124
|
+
#return output message with scraped information
|
125
|
+
def local_aqi_return
|
126
|
+
|
127
|
+
puts <<-DOC
|
128
|
+
|
129
|
+
Current Conditions in #{self.current_location_city}, #{self.current_location_state} (#{self.current_aqi_timestamp}):
|
130
|
+
|
131
|
+
AQI: #{self.current_aqi_index}
|
132
|
+
Health Message: #{self.current_health_msg}
|
133
|
+
|
134
|
+
Ozone: #{self.current_ozone} (#{self.current_ozone_msg})
|
135
|
+
Particles (PM2.5): #{self.current_pm} (#{self.current_pm_msg})
|
136
|
+
|
137
|
+
Today's Forecast in #{self.current_location_city}, #{self.current_location_state}
|
138
|
+
|
139
|
+
AQI: #{self.today_aqi}
|
140
|
+
Ozone: #{self.today_ozone}
|
141
|
+
Health Message: #{self.today_aqi_msg}
|
142
|
+
|
143
|
+
Tomorrow's Forecast in #{self.current_location_city}, #{self.current_location_state}
|
144
|
+
|
145
|
+
AQI: #{self.tomorrow_aqi}
|
146
|
+
Ozone: #{self.tomorrow_ozone}
|
147
|
+
Health Message: #{self.tomorrow_aqi_msg}
|
148
|
+
|
149
|
+
DOC
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
class AirQualityIndex::NationwideAQI
|
2
|
+
|
3
|
+
attr_accessor :todays_date, :national_aqi, :first_city, :first_index, :first_message, :second_city, :second_index, :second_message, :third_city, :third_index, :third_message, :fourth_city, :fourth_index, :fourth_message, :fifth_city, :fifth_index, :fifth_message, :html, :first_link, :second_link, :third_link, :fourth_link, :fifth_link
|
4
|
+
|
5
|
+
#instantiates a new pull from AirNow.gov for the top 5 current rankings on air pollution
|
6
|
+
def call
|
7
|
+
self.get_nationwide_data
|
8
|
+
puts self.todays_rankings_output
|
9
|
+
self.get_more_info?
|
10
|
+
end
|
11
|
+
|
12
|
+
#outputs nationwide ranking information from scraped web data
|
13
|
+
def todays_rankings_output
|
14
|
+
puts ""
|
15
|
+
puts "Nationwide AQI Rankings for #{self.todays_date.month}/#{self.todays_date.day}/#{self.todays_date.year}"
|
16
|
+
puts ""
|
17
|
+
puts <<-DOC
|
18
|
+
1. #{self.first_city} - #{self.first_index} (#{self.first_message})
|
19
|
+
2. #{self.second_city} - #{self.second_index} (#{self.second_message})
|
20
|
+
3. #{self.third_city} - #{self.third_index} (#{self.third_message})
|
21
|
+
4. #{self.fourth_city} - #{self.fourth_index} (#{self.fourth_message})
|
22
|
+
5. #{self.fifth_city} - #{self.fifth_index} (#{self.fifth_message})
|
23
|
+
DOC
|
24
|
+
end
|
25
|
+
|
26
|
+
#sets today's date for data output
|
27
|
+
def todays_date
|
28
|
+
@todays_date = Time.new
|
29
|
+
end
|
30
|
+
|
31
|
+
#sets instance variables for each piece of ranking data from scraped html
|
32
|
+
def get_nationwide_data
|
33
|
+
|
34
|
+
#scrape page for top ranking cities
|
35
|
+
@html = AirQualityIndex::Scraper.new.nationwide_aqi_scraper
|
36
|
+
|
37
|
+
#store first rank data
|
38
|
+
self.first_city = self.html.search("a.NtnlSummaryCity")[0].text.strip
|
39
|
+
self.first_index = self.html.search("div.TabbedPanelsContent").children.css("tr td")[1].children.text.strip
|
40
|
+
self.first_message = aqi_message_set(self.first_index)
|
41
|
+
self.first_link = html.search("a.NtnlSummaryCity")[0]['href']
|
42
|
+
|
43
|
+
#store second rank data
|
44
|
+
self.second_city = self.html.search("a.NtnlSummaryCity")[1].text.strip
|
45
|
+
self.second_index = self.html.search("div.TabbedPanelsContent").children.css("tr td")[6].children.text.strip
|
46
|
+
self.second_message = aqi_message_set(self.second_index)
|
47
|
+
self.second_link = html.search("a.NtnlSummaryCity")[1]['href']
|
48
|
+
|
49
|
+
#store third rank data
|
50
|
+
self.third_city = self.html.search("a.NtnlSummaryCity")[2].text.strip
|
51
|
+
self.third_index = self.html.search("div.TabbedPanelsContent").children.css("tr td")[11].children.text.strip
|
52
|
+
self.third_message = aqi_message_set(self.third_index)
|
53
|
+
self.third_link = html.search("a.NtnlSummaryCity")[2]['href']
|
54
|
+
|
55
|
+
#store fourth rank data
|
56
|
+
self.fourth_city = self.html.search("a.NtnlSummaryCity")[3].text.strip
|
57
|
+
self.fourth_index = self.html.search("div.TabbedPanelsContent").children.css("tr td")[16].children.text.strip
|
58
|
+
self.fourth_message = aqi_message_set(self.fourth_index)
|
59
|
+
self.fourth_link = html.search("a.NtnlSummaryCity")[3]['href']
|
60
|
+
|
61
|
+
#store fifth rank data
|
62
|
+
self.fifth_city = self.html.search("a.NtnlSummaryCity")[4].text.strip
|
63
|
+
self.fifth_index = self.html.search("div.TabbedPanelsContent").children.css("tr td")[21].children.text.strip
|
64
|
+
self.fifth_message = aqi_message_set(self.fifth_index)
|
65
|
+
self.fifth_link = html.search("a.NtnlSummaryCity")[4]['href']
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
#set aqi messages based off of aqi index ranking
|
70
|
+
def aqi_message_set(index)
|
71
|
+
case index.to_i
|
72
|
+
when 0..50
|
73
|
+
"Good"
|
74
|
+
when 51..100
|
75
|
+
"Moderate"
|
76
|
+
when 101..150
|
77
|
+
"Unhealthy For Sensitive Groups"
|
78
|
+
when 151..200
|
79
|
+
"Unhealthy"
|
80
|
+
when 201..300
|
81
|
+
"Very Unhealthy"
|
82
|
+
when 301 - 500
|
83
|
+
"Hazardous"
|
84
|
+
else
|
85
|
+
"You are probably too dead to read this from all of the air pollution"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
#asks user if they would like additional information on any of the ranked cities, if so, pulls individual ranking information via local_aqi method call
|
90
|
+
def get_more_info?
|
91
|
+
|
92
|
+
puts "Would you like local information for any of the cities listed? Please enter a numerical value 1-5, type 'exit' to end program, or type any other key to return to previous menu."
|
93
|
+
puts ""
|
94
|
+
|
95
|
+
link = 'https://airnow.gov/'
|
96
|
+
city_info = nil
|
97
|
+
|
98
|
+
#gets user input
|
99
|
+
user_input = gets.strip.downcase
|
100
|
+
|
101
|
+
#depending on user input, sets new local aqi instance to city_info variable
|
102
|
+
case user_input
|
103
|
+
when '1'
|
104
|
+
city_info = AirQualityIndex::LocalAQI.new.call_from_ranking(link.concat(self.first_link))
|
105
|
+
when '2'
|
106
|
+
city_info = AirQualityIndex::LocalAQI.new.call_from_ranking(link.concat(self.second_link))
|
107
|
+
when '3'
|
108
|
+
city_info = AirQualityIndex::LocalAQI.new.call_from_ranking(link.concat(self.third_link))
|
109
|
+
when '4'
|
110
|
+
city_info = AirQualityIndex::LocalAQI.new.call_from_ranking(link.concat(self.fourth_link))
|
111
|
+
when '5'
|
112
|
+
city_info = AirQualityIndex::LocalAQI.new.call_from_ranking(link.concat(self.fifth_link))
|
113
|
+
when 'exit'
|
114
|
+
exit!
|
115
|
+
end
|
116
|
+
|
117
|
+
#return city_info if user selected one
|
118
|
+
city_info.local_aqi_return unless city_info.nil?
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class AirQualityIndex::Scraper
|
2
|
+
|
3
|
+
#scrape AQI webpage based on submitted zip code
|
4
|
+
def local_aqi_scraper(zip_code)
|
5
|
+
Nokogiri::HTML(open("https://airnow.gov/index.cfm?action=airnow.local_city&zipcode=#{zip_code}"))
|
6
|
+
end
|
7
|
+
|
8
|
+
#scrape main AQI website to grab top rankings for air quality
|
9
|
+
def nationwide_aqi_scraper
|
10
|
+
Nokogiri::HTML(open("https://airnow.gov/index.cfm?action=airnow.main"))
|
11
|
+
end
|
12
|
+
|
13
|
+
#scrape main AQI website to grab individual page from top ranking based on user input
|
14
|
+
def nationwide_scraper_more_info(link)
|
15
|
+
Nokogiri::HTML(open(link))
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'pry'
|
4
|
+
|
5
|
+
|
6
|
+
require_relative "./air_quality_index/version"
|
7
|
+
require_relative "./air_quality_index/cli"
|
8
|
+
require_relative "./air_quality_index/local_aqi"
|
9
|
+
require_relative "./air_quality_index/nationwide_aqi"
|
10
|
+
require_relative "./air_quality_index/aqi_info"
|
11
|
+
require_relative "./air_quality_index/scraper.rb"
|
12
|
+
|
13
|
+
module AirQualityIndex
|
14
|
+
end
|
data/spec.md
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: air_quality_index
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "'Jameson Bass'"
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-18 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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
|
+
description: This gem pulls local Air Quality Index measurements and forecasts based
|
42
|
+
on Zip Code. Users can also view the top 5 national rankings for AQI Indexes (most
|
43
|
+
polluted areas)
|
44
|
+
email:
|
45
|
+
- "'jamesonbass@gmail.com'"
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- NOTES.md
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- air_quality_index.gemspec
|
57
|
+
- bin/air-quality-index
|
58
|
+
- bin/console
|
59
|
+
- bin/setup
|
60
|
+
- lib/air_quality_index.rb
|
61
|
+
- lib/air_quality_index/aqi_info.rb
|
62
|
+
- lib/air_quality_index/cli.rb
|
63
|
+
- lib/air_quality_index/local_aqi.rb
|
64
|
+
- lib/air_quality_index/nationwide_aqi.rb
|
65
|
+
- lib/air_quality_index/scraper.rb
|
66
|
+
- lib/air_quality_index/version.rb
|
67
|
+
- spec.md
|
68
|
+
homepage: https://github.com/jamesonbass1987/air_quality_index
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.6.11
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Air quality index grabber
|
92
|
+
test_files: []
|