Created
March 30, 2014 06:27
-
-
Save cnosuke/9868550 to your computer and use it in GitHub Desktop.
This file contains 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
WEATHER_API = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=130010'.freeze | |
require 'json' | |
require 'open-uri' | |
module Filter | |
class WeatherBot | |
def update(p) | |
return nil unless p['text'] =~ /^\:weather/ | |
{ | |
username: "Weather", | |
text: forecast | |
} | |
end | |
def weather | |
@weather ||= JSON.parse(open(WEATHER_API).read) | |
end | |
def forecast | |
f = weather['forecasts'] | |
"...\n" + f.map{ |e| forecast_day(e) }.join | |
end | |
def forecast_day(d) | |
str =<<EOB | |
#{d["dateLabel"]}(#{d["date"]}): #{d["telop"]} | |
温度: #{temperature(d["temperature"])} | |
EOB | |
end | |
def temperature(d) | |
d.map{ |k,v| | |
if v | |
"#{k}: #{v['celsius']}度" | |
end | |
}.compact.join(", ") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment