Skip to content

Commit 9402e34

Browse files
committed
add a /wms route that returns json
This is a first, half-hapharded stab at it, but maybe it can be useful to start on #8
1 parent 55adf1c commit 9402e34

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/Whim.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ sub startup {
6161
$r->get('/')->to('listen#default');
6262
$r->post('/')->to('listen#receive');
6363

64+
$r->get('/wms')->to('display#json');
6465
$r->get('/display_wms')->to('display#display');
6566
$r->get('/summarize_wms')->to('display#summarize');
6667

lib/Whim/Controller/Display.pm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
11
package Whim::Controller::Display;
22
use Mojo::Base 'Mojolicious::Controller';
33
use Whim::Mention;
4+
use List::Util qw/ pairmap /;
45

56
use Readonly;
67
Readonly my $BAD_REQUEST => 400;
78

9+
sub serialize_wm {
10+
my $wm = shift;
11+
12+
my $data = $wm->TO_JSON;
13+
14+
# TO_JSON doesn't put all the yumminess
15+
# in the hash, so I augment with the
16+
# other stuff I want
17+
18+
if( my $author = $wm->author ) {
19+
$data->{author} = {
20+
map { $_ => $author->$_ } qw/ name url photo /
21+
}
22+
}
23+
24+
if( $wm->author_photo_hash ) {
25+
$data->{author}{local_photo} =
26+
'/author_photos/' . $wm->author_photo_hash;
27+
}
28+
29+
return $data;
30+
}
31+
32+
sub json {
33+
my $self = shift;
34+
35+
return unless $self->_get_wms;
36+
37+
my %mentions = pairmap {
38+
$a => [ map { serialize_wm($_) } @$b ]
39+
} %{ $self->stash->{webmentions} };
40+
41+
$self->render( json => \%mentions );
42+
}
43+
844
sub display {
945
my $self = shift;
1046

0 commit comments

Comments
 (0)