Instantly share code, notes, and snippets.

Embed
What would you like to do?
yasumi api
# -*- 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