Created
January 30, 2018 17:36
-
-
Save chrisharrison/c1de4e465c8976a599948f83cb49bdf8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# coding: utf-8 | |
# <bitbar.title>GeoIPWeather</bitbar.title> | |
# <bitbar.version>v0.1.1</bitbar.version> | |
# <bitbar.author>Taylor Zane</bitbar.author> | |
# <bitbar.author.github>taylorzane</bitbar.author.github> | |
# <bitbar.desc>Your weather in the menu bar 🌤</bitbar.desc> | |
# <bitbar.image>http://i.imgur.com/vrT6vfb.png</bitbar.image> | |
# <bitbar.dependencies>ruby</bitbar.dependencies> | |
# <bitbar.abouturl>https://github.com/taylorzane</bitbar.abouturl> | |
### USER VARIABLES | |
UNITS = 'C' # This can be: (F)ahrenheit, (C)elsius, (K)elvin | |
API_KEY = '8b4824b451d5db1612156837df880f55' # you can also get your own at http://openweathermap.org/ | |
LOCATION = 'Exeter, UK' | |
require 'json' | |
require 'net/http' | |
def no_data(message = nil) | |
if message | |
puts message | |
else | |
puts 'Cannot get weather.' | |
end | |
exit | |
end | |
def weather(location) | |
temperature_unit = | |
case UNITS.upcase | |
when 'F' | |
'&units=imperial' | |
when 'C' | |
'&units=metric' | |
else | |
'' | |
end | |
temperature_symbol = | |
case UNITS.upcase | |
when 'F' | |
'℉' | |
when 'C' | |
'℃' | |
else | |
'K' | |
end | |
weather_uri = | |
URI('http://api.openweathermap.org/data/2.5/weather' \ | |
"?q=#{location}" \ | |
"&appid=#{API_KEY}" \ | |
"#{temperature_unit}") | |
weather_data = Net::HTTP.get(weather_uri) | |
no_data unless weather_data | |
weather_json = JSON.parse weather_data | |
no_data weather_json['message'] if weather_json['cod'] == '404' | |
temperature = weather_json['main']['temp'].round | |
puts "#{temperature}#{temperature_symbol} (#{location})" | |
end | |
weather(*LOCATION) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment