Last active
April 14, 2016 16:22
-
-
Save hkraji/5c3c2b04eb498ac4c1a787682bbeed8a to your computer and use it in GitHub Desktop.
Improvement for gist https://gist.github.com/davefp/4990174 switching to Open Weather API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
# you can find CITY_ID here http://bulk.openweathermap.org/sample/city.list.json.gz | |
CITY_ID = 2172517 | |
# options: metric / imperial | |
UNITS = 'metric' | |
# create free account on open weather map to get API key | |
API_KEY = ENV['WEATHER_KEY'] | |
SCHEDULER.every '60s', :first_in => 0 do |job| | |
http = Net::HTTP.new('api.openweathermap.org') | |
response = http.request(Net::HTTP::Get.new("/data/2.5/weather?id=#{CITY_ID}&units=#{UNITS}&appid=#{API_KEY}")) | |
next unless '200'.eql? response.code | |
weather_data = JSON.parse(response.body) | |
detailed_info = weather_data['weather'].first | |
send_event('weather', { :temp => "#{weather_data['main']['temp'].to_f.round} °#{temperature_units}", | |
:condition => detailed_info['main'], | |
:title => "#{weather_data['name']} Weather", | |
:climacon => climacon_class(detailed_info['id'])}) | |
end | |
def temperature_units | |
'metric'.eql?(UNITS) ? 'C' : 'K' | |
end | |
# fun times ;) legend: http://openweathermap.org/weather-conditions | |
def climacon_class(weather_code) | |
case weather_code.to_s | |
when /800/ | |
'sun' | |
when /80./ | |
'cloud' | |
when /2.*/ | |
'lightning' | |
when /3.*/ | |
'drizzle' | |
when /5.*/ | |
'rain' | |
when /6.*/ | |
'snow' | |
else | |
'sun' | |
end | |
end |
@travelling-man Just put next
instead of return
. But still it looks like you are getting non 200
response, did you register and got API key?
@hkraji it works. I use the variable API KEY in the following format: API_KEY = 'xxxxxxxxxxxxxxx'
Hey, thanks for the revision, it works well for me. The only update I'd have is that the climacons weren't all correct for me, I came up with this for the climacon_class function:
def climacon_class(weather_code)
case weather_code.to_i
when 200..232
'lightning'
when 300..321
'drizzle'
when 500..531
'rain'
when 600..602
'snow'
when 611..622
'sleet'
when 701..731
'haze'
when 741
'fog'
when 751..762
'haze'
when 771
'rain'
when 781
'tornado'
when 800
'sun'
when 801..803
'cloud sun'
when 804
'cloud'
when 900
'tornado'
when 901..902
'lightning'
when 903
'thermometer low'
when 904
'thermometer full'
when 905
'wind'
when 906
'hail'
when 951
'sun'
when 952..959
'wind'
when 960..962
'lightning'
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I got the following exception, can you help me?
scheduler caught exception:
unexpected return
/var/dashboard/jobs/weather.rb:17:in
block in <top (required)>' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in
call'/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in
trigger_block' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in
block in trigger'/var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in
call' /var/lib/gems/1.9.1/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in
block in trigger_job'