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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: