lita-onewheel-forecast-io 1.9.1 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33e09546f949c74898eca3909bc15e4bb6356645fe3023c71d2dede103916326
4
- data.tar.gz: 8f78e1ef1893a01f97c66d55a5f5cfa27bc614f9fddde9f63ab328c56c05a858
3
+ metadata.gz: 852d0e1530401241fb6fdfcb48bd96ca52e08f6a09b4d0ecda3133d655e650b4
4
+ data.tar.gz: d616a450faec2c0343a966f52f4bb8eb65b5542a745b66e6f394ddb3924ebc00
5
5
  SHA512:
6
- metadata.gz: 5a7a60d3b098e5301d01e17010b995d55b5f3cf608d3f978dd7a016e82dafeaa7bd3fc6d9c12acef7e2f6d4926d459054cb02245990f7f195a27d74245e65038
7
- data.tar.gz: b31aa8bcad56cd473c73e824b1ff85a5f1b3d2e69446cd18feacac67a54995e92b688bf480c17d6da62b39f0af95920d9f2a6f9102ed2fcc4ac7c16138032c0b
6
+ metadata.gz: 12f61c91537470b93659483b493b0583ae0052da64d081e7c3022b53d5831f0b5ed28ab0700da01e4492007d7bf647cde4d8c8a8a60b6681cce59bdc234250fc
7
+ data.tar.gz: 1bffa394dd2accd79dc33bf7df4876d63d2a460d1867bfa7be2b9ce3766cfc22cb47cb49ad0520a12e7c0d905da63d4076e69a5c4d3bd8c46131b96d9f869189
@@ -5,19 +5,21 @@ module ForecastIo
5
5
  def ascii_rain_forecast(forecast)
6
6
  (str, precip_type) = do_the_rain_chance_thing(forecast, ascii_chars, 'precipProbability')
7
7
  max = get_max_by_data_key(forecast, 'minutely', 'precipProbability')
8
- "1hr #{precip_type} probability #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s} max #{(max.to_f * 100).round(2)}%"
8
+ agg = get_aggregate_by_data_key(forecast, 'minutely', 'precipIntensity')
9
+ "1hr #{precip_type} probability #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s} max #{(max.to_f * 100).round(2)}%, #{get_accumulation agg} accumulation"
9
10
  end
10
11
 
11
12
  def ansi_rain_forecast(forecast)
12
13
  (str, precip_type) = do_the_rain_chance_thing(forecast, ansi_chars, 'precipProbability') #, 'probability', get_rain_range_colors)
13
14
  max = get_max_by_data_key(forecast, 'minutely', 'precipProbability')
14
- "1hr #{precip_type} probability #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s} max #{(max.to_f * 100).round(2)}%"
15
+ agg = get_aggregate_by_data_key(forecast, 'minutely', 'precipIntensity')
16
+ "1hr #{precip_type} probability #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s} max #{(max.to_f * 100).round(2)}%, #{get_accumulation agg} accumulation"
15
17
  end
16
18
 
17
19
  def ansi_rain_intensity_forecast(forecast)
18
20
  (str, precip_type) = do_the_rain_intensity_thing(forecast, ansi_chars, 'precipIntensity') #, 'probability', get_rain_range_colors)
19
- max_str = get_max_by_data_key(forecast, 'minutely', 'precipIntensity')
20
- "1hr #{precip_type} intensity #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s} max: #{max_str}"
21
+ agg = get_aggregate_by_data_key(forecast, 'minutely', 'precipIntensity')
22
+ "1hr #{precip_type} intensity #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s}, #{get_accumulation agg} accumulation"
21
23
  end
22
24
 
23
25
  def ansi_humidity_forecast(forecast)
@@ -44,6 +46,17 @@ module ForecastIo
44
46
  end
45
47
  end
46
48
 
49
+ def get_aggregate_by_data_key(forecast, key, datum)
50
+ unless forecast[key].nil?
51
+ sum = 0
52
+ forecast[key]['data'].each do |data_point|
53
+ Lita.logger.debug "Adding #{data_point[datum]} to #{sum}"
54
+ sum += data_point[datum].to_f
55
+ end
56
+ sum.round(3)
57
+ end
58
+ end
59
+
47
60
  def do_the_rain_chance_thing(forecast, chars, key, use_color = config.colors, minute_limit = nil)
48
61
  if forecast['minutely'].nil?
49
62
  return 'No minute-by-minute data available.'
@@ -410,8 +423,9 @@ module ForecastIo
410
423
  end
