Commit | Line | Data |
cefad154 |
1 | package Promulger::Web; |
2 | use Web::Simple 'Promulger::Web'; |
3 | use autodie; |
4 | |
5 | use Promulger::List; |
6 | |
7 | use Method::Signatures::Simple; |
8 | |
9 | has list_home => ( |
10 | is => 'ro', |
11 | default => sub { 'etc/lists' }, |
12 | lazy => 1, |
13 | ); |
14 | |
15 | sub dispatch_request { |
16 | my ($self) = shift; |
17 | sub (GET + /list/) { |
18 | redispatch_to '/index'; |
19 | }, |
20 | sub (GET + /list) { |
21 | redispatch_to '/index'; |
22 | }, |
23 | sub (GET + /) { |
24 | redispatch_to '/index'; |
25 | }, |
26 | sub (GET + /index) { |
27 | my ($self) = @_; |
28 | [ 200, [ 'Content-type', 'text/html' ], [ $self->render_index(@_) ] ] |
29 | }, |
30 | sub (GET + /list/*/) { |
31 | my ($self, $list) = @_; |
32 | redispatch_to "/list/${list}"; |
33 | }, |
34 | sub (GET + /list/*) { |
35 | my ($self, $list) = @_; |
36 | [ 200, [ 'Content-type', 'text/html' ], [ $self->show_list($list) ] ] |
37 | }, |
38 | sub (GET + /list/*/subscriber/*) { |
39 | my ($self, $list, $subscriber) = @_; |
40 | [ |
41 | 200, |
42 | [ 'Content-type', 'text/html' ], |
43 | [ $self->show_subscriber($list, $subscriber) ] |
44 | ] |
45 | }, |
46 | sub (GET + /list/*/subscriber/*/unsubscribe) { |
47 | my ($self, $list, $subscriber) = @_; |
48 | [ |
49 | 200, |
50 | [ 'Content-type', 'text/html' ], |
51 | [ $self->unsubscribe($list, $subscriber) ] |
52 | ] |
53 | }, |
54 | sub (POST + /list/*/subscribe + %email=) { |
55 | my ($self, $list, $email) = @_; |
56 | [ |
57 | 200, |
58 | [ 'Content-type', 'text/html' ], |
59 | [ $self->subscribe($list, $email) ], |
60 | ] |
61 | }, |
62 | sub () { |
63 | [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ] |
64 | } |
65 | } |
66 | |
67 | method render_index { |
68 | my ($self) = @_; |
69 | my @lists = Promulger::List->get_lists; |
70 | my $html = <<HTML; |
71 | <p>Promulger</p> |
72 | |
73 | <p>Lists:</p> |
74 | <ul> |
75 | HTML |
76 | for my $list (@lists) { |
77 | $html .= qq{<li><a href="/list/${list}">${list}</a></li>}; |
78 | } |
79 | $html .= "</ul>"; |
80 | return $html; |
81 | } |
82 | |
83 | method show_list($list_name) { |
84 | my $list = Promulger::List->resolve($list_name); |
85 | my $name = $list->listname; |
86 | my $active = $list->active; |
87 | my @subscribers = keys %{ $list->subscribers }; |
88 | my $html = <<"HTML"; |
89 | <p>List: ${name}</p> |
90 | <p>Active: ${active}</p> |
91 | <form method="POST" action="/list/${list_name}/subscribe"> |
92 | <input type="text" name="email"> |
93 | </form> |
94 | <p>Subscribers:</p> |
95 | <ul> |
96 | HTML |
97 | for my $sub (@subscribers) { |
98 | $html .= qq{<li><a href="/list/${name}/subscriber/${sub}">${sub}</a></li>}; |
99 | } |
100 | $html .= "</ul>"; |
101 | return $html; |
102 | } |
103 | |
104 | method subscribe($list_name, $email) { |
105 | my $list = Promulger::List->resolve($list_name); |
106 | $list->subscribe($email); |
107 | return "<p>Subscribed ${email} to ${list_name}.</p>"; |
108 | } |
109 | |
110 | method unsubscribe($list_name, $email) { |
111 | my $list = Promulger::List->resolve($list_name); |
112 | $list->unsubscribe($email); |
113 | return "<p>Unsubscribed ${email} from ${list_name}.</p>"; |
114 | } |
115 | |
116 | method show_subscriber($list_name, $subscriber) { |
117 | my $html = <<"HTML"; |
118 | <p>Subscriber ${subscriber}</p> |
119 | <a href="/list/${list_name}/subscriber/${subscriber}/unsubscribe">unsubscribe</a> |
120 | HTML |
121 | } |
122 | |
123 | 1; |