Class: BreatheIn::Scraper

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

Constant Summary collapse

@@air_quality_info =
{}
@@scraped =
nil

Class Method Summary collapse

Class Method Details

.air_qualityObject



6
7
8
# File 'lib/breathe_in/scraper.rb', line 6

def self.air_quality
  @@air_quality_info
end

.AQI_range_informationObject



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/breathe_in/scraper.rb', line 98

def self.AQI_range_information
  information = <<-Ruby
    The Air Quality Index (AQI) translates air quality data into an easily understandable number to identify how clean or polluted the outdoor air is, along with possible health effects. 
    The U.S. Environmental Protection Agency, National Oceanic and Atmospheric Administration, National Park Service, tribal, state, and local agencies developed the AirNow system to provide the public with easy access to national air quality information. 

    "Good" AQI is 0 - 50. 
    Air quality is considered satisfactory, and air pollution poses little or no risk.
    ***************************
    "Moderate" AQI is 51 - 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.
    ***************************
    "Unhealthy for Sensitive Groups" AQI is 101 - 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. 
    ***************************
    "Unhealthy" AQI is 151 - 200. 
    Everyone may begin to experience some adverse health effects, and members of the sensitive groups may experience more serious effects.
    ***************************
    "Very Unhealthy" AQI is 201 - 300. 
    This would trigger a health alert signifying that everyone may experience more serious health effects.
    ***************************
    "Hazardous" AQI greater than 300. 
    This would trigger a health warnings of emergency conditions. The entire population is more likely to be affected.
    ***************************
    All descriptions, information, and data are provided courtesy of AirNow.gov. Visit the website to learn more.
  Ruby
  puts information
end

.city_air_qualityObject



26
27
28
29
30
31
32
33
34
# File 'lib/breathe_in/scraper.rb', line 26

def self.city_air_quality
  city_name
  today_high
  index_level
  current_conditions_time
  current_conditions_value
  current_conditions_index 
  air_quality
end

.city_nameObject



36
37
38
39
40
# File 'lib/breathe_in/scraper.rb', line 36

def self.city_name
  city = scraped_pg.css("#pageContent .ActiveCity")
  #returns array of an object with attributes including city name
  city.empty? ? nil : air_quality[:city_name] = city.text.strip
end

.current_conditions_indexObject



68
69
70
71
72
# File 'lib/breathe_in/scraper.rb', line 68

def self.current_conditions_index 
  current_index = scraped_pg.css(".AQDataSectionTitle .TblInvisible .AQDataLg")
  #returns array of an object with attributes including current condition index
  current_index.empty? ? nil : air_quality[:last_update_index] = current_index.text.strip
end

.current_conditions_timeObject



54
55
56
57
58
59
60
# File 'lib/breathe_in/scraper.rb', line 54

def self.current_conditions_time
  unformatted_info = scraped_pg.css(".AQData .AQDataSectionTitle .AQDataSectionTitle")
  #returns array of objects with attributes of title and date information
  current_info = unformatted_info.text.strip.split("\r\n                                       \t")
  # => returns array ["Air Quality Index (AQI)", "observed at 19:00 PST"]
  unformatted_info.empty? ? nil : air_quality[:last_update_time] = current_info[1]
end

.current_conditions_valueObject



62
63
64
65
66
# File 'lib/breathe_in/scraper.rb', line 62

def self.current_conditions_value 
  current_value = scraped_pg.css(".AQDataSectionTitle .TblInvisible .TblInvisible td")
  #returns array of an object with attributes including current condition value
  current_value.empty? ? nil : air_quality[:last_update_value] = current_value.text.strip.to_i
end

.index_goodObject



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

def self.index_good
  print "Air quality is considered satisfactory, and air pollution poses little or no risk."
end

.index_hazardousObject



94
95
96
# File 'lib/breathe_in/scraper.rb', line 94

def self.index_hazardous
  print "Health alert: everyone may experience more serious health effects."
end

.index_levelObject



48
49
50
51
52
# File 'lib/breathe_in/scraper.rb', line 48

def self.index_level 
  quality = scraped_pg.css(".AQDataContent .AQILegendText")
  #returns array of objects with attributes including quality
  quality.empty? ? nil : air_quality[:today_index] = quality.first.text
end

.index_moderateObject



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

def self.index_moderate
  print "Air quality is acceptable; however, for some pollutants there may be a moderate health concern for a very small number of people who are unusually sensitive to air pollution."
end

.index_sensitiveObject



82
83
84
# File 'lib/breathe_in/scraper.rb', line 82

def self.index_sensitive
  print "Members of sensitive groups may experience health effects. The general public is not likely to be affected."
end

.index_unhealthyObject



86
87
88
# File 'lib/breathe_in/scraper.rb', line 86

def self.index_unhealthy
  print "Everyone may begin to experience health effects; members of sensitive groups may experience more serious health effects."
end

.index_very_unhealthyObject



90
91
92
# File 'lib/breathe_in/scraper.rb', line 90

def self.index_very_unhealthy
  print "Health warnings of emergency conditions. The entire population is more likely to be affected."
end

.scraped_page(zipcode) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/breathe_in/scraper.rb', line 14

def self.scraped_page(zipcode)
  begin
    @@scraped = Nokogiri::HTML(open("https://airnow.gov/?action=airnow.local_city&zipcode=#{zipcode}&submit=Go"))
  rescue OpenURI::HTTPError => e
      if e.message == '404 Not Found'
        puts "The website is currently down. Pleaes try again later."
      else
        raise e
      end
  end
end

.scraped_pgObject



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

def self.scraped_pg
  @@scraped
end

.today_highObject



42
43
44
45
46
# File 'lib/breathe_in/scraper.rb', line 42

def self.today_high 
  data = scraped_pg.css(".AQDataContent tr td .TblInvisible tr td")
  #returns array of data numbers (today's high, tomorrow's high, particles, etc.)
  data.empty? ? nil : air_quality[:today_high] = data[0].text.strip.to_i
end

.under_maintenanceObject

returns true if under maintenance



126
127
128
# File 'lib/breathe_in/scraper.rb', line 126

def self.under_maintenance #returns true if under maintenance 
  scraped_pg.css("#pageContent .TblInvisibleFixed tr p[style*='color:#F00;']").text.include?("maintenance")
end