411
424
 
412
425
  max = get_max_by_data_key(forecast, 'hourly', 'precipProbability')
426
+ agg = get_aggregate_by_data_key(forecast, 'hourly', 'precipIntensity')
413
427
 
414
- "48 hr #{precip_type}s |#{str}| max #{(max.to_f * 100).round}%"
428
+ "48 hr #{precip_type}s |#{str}| max #{(max.to_f * 100).round}%, #{get_accumulation agg} accumulation"
415
429
  end
416
430
 
417
431
  def do_the_daily_wind_thing(forecast)
@@ -45,13 +45,12 @@ module ForecastIo
45
45
 
46
46
  # Geographical stuffs
47
47
  # Now with moar caching!
48
- def optimistic_geo_wrapper(query)
48
+ def optimistic_geo_wrapper(query, geocoder_key)
49
49
  Lita.logger.debug "Optimistically geo wrapping #{query}!"
50
+ ::Geocoder.configure(
51
+ :api_key => geocoder_key
52
+ )
50
53
  geocoded = nil
51
- # ::Geocoder.configure(
52
- # api_key: config.geocoder_key,
53
- # timeout: 10
54
- # )
55
54
  result = ::Geocoder.search(query)
56
55
  Lita.logger.debug "Geocoder result: '#{result.inspect}'"
57
56
  if result[0]
@@ -63,36 +62,113 @@ module ForecastIo
63
62
  # Perform a geocoder lookup based on a) the query or b) the user's serialized state.
64
63
  # If neither of those exist, default to config location.
65
64
  def geo_lookup(user, query, persist = true)
66
- Lita.logger.debug "Performing geolookup for '#{user.name}' for '#{query}'"
65
+ Lita.logger.debug "Performing geolookup for '#{user}' for '#{query}'"
66
+
67
+ geocoded = nil
67
68
 
68
69
  if query.nil? or query.empty?
69
- Lita.logger.debug "No query specified, pulling from redis #{REDIS_KEY}, #{user.name}"
70
- serialized_geocoded = redis.hget(REDIS_KEY, user.name)
70
+ Lita.logger.debug "No query specified, pulling from redis '#{REDIS_KEY}', '#{user}'"
71
+ serialized_geocoded = redis.hget(REDIS_KEY, user)
71
72
  unless serialized_geocoded == 'null' or serialized_geocoded.nil?
72
- geocoded = JSON.parse(serialized_geocoded)
73
+ if serialized_geocoded[/^http/]
74
+ query = serialized_geocoded
75
+ else
76
+ geocoded = JSON.parse(serialized_geocoded)
77
+ end
78
+ Lita.logger.debug "Cached location: #{geocoded.inspect}"
73
79
  end
74
- Lita.logger.debug "Cached location: #{geocoded.inspect}"
75
80
  end
76
81
 
77
82
  query = (query.nil?)? config.default_location : query
78
83
  Lita.logger.debug "q & g #{query.inspect} #{geocoded.inspect}"
79
84
 
80
- unless geocoded
81
- Lita.logger.debug "Redis hget failed, performing lookup for #{query}"
82
- geocoded = optimistic_geo_wrapper query
83
- Lita.logger.debug "Geolocation found. '#{geocoded.inspect}' failed, performing lookup"
85
+ if query[/^http/] or (!geocoded.nil? and geocoded.key? 'geo') # For now this is aaronpk's loc
86
+ Lita.logger.debug "Getting location from #{query}"
87
+ resp = JSON.parse(RestClient.get query)
88
+
89
+ locality = ''
90
+ if resp['geo']
91
+ locality = resp['geo']['locality']
92
+ end
93
+
94
+ geocoded = optimistic_geo_wrapper locality, config.geocoder_key
95
+
96
+ # loc = Location.new(
97
+ # locality,
98
+ # resp['location']['latitude'],
99
+ # resp['location']['longitude']
100
+ # )
101
+
84
102
  if persist
85
- redis.hset(REDIS_KEY, user.name, geocoded.to_json)
103
+ redis.hset(REDIS_KEY, user, query)
104
+ end
105
+
106
+ else
107
+
108
+ unless geocoded
109
+ # uri = "https://atlas.p3k.io/api/geocode?input=#{URI.escape query}"
110
+ # Lita.logger.debug "Redis hget failed, performing lookup for #{query} on #{uri}"
111
+ geocoded = optimistic_geo_wrapper query, config.geocoder_key
112
+ # Catch network errors here
113
+ # begin
114
+ # geocoded = JSON.parse RestClient.get(uri)
115
+ # rescue RuntimeError => e
116
+ # puts "x"
117
+ # end
118
+
119
+ Lita.logger.debug "Geolocation found. '#{geocoded.inspect}' failed, performing lookup"
120
+ if persist
121
+ redis.hset(REDIS_KEY, user, geocoded.to_json)
122
+ end
86
123
  end
124
+
125
+ # {"latitude": 45.51179,
126
+ # "longitude": -122.67563,
127
+ # "locality": "Portland",
128
+ # "region": "Oregon",
129
+ # "country": "USA",
130
+ # "best_name": "Portland",
131
+ # "full_name": "Portland, Oregon, USA",
132
+ # "postal-code": "97201",
133
+ # "timezone": "America\/Los_Angeles",
134
+ # "offset": "-07:00",
135
+ # "seconds": -25200,
136
+ # "localtime": "2018-08-09T08:05:43-07:00"}
137
+
138
+ # loc = Location.new(
139
+ # geocoded['best_name'],
140
+ # geocoded['latitude'],
141
+ # geocoded['longitude']
142
+ # )
143
+ # loc = Location.new(
144
+ # geocoded['formatted_address'],
145
+ # geocoded['geometry']['location']['lat'],
146
+ # geocoded['geometry']['location']['lng']
147
+ # )
148
+
87
149
  end
88
150
 
89
- Lita.logger.debug "geocoded: '#{geocoded}'"
90
- loc = Location.new(
91
- "#{geocoded['address']['city']}, #{geocoded['address']['state']}",
92
- geocoded['lat'],
93
- geocoded['lon']
94
- )
151
+ Lita.logger.debug "best_name: #{geocoded['best_name']}"
152
+ Lita.logger.debug "display_name: #{geocoded['display_name']}"
153
+ Lita.logger.debug "formatted_address: #{geocoded['formatted_address']}"
154
+ if geocoded['best_name']
155
+ loc = Location.new(
156
+ geocoded['best_name'],
157
+ geocoded['latitude'],
158
+ geocoded['longitude'])
159
+ elsif geocoded['lon']
160
+ loc = Location.new(
161
+ "#{geocoded['address']['city']}, #{geocoded['address']['state']}",
162
+ geocoded['lat'],
163
+ geocoded['lon'])
164
+ else
165
+ loc = Location.new(
166
+ geocoded['formatted_address'],
167
+ geocoded['geometry']['location']['lat'],
168
+ geocoded['geometry']['location']['lng'])
169
+ end
95
170
 
171
+ Lita.logger.debug "geocoded: '#{geocoded}'"
96
172
  Lita.logger.debug "loc: '#{loc}'"
97
173
 
98
174
  loc
@@ -154,6 +230,8 @@ module ForecastIo
154
230
  uri += ",#{time}"
155
231
  end
156
232
 
233
+ uri += "?units=si"
234
+
157
235
  Lita.logger.debug "Requesting forcast data from: #{uri}"
158
236
  set_scale(user)
159
237
  gimme_some_weather uri
@@ -319,6 +397,14 @@ module ForecastIo
319
397
  end
320
398
  end
321
399
 
400
+ def get_accumulation(accum_mm)
401
+ if @scale == 'c' or @scale == 'k'
402
+ accum_mm.round(0).to_s + 'mm'
403
+ else
404
+ inches_from_mm(accum_mm).to_s + 'in'
405
+ end
406
+ end
407
+
322
408
  def get_humidity(humidity_decimal)
323
409
  (humidity_decimal * 100).round(0).to_s + '%'
324
410
  end
@@ -331,6 +417,10 @@ module ForecastIo
331
417
  ((degrees_f.to_f + 459.67) * 5/9).round(2)
332
418
  end
333
419
 
420
+ def inches_from_mm(dist_mm)
421
+ (dist_mm * 0.0393701).round(1)
422
+ end
423
+
334
424
  def kilometers(speed_imperial)
335
425
  (speed_imperial * 1.6).round(2)
336
426
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'lita-onewheel-forecast-io'
3
- spec.version = '1.9.1'
3
+ spec.version = '1.13.0'
4
4
  spec.authors = ['Andrew Kreps']
5
5
  spec.email = ['andrew.kreps@gmail.com']
6
6
  spec.description = <<-EOS
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-onewheel-forecast-io
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.1
4
+ version: 1.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kreps
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-05 00:00:00.000000000 Z
11
+ date: 2019-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita