Created
October 6, 2011 16:41
-
-
Save ultraist/1267899 to your computer and use it in GitHub Desktop.
yasumi 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
# -*- coding: utf-8 -*- | |
require 'rubygems' | |
require 'json' | |
require 'open-uri' | |
require 'rss' | |
require 'date' | |
require 'cgi' | |
API_URL="http://www.google.com/calendar/feeds/japanese__ja%%40holiday.calendar.google.com/public/basic?start-min=%04d-%02d-%02d&start-max=%04d-%02d-%02d" | |
today = Date.today | |
yday = today + 1 | |
message = nil | |
if (today.wday == 6) | |
message = "休みです。土曜日です。" | |
elsif (today.wday == 0) | |
message = "日曜日で休みです。" | |
else | |
url = sprintf(API_URL, today.year, today.month, today.day, | |
yday.year, yday.month, yday.day) | |
open(url) do |f| | |
rss = RSS::Parser.parse(f.read) | |
rss.items.each do |item| | |
message = item.title.content + "。祝日です。" | |
end | |
end | |
end | |
unless (message) | |
message = [nil, "休みじゃない", "平日", "まだ平日", "はあ平日", "平日", nil][today.wday] | |
yasumi = false | |
else | |
yasumi = true | |
end | |
cgi = CGI.new | |
json = {'yasumi' => yasumi}.to_json | |
if (cgi['format'] == 'json') | |
cgi.out("content-type" => "application/json") do | |
json | |
end | |
else | |
cgi.out("content-type" => "text/html; charset=utf-8") do | |
<<HTML | |
<html> | |
<head><title>休みAPI</title></head> | |
<body> | |
<h1> | |
#{CGI.escapeHTML(message)} | |
</h1> | |
<div> | |
<a href="?format=json">#{CGI.escapeHTML(json)}</a> | |
</div> | |
<address style="margin-top:1em"> | |
powered by <a href="https://gist.github.com/1267899">yasumi.rb</a> | |
</address> | |
</body> | |
</html> | |
HTML | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment