swdyh (owner)

Revisions

gist: 69693 Download_button fork
public
Public Clone URL: git://gist.github.com/69693.git
Embed All Files: show embed
d_ap.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- 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>