Skip to content

Commit

Permalink
add a /wms route that returns json
Browse files Browse the repository at this point in the history
This is a first, half-hapharded stab at
it, but maybe it can be useful to start
on jmacdotorg#8
  • Loading branch information
yanick committed Jul 17, 2020
1 parent 55adf1c commit a9679f8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/Whim.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sub startup {
$r->get('/')->to('listen#default');
$r->post('/')->to('listen#receive');

$r->get('/wms')->to('display#json');
$r->get('/display_wms')->to('display#display');
$r->get('/summarize_wms')->to('display#summarize');

Expand Down
38 changes: 37 additions & 1 deletion lib/Whim/Controller/Display.pm
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
package Whim::Controller::Display;
use Mojo::Base 'Mojolicious::Controller';
use Whim::Mention;
use List::Util qw/ pairmap /;

use Readonly;
Readonly my $BAD_REQUEST => 400;

sub serialize_wm {
my $wm = shift;

my $data = $wm->TO_JSON;

# TO_JSON doesn't put all the yumminess
# in the hash, so I augment with the
# other stuff I want

if( my $author = $wm->author ) {
$data->{author} = {
map { $_ => $author->$_ } qw/ name url photo /
}
}

if( $wm->author_photo_hash ) {
$data->{author}{local_photo} =
'/author_photos/' . $wm->author_photo_hash;
}

return $data;
}

sub json {
my $self = shift;

return unless $self->_get_wms;

my %mentions = pairmap {
$a => [ map { serialize_wm($_) } @$b ]
} %{ $self->stash->{webmentions} };

$self->render( json => \%mentions );
}

sub display {
my $self = shift;

Expand All @@ -27,7 +63,7 @@ sub _get_wms {
my $url = $self->param('url');

unless ($url) {
$self->render(
$self->render(
status => $BAD_REQUEST,
text => 'No "url" parameter found.',
);
Expand Down

0 comments on commit a9679f8

Please sign in to comment.