Skip to content

Instantly share code, notes, and snippets.

@hkraji
Last active April 14, 2016 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkraji/5c3c2b04eb498ac4c1a787682bbeed8a to your computer and use it in GitHub Desktop.
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
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
Copy link

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:incall'
/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:inblock 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:inblock in trigger_job'

@hkraji
Copy link
Author

hkraji commented Apr 6, 2016

@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?

@travelling-man
Copy link

@hkraji it works. I use the variable API KEY in the following format: API_KEY = 'xxxxxxxxxxxxxxx'

@bindrelie
Copy link

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