Class: BreatheIn::CLI

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

Constant Summary collapse

@@zipcode =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.zipcodeObject



5
6
7
# File 'lib/breathe_in/cli.rb', line 5

def self.zipcode
  @@zipcode
end

Instance Method Details

#assign_attributes(new_city) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/breathe_in/cli.rb', line 96

def assign_attributes(new_city)
  attributes = BreatheIn::Scraper.city_air_quality

  attributes[:today_high] = "Data currently unavailable." if !attributes.has_key?(:today_high)
  attributes[:today_index] = "Data currently unavailable." if !attributes.has_key?(:today_index)
  attributes[:last_update_value] = "Data currently unavailable." if !attributes.has_key?(:last_update_value)
  attributes[:last_update_time] = "Data currently unavailable." if !attributes.has_key?(:last_update_time)
  attributes[:last_update_index] = "Data currently unavailable." if !attributes.has_key?(:last_update_index)  

  city_info_hash = new_city.add_city_air_quality(attributes)
end

#check_site_availabilityObject

def get_information

get_zipcode
@city = BreatheIn::City.find_by_zipcode({zipcode: self.class.zipcode})
scrape_data BreatheIn::City.new({zipcode: self.class.zipcode})
if BreatheIn::Scraper.city_name == nil
  puts "That zipcode is not recognized by AirNow.gov."
  get_information
else
  new_city =  BreatheIn::City.new({zipcode: self.class.zipcode})
  assign_attributes(new_city)
  display_information
end

end



45
46
47
48
49
50
51
52
53
# File 'lib/breathe_in/cli.rb', line 45

def check_site_availability
  if BreatheIn::Scraper.under_maintenance
    disclaimer = <<-Ruby
      ***AirNow.gov undergoes maintenance from midnight to 4am EST. 
      If information is currently unavailable, please try again later.***
      Ruby
    puts disclaimer
  end
end

#display_informationObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/breathe_in/cli.rb', line 108

def display_information
  BreatheIn::City.cities.each do |city|
    puts "---------------------"
    puts "City/Area: #{city.city_name}, Zipcode: #{city.zipcode}"
    puts "---------------------"
    puts "Today's High AQI: #{city.today_high}"
    puts "Today's Index: #{city.today_index}"
    health_description(city.today_high) if city.today_high.is_a?(Integer)
    puts "---------------------"
    puts "Last #{city.last_update_time}"
    puts "Current AQI: #{city.last_update_value}"
    puts "Current Index: #{city.last_update_index}"
    health_description(city.last_update_value) if city.last_update_value.is_a?(Integer) 
     puts "---------------------"      
  end
end

#get_informationObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/breathe_in/cli.rb', line 18

def get_information
  get_zipcode
  scrape_data
  if BreatheIn::Scraper.city_name == nil
    puts "That zipcode is not recognized by AirNow.gov."
    get_information
  else
    new_city = BreatheIn::City.new({zipcode: self.class.zipcode})
    assign_attributes(new_city)
    display_information
  end
end

#get_zipcodeObject



82
83
84
85
86
87
88
89
90
# File 'lib/breathe_in/cli.rb', line 82

def get_zipcode
  input = ""
  until input.match(/\b\d{5}\b/)
    puts "Please enter a valid zipcode and wait a few seconds:"
    puts ""
    input = gets.strip
  end
  @@zipcode = input.to_s.rjust(5, '0')
end

#health_description(level) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/breathe_in/cli.rb', line 125

def health_description(level)
    if level.between?(0,50)
      puts "#{BreatheIn::Scraper.index_good}"
    elsif level.between?(51,100)
      puts "#{BreatheIn::Scraper.index_moderate}"
    elsif level.between?(100,150)
      puts "#{BreatheIn::Scraper.index_sensitive}"
    elsif level.between?(151,200)
      puts "#{BreatheIn::Scraper.index_unhealthy}"
    elsif level.between?(201,300)
      puts "#{BreatheIn::Scraper.index_very_unhealthy}"
    elsif level.between?(301,500)
      puts "#{BreatheIn::Scraper.index_hazardous}"   
    end
end


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/breathe_in/cli.rb', line 55

def menu
  input = nil

  while input != 3
    puts ""
    puts "1. Learn more about the AQI values and the ranges."
    puts "2. Choose another zipcode."
    puts "3. Exit."
    puts "Please make a selection:"
    puts ""
    
    input = gets.strip.to_i
    case input
      when 1
        BreatheIn::Scraper.AQI_range_information
      when 2
        BreatheIn::City.reset
        get_information
        check_site_availability
      when 3
        puts "Breathe safely!"
      else
        puts "Please choose 1, 2, or 3."
    end
  end
end

#runObject



9
10
11
12
13
14
15
16
# File 'lib/breathe_in/cli.rb', line 9

def run
  puts "*Data provided courtesy of AirNow.gov*"
  puts "How safe it is to breathe today?"
  puts ""
  get_information
  check_site_availability
  menu
end

#scrape_dataObject



92
93
94
# File 'lib/breathe_in/cli.rb', line 92

def scrape_data
  BreatheIn::Scraper.scraped_page(self.class.zipcode)
end