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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/whisper/blog.rb', line 46
def install_default_routes! emailer=nil
common_opts = { :config => @config, :router => @router, :helper => @helper, :extra_deps => [@entryset, @commentset], :entryset => @entryset }
@router.add_handler :index, :get, "/index/:page:format" do |route, page, format, params|
debug "index handler called for page #{page.inspect} format #{format.inspect}"
Page.new common_opts.merge({ :format => format, :url_vars => { :route => route },
:master_template => template_for("master", format),
:template => template_for("list", format) }) do
pages = paginate :entries_by_page, @entryset.timestamp, @entryset.entries
entries = pages[page] or raise InvalidPageError
= entries
{ :entries => entries.zip(), :pages => pages.size, :page => page,
:recent_comments => recent_comments_and_entries, :route => route
}
end
end
@router.add_handler :label, :get, "/label/+label/:page:format" do |route, label, page, format, params|
debug "label handler called for label #{label.inspect} page #{page.inspect} format #{format.inspect}"
Page.new common_opts.merge({:format => format, :url_vars => { :route => route, :label => label },
:master_template => template_for("master", format),
:template => template_for("list", format) }) do
raw_entries = @entryset.entries_by_label[label] or raise InvalidPageError
pages = paginate [:entries_by_label, label], @entryset.timestamp, raw_entries
entries = pages[page] or raise InvalidPageError
= entries
{ :entries => entries.zip(), :pages => pages.size, :page => page,
:recent_comments => recent_comments_and_entries, :route => route, :label => label
}
end
end
@router.add_handler :author, :get, "/by/+author/:page:format" do |route, author, page, format, params|
debug "author handler called for author #{author.inspect} page #{page.inspect} format #{format.inspect}"
Page.new common_opts.merge({:format => format, :url_vars => { :route => route, :author => author },
:master_template => template_for("master", format),
:template => template_for("list", format) }) do
pages = paginate [:entries_by_author, author], @entryset.timestamp, @entryset.entries_by_author[author]
entries = pages[page] or raise InvalidPageError
= entries
{ :entries => entries.zip(), :pages => pages.size, :page => page,
:recent_comments => recent_comments_and_entries, :route => route, :author => author
}
end
end
@router.add_handler :root, :get, "/:format" do |route, format, params|
debug "root handler called with format #{format}"
Page.new common_opts.merge({ :format => format, :url_vars => { :route => :index },
:template => template_for("list", format),
:master_template => template_for("master", format) }) do
pages = paginate :entries_by_page, @entryset.timestamp, @entryset.entries
entries = pages[0]
= entries
{ :entries => entries.zip(), :pages => pages.size, :page => 0,
:recent_comments => recent_comments_and_entries, :route => route
}
end
end
if emailer
@router.add_handler :comment, :post, "/comment/+id" do |route, id, params|
debug "request comment handler called for id #{id} with params #{params.inspect}"
begin
emailer.handle_send_request id, params if emailer
["email sent!", "text/plain"]
rescue EmailSender::Error => e
["error: #{e.message}", "text/plain"]
end
end
end
@router.add_handler :entry, :get, "/+id:format" do |route, id, format, params|
debug "entry handler called for id #{id.inspect} format #{format.inspect}"
Page.new common_opts.merge({ :format => format, :url_vars => { :route => route, :id => id },
:template => template_for("entry", format),
:master_template => template_for("master", format)
}) do
raise InvalidPageError unless (entry = @entryset.entries_by_id[id])
{ :entry => entry, :comments => (@commentset.[entry.id] || {}),
:recent_comments => recent_comments_and_entries, :route => route,
:author_info => @authors
}
end
end
end
|