# -*- coding: utf-8 -*-
require 'open-uri'
require 'pathname'
require 'erb'
require 'rubygems'
require 'json'
require 'sinatra'
include ERB::Util
def get_siteinfo
url = 'http://wedata.net/databases/AutoPagerize/items.json'
cache = Pathname.pwd.join 'items.json'
if cache.exist?
json = IO.read cache
else
json = open(url).read
puts "get #{url}"
open(cache, 'w') { |f| f.write json }
end
JSON.parse json
end
get '/' do
siteinfo = get_siteinfo.sort_by { |i| i['name'] }
lis = siteinfo.map do |i|
id = i['resource_url'].split('/').last
erb :list, :locals => { :item => i, :id => id }
end
erb :index, :locals => { :lis => lis.join }
end
get '/:id.:format' do
siteinfo = get_siteinfo
i = siteinfo.find do |i|
i['resource_url'].split('/').last == params[:id]
end
if i
locals = { :item => i, :id => params[:id] }
case params[:format]
when 'xml'
content_type 'application/xml', :charset => 'utf-8'
erb :xml, :locals => locals
else
erb :html, :locals => locals
end
else
'e'
end
end
use_in_file_templates!
__END__
@@ index
<html>
<body>
<h1><a href="/">SITEINFO for D-AutoPagerize</a></h1>
<ul>
<%= lis %>
</ul>
</body>
</html>
@@ list
<li><a href="/<%=h id %>.html"><%=h item['name'] %></a></li>
@@ html
<html>
<head><link rel="siteinfo" type="application/xml" href="/<%=h id %>.xml"></head>
<body>
<h1><a href="/">D-AutoPagerize SITEINFO</a></h1>
<h2><%=h item['name'] %></h2>
<p><a href="/<%=h id %>.xml">siteinfo</a></p>
<dl>
<% item['data'].each do |k, v| %>
<dt><%=h k %></dt>
<dd><%=h v %></dd>
<% end %>
</dl>
</body>
</html>
@@ xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<si:siteinfo xmlns:si="urn:uuid:e3f93d05-291f-4b47-b15c-e6dc99663d03">
<si:name><%=h item['name'] %></si:name>
<si:created_by><%=h item['created_by'] %></si:created_by>
<si:created_at><%=h item['created_at'] %></si:created_at>
<si:updated_at><%=h item['updated_at'] %></si:updated_at>
<si:group xmlns="http://userscripts.org/scripts/show/8551">
<% item['data'].each do |k, v| %><<%=h k %>><%=h v %></<%=h k %>><% end %>
</si:group>
</si:siteinfo>