lita-onewheel-aqi 0.1.2 → 0.2.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 +4 -4
- data/lib/lita/handlers/onewheel_aqi.rb +66 -31
- data/lita-onewheel-aqi.gemspec +1 -1
- data/spec/lita/handlers/onewheel_aqi_spec.rb +30 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1596bc749ff6ad46b497452b7f4cf8b503bd58a3
|
4
|
+
data.tar.gz: 2fe48f69fafa46b60b95b28dc98f7237ee43ab1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1151dfe8c05ba826c6f0faf31ce6d33bfd8b0e2e8adbdf69fbf7115e97a1d52ac86779e8e080c95108b36d528069f738c46f050e1eb5c7a744f75ed7bfb06ddb
|
7
|
+
data.tar.gz: 9242e9b374b9603ca4be44be5f5690d4b03e5e89c7e56e2c7ce9df9206f006ea2f848d8c225e30382f790df694d82da226a5da8b8de6e8e9acaf9d02510fdf8a
|
@@ -10,7 +10,37 @@ module Lita
|
|
10
10
|
route /^aqi(.*)$/i,
|
11
11
|
:get_aqi,
|
12
12
|
command: true,
|
13
|
-
help: {'!aqi [location]' => 'Gives you available data for air quality (PM2.5) forecast and latest observation.'}
|
13
|
+
help: { '!aqi [location]' => 'Gives you available data for air quality (PM2.5) forecast and latest observation.' }
|
14
|
+
|
15
|
+
# IRC colors.
|
16
|
+
def colors
|
17
|
+
{ white: '00',
|
18
|
+
black: '01',
|
19
|
+
blue: '02',
|
20
|
+
green: '03',
|
21
|
+
red: '04',
|
22
|
+
brown: '05',
|
23
|
+
purple: '06',
|
24
|
+
orange: '07',
|
25
|
+
yellow: '08',
|
26
|
+
lime: '09',
|
27
|
+
teal: '10',
|
28
|
+
aqua: '11',
|
29
|
+
royal: '12',
|
30
|
+
pink: '13',
|
31
|
+
grey: '14',
|
32
|
+
silver: '15' }
|
33
|
+
end
|
34
|
+
|
35
|
+
# Based on the temp in F.
|
36
|
+
def aqi_range_colors
|
37
|
+
{ 0..50 => :green,
|
38
|
+
51..100 => :yellow,
|
39
|
+
101..150 => :orange,
|
40
|
+
151..200 => :red,
|
41
|
+
201..300 => :purple,
|
42
|
+
301..500 => :pink }
|
43
|
+
end
|
14
44
|
|
15
45
|
def get_aqi(response)
|
16
46
|
location = response.matches[0][0].to_s.strip
|
@@ -21,24 +51,26 @@ module Lita
|
|
21
51
|
puts loc.inspect
|
22
52
|
|
23
53
|
parameters = {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
54
|
+
latitude: loc[:lat],
|
55
|
+
longitude: loc[:lng],
|
56
|
+
api_key: config.api_key,
|
57
|
+
format: 'application/json'
|
28
58
|
}
|
29
59
|
|
30
|
-
query_string = (parameters.map { |k, v| "#{k}=#{v}" }).join
|
60
|
+
query_string = (parameters.map { |k, v| "#{k}=#{v}" }).join '&'
|
31
61
|
uri = 'http://airnowapi.org/aq/forecast/latLong/?' + query_string
|
32
62
|
Lita.logger.debug "Getting aqi from #{uri}"
|
33
|
-
forecast_response = RestClient.get(uri)
|
34
|
-
forecasted_aqi = JSON.parse(forecast_response)
|
35
|
-
Lita.logger.debug 'Forecast response: ' + forecasted_aqi.inspect
|
63
|
+
# forecast_response = RestClient.get(uri)
|
64
|
+
# forecasted_aqi = JSON.parse(forecast_response)
|
65
|
+
# Lita.logger.debug 'Forecast response: ' + forecasted_aqi.inspect
|
36
66
|
|
37
|
-
|
67
|
+
uri = 'http://airnowapi.org/aq/observation/latLong/current/?' + query_string
|
68
|
+
Lita.logger.debug "Getting aqi observed from #{uri}"
|
69
|
+
observed_response = RestClient.get(uri)
|
38
70
|
observed_aqi = JSON.parse(observed_response)
|
39
71
|
Lita.logger.debug 'Observed response: ' + observed_aqi.inspect
|
40
72
|
|
41
|
-
if
|
73
|
+
if observed_aqi.nil? # forecasted_aqi.nil? and
|
42
74
|
response.reply "No AQI data for #{loc[:name]}."
|
43
75
|
Lita.logger.info "No data found for #{response.matches[0][0]}"
|
44
76
|
return
|
@@ -46,25 +78,33 @@ module Lita
|
|
46
78
|
|
47
79
|
# [{"DateObserved":"2015-08-23 ","HourObserved":14,"LocalTimeZone":"PST","ReportingArea":"Portland","StateCode":"OR","Latitude":45.538,"Longitude":-122.656,"ParameterName":"O3","AQI":49,"Category":{"Number":1,"Name":"Good"}},{"DateObserved":"2015-08-23 ","HourObserved":14,"LocalTimeZone":"PST","ReportingArea":"Portland","StateCode":"OR","Latitude":45.538,"Longitude":-122.656,"ParameterName":"PM2.5","AQI":167,"Category":{"Number":4,"Name":"Unhealthy"}}]
|
48
80
|
# todo: extract today's forecast, not tomorrow's.
|
49
|
-
forecasted_aqi = extract_pmtwofive(forecasted_aqi)
|
81
|
+
# forecasted_aqi = extract_pmtwofive(forecasted_aqi)
|
50
82
|
observed_aqi = extract_pmtwofive(observed_aqi)
|
51
83
|
|
52
84
|
reply = "AQI for #{loc[:name]}, "
|
53
|
-
unless forecasted_aqi == []
|
54
|
-
|
55
|
-
end
|
85
|
+
# unless forecasted_aqi == []
|
86
|
+
# reply += "Forecasted: #{(forecasted_aqi['ActionDay'] == 'true')? 'Action Day! ' : ''}#{forecasted_aqi['AQI']} #{forecasted_aqi['Category']['Name']} "
|
87
|
+
# end
|
56
88
|
unless observed_aqi == []
|
57
|
-
reply += "Observed: #{observed_aqi['AQI']} #{observed_aqi['Category']['Name']} at #{observed_aqi['DateObserved']}#{observed_aqi['HourObserved']}00 hours."
|
89
|
+
reply += "Observed: #{color_str(observed_aqi['AQI'])} #{observed_aqi['Category']['Name']} at #{observed_aqi['DateObserved']}#{observed_aqi['HourObserved']}00 hours."
|
58
90
|
end
|
59
91
|
response.reply reply
|
60
92
|
end
|
61
93
|
|
94
|
+
def color_str(str)
|
95
|
+
aqi_range_colors.keys.each do |color_key|
|
96
|
+
if color_key.cover? str.to_i # Super secred cover sauce
|
97
|
+
color = colors[color_key]
|
98
|
+
str = "\x03#{color}#{str}\x03"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
str
|
102
|
+
end
|
103
|
+
|
62
104
|
def extract_pmtwofive(aqi)
|
63
105
|
returned_aqi = aqi
|
64
106
|
aqi.each do |a|
|
65
|
-
if a['ParameterName'] == 'PM2.5'
|
66
|
-
returned_aqi = a
|
67
|
-
end
|
107
|
+
returned_aqi = a if a['ParameterName'] == 'PM2.5'
|
68
108
|
end
|
69
109
|
returned_aqi
|
70
110
|
end
|
@@ -74,26 +114,21 @@ module Lita
|
|
74
114
|
def optimistic_geo_wrapper(query)
|
75
115
|
geocoded = nil
|
76
116
|
result = ::Geocoder.search(query)
|
77
|
-
|
78
|
-
|
79
|
-
end
|
117
|
+
puts result.inspect
|
118
|
+
geocoded = result[0].data if result[0]
|
80
119
|
geocoded
|
81
120
|
end
|
82
121
|
|
83
122
|
def geo_lookup(query)
|
84
|
-
puts query.inspect
|
123
|
+
puts "Location lookup #{query.inspect}"
|
85
124
|
geocoded = optimistic_geo_wrapper query
|
86
125
|
|
87
|
-
if (query.nil?
|
88
|
-
query = 'Portland, OR'
|
89
|
-
end
|
126
|
+
query = 'Portland, OR' if (query.nil? || query.empty?) && geocoded.nil?
|
90
127
|
|
91
|
-
{name: geocoded['formatted_address'],
|
92
|
-
|
93
|
-
|
94
|
-
}
|
128
|
+
{ name: geocoded['formatted_address'],
|
129
|
+
lat: geocoded['geometry']['location']['lat'],
|
130
|
+
lng: geocoded['geometry']['location']['lng'] }
|
95
131
|
end
|
96
|
-
|
97
132
|
end
|
98
133
|
|
99
134
|
Lita.register_handler(OnewheelAqi)
|
data/lita-onewheel-aqi.gemspec
CHANGED
@@ -1,5 +1,34 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Lita::Handlers::OnewheelAqi, lita_handler: true do
|
4
4
|
it { is_expected.to route_command('aqi') }
|
5
|
+
|
6
|
+
before do
|
7
|
+
mock = File.open('spec/fixtures/Output.json').read
|
8
|
+
allow(RestClient).to receive(:get) { mock }
|
9
|
+
|
10
|
+
Geocoder.configure(lookup: :test)
|
11
|
+
|
12
|
+
Geocoder::Lookup::Test.add_stub(
|
13
|
+
'Portland, OR', [{
|
14
|
+
'formatted_address' => 'Portland, OR, USA',
|
15
|
+
'geometry' => {
|
16
|
+
'location' => {
|
17
|
+
'lat' => 45.523452,
|
18
|
+
'lng' => -122.676207,
|
19
|
+
'address' => 'Portland, OR, USA',
|
20
|
+
'state' => 'Oregon',
|
21
|
+
'state_code' => 'OR',
|
22
|
+
'country' => 'United States',
|
23
|
+
'country_code' => 'US'
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}]
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'queries the aqi' do
|
31
|
+
send_command 'aqi'
|
32
|
+
expect(replies.last).to include("AQI for Portland, OR, USA, Observed: \u0003118\u0003 Unhealthy for Sensitive Groups at 2017-08-09 1100 hours.")
|
33
|
+
end
|
5
34
|
end
|