Class: AirQualityIndex::LocalAQI
- Inherits:
-
Object
- Object
- AirQualityIndex::LocalAQI
- Defined in:
- lib/air_quality_index/local_aqi.rb
Instance Attribute Summary collapse
-
#city ⇒ Object
Returns the value of attribute city.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#zip_code ⇒ Object
Returns the value of attribute zip_code.
Instance Method Summary collapse
-
#aqi_info_validation_return(html, city = nil) ⇒ Object
check to see if page information is available based on scraped web site data (based on zip), otherwise proceed with aqi info pull.
-
#call_from_ranking(ranked_city) ⇒ Object
passes ranked_city instance from the Nationwide rankings based on user selection.
-
#call_from_zip_code ⇒ Object
instantiates new instance based on the Local AQI option in the CLI menu.
-
#is_zip_code?(user_zip) ⇒ Boolean
check to see if zip code is valid.
-
#local_aqi_index(city) ⇒ Object
assign scraped information to instance variables.
-
#local_aqi_return ⇒ Object
return output message with scraped information.
-
#timestamp ⇒ Object
grabs timestamp of aqi measurement, if none available, sets to “Time Unavailable” and returns.
-
#zip_code_grabber ⇒ Object
grab zip code from user for local aqi instance.
Instance Attribute Details
#city ⇒ Object
Returns the value of attribute city.
3 4 5 |
# File 'lib/air_quality_index/local_aqi.rb', line 3 def city @city end |
#doc ⇒ Object
Returns the value of attribute doc.
3 4 5 |
# File 'lib/air_quality_index/local_aqi.rb', line 3 def doc @doc end |
#zip_code ⇒ Object
Returns the value of attribute zip_code.
3 4 5 |
# File 'lib/air_quality_index/local_aqi.rb', line 3 def zip_code @zip_code end |
Instance Method Details
#aqi_info_validation_return(html, city = nil) ⇒ Object
check to see if page information is available based on scraped web site data (based on zip), otherwise proceed with aqi info pull
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/air_quality_index/local_aqi.rb', line 66 def aqi_info_validation_return(html, city = nil) = html.search("h2").text.strip if .include? "does not currently have Air Quality data" puts "" puts else #store information as local instance variables self.local_aqi_index(city) #return aqi index information self.local_aqi_return end end |
#call_from_ranking(ranked_city) ⇒ Object
passes ranked_city instance from the Nationwide rankings based on user selection
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/air_quality_index/local_aqi.rb', line 54 def call_from_ranking(ranked_city) site_link = 'https://airnow.gov/' #call scraper class method to access html of page based ranking @doc = AirQualityIndex::Scraper.new.nationwide_scraper_more_info(site_link.concat(ranked_city.link)) #return aqi information for ranked city self.aqi_info_validation_return(self.doc, ranked_city) end |
#call_from_zip_code ⇒ Object
instantiates new instance based on the Local AQI option in the CLI menu
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/air_quality_index/local_aqi.rb', line 6 def call_from_zip_code #grab user's zip code, self.zip_code_grabber #call scraper class method to access html of page based on zip code @doc = AirQualityIndex::Scraper.new.local_aqi_scraper(self.zip_code) #return aqi information, unless unavailable for that zip code self.aqi_info_validation_return(self.doc) end |
#is_zip_code?(user_zip) ⇒ Boolean
check to see if zip code is valid
45 46 47 48 49 50 51 |
# File 'lib/air_quality_index/local_aqi.rb', line 45 def is_zip_code?(user_zip) if user_zip.nil? false else user_zip.size == 5 && user_zip.to_i.to_s == user_zip && !user_zip.nil? end end |
#local_aqi_index(city) ⇒ Object
assign scraped information to instance variables
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/air_quality_index/local_aqi.rb', line 82 def local_aqi_index(city) #Store 'Data Unavailable Message' as variable. Each method below checks for a nil return and sets message if found. unavailable_msg = "Data Not Currently Available" #If a ranked city instance is supplied, set @city to this instance, otherwise, create a new city instance if city.nil? @city = AirQualityIndex::City.new else @city = city end #store location information self.city.location_city = self.doc.search(".ActiveCity").text.strip self.city.location_state = self.doc.search("a.Breadcrumb")[1].text.strip #store aqi index self.doc.at('.TblInvisible').css('tr td').children[1].nil?? self.city.current_aqi_index = unavailable_msg : self.city.current_aqi_index = self.doc.at('.TblInvisible').css('tr td').children[1].text.strip self.doc.search("td.AQDataLg").nil?? self.city.current_aqi_msg = unavailable_msg : self.city.current_aqi_msg = self.doc.search("td.AQDataLg")[0].text.strip self.doc.search("td.HealthMessage")[0].nil?? self.city.current_health_msg = unavailable_msg : self.city.current_health_msg = self.doc.search("td.HealthMessage")[0].text.strip[/(?<=Health Message: ).*/] #store current aqi/ozone data self.doc.search("table")[14].children.css("td")[4].nil?? self.city.current_pm = unavailable_msg : self.city.current_pm = self.doc.search("table")[14].children.css("td")[4].text.strip self.doc.search("td.AQDataPollDetails")[3].nil?? self.city.current_pm_msg = unavailable_msg : self.city.current_pm_msg = self.doc.search("td.AQDataPollDetails")[3].text.strip self.doc.search("table")[14].children.css("td")[1].nil?? self.city.current_ozone = unavailable_msg : self.city.current_ozone = self.doc.search("table")[14].children.css("td")[1].text.strip self.doc.search("td.AQDataPollDetails")[1].nil?? self.city.current_ozone_msg = unavailable_msg : self.city.current_ozone_msg = self.doc.search("td.AQDataPollDetails")[1].text.strip #Extract time from site self.city. = self. #store todays forecast data self.doc.search("td.AQDataPollDetails")[7].nil?? self.city.today_aqi = unavailable_msg : self.city.today_aqi = self.doc.search("td.AQDataPollDetails")[7].text.strip self.doc.search("td.HealthMessage")[1].nil?? self.city.today_aqi_msg = unavailable_msg : self.city.today_aqi_msg = self.doc.search("td.HealthMessage")[1].text.strip[/(?<=Health Message: ).*/] self.doc.search("td.AQDataPollDetails")[5].nil?? self.city.today_ozone = unavailable_msg : self.city.today_ozone = self.doc.search("td.AQDataPollDetails")[5].text.strip #store tomorrows forecast data self.doc.search("td.AQDataPollDetails")[11].nil?? self.city.tomorrow_aqi = unavailable_msg : self.city.tomorrow_aqi = self.doc.search("td.AQDataPollDetails")[11].text.strip self.doc.search("td.HealthMessage")[2].nil?? self.city.tomorrow_aqi_msg = unavailable_msg : self.city.tomorrow_aqi_msg = self.doc.search("td.HealthMessage")[2].text.strip[/(?<=Health Message: ).*/] self.doc.search("td.AQDataPollDetails")[9].nil?? self.city.tomorrow_ozone = unavailable_msg : self.city.tomorrow_ozone = self.doc.search("td.AQDataPollDetails")[9].text.strip #return self self end |
#local_aqi_return ⇒ Object
return output message with scraped information
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/air_quality_index/local_aqi.rb', line 139 def local_aqi_return puts <<-DOC Current Conditions in #{self.city.location_city}, #{self.city.location_state} (#{self.city.}): AQI: #{self.city.current_aqi_index} (#{self.city.current_aqi_msg}) Health Message: #{self.city.current_health_msg} Ozone: #{self.city.current_ozone} (#{self.city.current_ozone_msg}) Particles (PM2.5): #{self.city.current_pm} (#{self.city.current_pm_msg}) Today's Forecast in #{self.city.location_city}, #{self.city.location_state} AQI: #{self.city.today_aqi} Ozone: #{self.city.today_ozone} Health Message: #{self.city.today_aqi_msg} Tomorrow's Forecast in #{self.city.location_city}, #{self.city.location_state} AQI: #{self.city.tomorrow_aqi} Ozone: #{self.city.tomorrow_ozone} Health Message: #{self.city.tomorrow_aqi_msg} DOC end |
#timestamp ⇒ Object
grabs timestamp of aqi measurement, if none available, sets to “Time Unavailable” and returns
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/air_quality_index/local_aqi.rb', line 127 def = self.doc.search("td.AQDataSectionTitle").css("small").text.split(" ") if != [] [0].capitalize! = .join(" ") else = 'Time Captured Unavailable' end end |
#zip_code_grabber ⇒ Object
grab zip code from user for local aqi instance
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/air_quality_index/local_aqi.rb', line 19 def zip_code_grabber puts '' puts "Please enter your 5-digit zip code:" puts '' @zip_code = nil # checks for zip code validation utilzing is_zip_code? method while !self.is_zip_code?(self.zip_code) self.zip_code = gets.chomp if !self.is_zip_code?(self.zip_code) puts "" puts "I'm sorry. That entry was invalid. Please enter a valid 5 digit zip code." puts "" end end #return zip_code self.zip_code.to_i end |