breathe_in 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e600df3235dc0b4b8236ecc7de2b4d5d1b5857e0
4
+ data.tar.gz: f7deae9f7d4ddce9b6c63d1ce8640a8a48358a1f
5
+ SHA512:
6
+ metadata.gz: e3973d6f42a3c62330ad33335f906f17b4e9f65f31e4383103d504f512fafb31bd616cb1cf453ca540fff406d26a08dced9ea02743b1506038421306b4164136
7
+ data.tar.gz: 5f0672a788a09006b1280a261d89304ec75f0255b001bad82df4326af4125559a3610e1584e94fe9f18fb658da7df9535910b402977760e18912c204d6d45a85
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in breathe_in.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 eric-an
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/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # BreatheIn
2
+
3
+ Use this gem to find out the Air Quality Index (AQI) value for any zipcode in United States and see how clean or polluted the air is in your city. Data provided by AirNow.gov.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'breathe_in'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install breathe_in
20
+
21
+ ## Usage
22
+
23
+ Run `breathe_in` and follow the prompts.
24
+
25
+ ## Development
26
+
27
+ 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.
28
+
29
+ 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).
30
+
31
+ ## License
32
+
33
+ MIT License. Copyright 2016 Eric An.
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eric-an/breathe_in.
38
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/breathe_in ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "breathe_in"
4
+
5
+ BreatheIn::CLI.new.run
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "breathe_in"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'breathe_in/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'breathe_in'
8
+ spec.version = BreatheIn::VERSION
9
+ spec.authors = ['eric-an']
10
+ spec.email = ['erican@me.com']
11
+
12
+ spec.summary = %q{AQI scraper gem}
13
+ spec.description = %q{Check the air quality index (AQI) for a zipcode.}
14
+ spec.homepage = "https://github.com/eric-an/breathe-in"
15
+ spec.license = 'MIT'
16
+ spec.executables << 'breathe_in'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'bin'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 0"
26
+ spec.add_development_dependency "pry"
27
+
28
+ spec.add_dependency "nokogiri"
29
+ end
@@ -0,0 +1,7 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'pry'
4
+
5
+ require_relative "../lib/breathe_in/scraper"
6
+ require_relative "../lib/breathe_in/city"
7
+ require_relative "../lib/breathe_in/cli"
@@ -0,0 +1,23 @@
1
+ class BreatheIn::City
2
+ attr_accessor :city_name, :zipcode, :today_high, :today_index, :last_update_time, :last_update_value, :last_update_index
3
+
4
+ @@cities = []
5
+
6
+ def initialize(city_hash={})
7
+ city_hash.each { |key, value| self.send(("#{key}="), value) }
8
+ @@cities << self
9
+ end
10
+
11
+ def add_city_air_quality(air_quality_hash)
12
+ air_quality_hash.each { |key, value| self.send(("#{key}="), value) }
13
+ self
14
+ end
15
+
16
+ def self.cities
17
+ @@cities
18
+ end
19
+
20
+ def self.reset
21
+ @@cities.clear
22
+ end
23
+ end
@@ -0,0 +1,126 @@
1
+ class BreatheIn::CLI
2
+
3
+ @@zipcode = nil
4
+
5
+ def self.zipcode
6
+ @@zipcode
7
+ end
8
+
9
+ def run
10
+ puts "*Data provided courtesy of AirNow.gov*"
11
+ puts "How safe it is to breathe today?"
12
+ puts ""
13
+ get_information
14
+ check_site_availability
15
+ menu
16
+ end
17
+
18
+ def get_information
19
+ get_zipcode
20
+ scrape_data
21
+ if BreatheIn::Scraper.city_name == nil
22
+ puts "That zipcode is not recognized by Air.gov."
23
+ get_information
24
+ else
25
+ new_city = BreatheIn::City.new({zipcode: self.class.zipcode})
26
+ assign_attributes(new_city)
27
+ display_information
28
+ end
29
+ end
30
+
31
+ def check_site_availability
32
+ if BreatheIn::Scraper.under_maintenance
33
+ disclaimer = <<-Ruby
34
+ ***AirNow.gov undergoes maintenance from midnight to 4am EST.
35
+ If information is currently unavailable, please try again later.***
36
+ Ruby
37
+ puts disclaimer
38
+ end
39
+ end
40
+
41
+ def menu
42
+ input = nil
43
+
44
+ while input != 3
45
+ puts ""
46
+ puts "1. Learn more about the AQI values and the ranges."
47
+ puts "2. Choose another zipcode."
48
+ puts "3. Exit."
49
+ puts "Please make a selection:"
50
+ puts ""
51
+
52
+ input = gets.strip.to_i
53
+ case input
54
+ when 1
55
+ BreatheIn::Scraper.AQI_range_information
56
+ when 2
57
+ BreatheIn::City.reset
58
+ get_information
59
+ check_site_availability
60
+ when 3
61
+ puts "Breathe safely!"
62
+ else
63
+ puts "Please choose 1, 2, or 3."
64
+ end
65
+ end
66
+ end
67
+
68
+ def get_zipcode
69
+ input = ""
70
+ until input.match(/\b\d{5}\b/)
71
+ puts "Please enter a valid zipcode and wait a few seconds:"
72
+ puts ""
73
+ input = gets.strip
74
+ end
75
+ @@zipcode = input.to_s.rjust(5, '0')
76
+ end
77
+
78
+ def scrape_data
79
+ BreatheIn::Scraper.scraped_page(self.class.zipcode)
80
+ end
81
+
82
+ def assign_attributes(new_city)
83
+ attributes = BreatheIn::Scraper.city_air_quality
84
+
85
+ attributes[:today_high] = "Data currently unavailable." if !attributes.has_key?(:today_high)
86
+ attributes[:today_index] = "Data currently unavailable." if !attributes.has_key?(:today_index)
87
+ attributes[:last_update_value] = "Data currently unavailable." if !attributes.has_key?(:last_update_value)
88
+ attributes[:last_update_time] = "Data currently unavailable." if !attributes.has_key?(:last_update_time)
89
+ attributes[:last_update_index] = "Data currently unavailable." if !attributes.has_key?(:last_update_index)
90
+
91
+ city_info_hash = new_city.add_city_air_quality(attributes)
92
+ end
93
+
94
+ def display_information
95
+ BreatheIn::City.cities.each do |city|
96
+ puts "---------------------"
97
+ puts "City/Area: #{city.city_name}, Zipcode: #{city.zipcode}"
98
+ puts "---------------------"
99
+ puts "Today's High AQI: #{city.today_high}"
100
+ puts "Today's Index: #{city.today_index}"
101
+ health_description(city.today_high) if city.today_high.is_a?(Integer)
102
+ puts "---------------------"
103
+ puts "Last #{city.last_update_time}"
104
+ puts "Current AQI: #{city.last_update_value}"
105
+ puts "Current Index: #{city.last_update_index}"
106
+ health_description(city.last_update_value) if city.last_update_value.is_a?(Integer)
107
+ puts "---------------------"
108
+ end
109
+ end
110
+
111
+ def health_description(level)
112
+ if level.between?(0,50)
113
+ puts "#{BreatheIn::Scraper.index_good}"
114
+ elsif level.between?(51,100)
115
+ puts "#{BreatheIn::Scraper.index_moderate}"
116
+ elsif level.between?(100,150)
117
+ puts "#{BreatheIn::Scraper.index_sensitive}"
118
+ elsif level.between?(151,200)
119
+ puts "#{BreatheIn::Scraper.index_unhealthy}"
120
+ elsif level.between?(201,300)
121
+ puts "#{BreatheIn::Scraper.index_very_unhealthy}"
122
+ elsif level.between?(301,500)
123
+ puts "#{BreatheIn::Scraper.index_hazardous}"
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,134 @@
1
+ class BreatheIn::Scraper
2
+
3
+ @@air_quality_info = {}
4
+ @@scraped = nil
5
+
6
+ def self.air_quality
7
+ @@air_quality_info
8
+ end
9
+
10
+ def self.scraped_pg
11
+ @@scraped
12
+ end
13
+
14
+ def self.scraped_page(zipcode)
15
+ begin
16
+ @@scraped = Nokogiri::HTML(open("http://airnow.gov/?action=airnow.local_city&zipcode=#{zipcode}&submit=Go"))
17
+ rescue OpenURI::HTTPError => e
18
+ if e.message == '404 Not Found'
19
+ puts "The website is currently down. Pleaes try again later."
20
+ else
21
+ raise e
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.city_air_quality
27
+ city_name
28
+ today_high
29
+ index_level
30
+ current_conditions_time
31
+ current_conditions_value
32
+ current_conditions_index
33
+ air_quality
34
+ end
35
+
36
+ def self.city_name
37
+ city = scraped_pg.css("#pageContent .ActiveCity")
38
+ #returns array of an object with attributes including city name
39
+ city.empty? ? nil : air_quality[:city_name] = city.text.strip
40
+ end
41
+
42
+ def self.today_high
43
+ data = scraped_pg.css(".AQDataContent tr td .TblInvisible tr td")
44
+ #returns array of data numbers (today's high, tomorrow's high, particles, etc.)
45
+ data.empty? ? nil : air_quality[:today_high] = data[0].text.strip.to_i
46
+ end
47
+
48
+ def self.index_level
49
+ quality = scraped_pg.css(".AQDataContent .AQILegendText")
50
+ #returns array of objects with attributes including quality
51
+ quality.empty? ? nil : air_quality[:today_index] = quality.first.text
52
+ end
53
+
54
+ def self.current_conditions_time
55
+ unformatted_info = scraped_pg.css(".AQData .AQDataSectionTitle .AQDataSectionTitle")
56
+ #returns array of objects with attributes of title and date information
57
+ current_info = unformatted_info.text.strip.split("\r\n \t")
58
+ # => returns array ["Air Quality Index (AQI)", "observed at 19:00 PST"]
59
+ unformatted_info.empty? ? nil : air_quality[:last_update_time] = current_info[1]
60
+ end
61
+
62
+ def self.current_conditions_value
63
+ current_value = scraped_pg.css(".AQDataSectionTitle .TblInvisible .TblInvisible td")
64
+ #returns array of an object with attributes including current condition value
65
+ current_value.empty? ? nil : air_quality[:last_update_value] = current_value.text.strip.to_i
66
+ end
67
+
68
+ def self.current_conditions_index
69
+ current_index = scraped_pg.css(".AQDataSectionTitle .TblInvisible .AQDataLg")
70
+ #returns array of an object with attributes including current condition index
71
+ current_index.empty? ? nil : air_quality[:last_update_index] = current_index.text.strip
72
+ end
73
+
74
+ def self.index_good
75
+ print "Air quality is considered satisfactory, and air pollution poses little or no risk."
76
+ end
77
+
78
+ def self.index_moderate
79
+ 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."
80
+ end
81
+
82
+ def self.index_sensitive
83
+ print "Members of sensitive groups may experience health effects. The general public is not likely to be affected."
84
+ end
85
+
86
+ def self.index_unhealthy
87
+ print "Everyone may begin to experience health effects; members of sensitive groups may experience more serious health effects."
88
+ end
89
+
90
+ def self.index_very_unhealthy
91
+ print "Health warnings of emergency conditions. The entire population is more likely to be affected."
92
+ end
93
+
94
+ def self.index_hazardous
95
+ print "Health alert: everyone may experience more serious health effects."
96
+ end
97
+
98
+ def self.AQI_range_information
99
+ information = <<-Ruby
100
+ 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.
101
+ 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.
102
+
103
+ "Good" AQI is 0 - 50.
104
+ Air quality is considered satisfactory, and air pollution poses little or no risk.
105
+ ***************************
106
+ "Moderate" AQI is 51 - 100.
107
+ 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.
108
+ ***************************
109
+ "Unhealthy for Sensitive Groups" AQI is 101 - 150.
110
+ 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.
111
+ ***************************
112
+ "Unhealthy" AQI is 151 - 200.
113
+ Everyone may begin to experience some adverse health effects, and members of the sensitive groups may experience more serious effects.
114
+ ***************************
115
+ "Very Unhealthy" AQI is 201 - 300.
116
+ This would trigger a health alert signifying that everyone may experience more serious health effects.
117
+ ***************************
118
+ "Hazardous" AQI greater than 300.
119
+ This would trigger a health warnings of emergency conditions. The entire population is more likely to be affected.
120
+ ***************************
121
+ All descriptions, information, and data are provided courtesy of AirNow.gov. Visit the website to learn more.
122
+ Ruby
123
+ puts information
124
+ end
125
+
126
+ def self.under_maintenance #returns true if under maintenance
127
+ scraped_pg.css("#pageContent .TblInvisibleFixed tr p[style*='color:#F00;']").text.include?("maintenance")
128
+ end
129
+
130
+ # def self.unavailable_data #returns true if data is unavailable
131
+ # phrase = scraped_pg.css(".TblInvisibleFixed .AQData tr td[valign*='top']").text
132
+ # phrase.include?("Data Not Available")
133
+ # end
134
+ end
@@ -0,0 +1,3 @@
1
+ module BreatheIn
2
+ VERSION = "0.1.0"
3
+ end
data/lib/breathe_in.rb ADDED
@@ -0,0 +1,4 @@
1
+ module BreatheIn
2
+ end
3
+
4
+ require_relative "../config/environment"
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: breathe_in
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - eric-an
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-20 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Check the air quality index (AQI) for a zipcode.
84
+ email:
85
+ - erican@me.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/breathe_in
98
+ - bin/console
99
+ - bin/setup
100
+ - breathe_in.gemspec
101
+ - config/environment.rb
102
+ - lib/breathe_in.rb
103
+ - lib/breathe_in/city.rb
104
+ - lib/breathe_in/cli.rb
105
+ - lib/breathe_in/scraper.rb
106
+ - lib/breathe_in/version.rb
107
+ homepage: https://github.com/eric-an/breathe-in
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.4.6
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: AQI scraper gem
131
+ test_files: []