File Coverage

blib/lib/Whim/Controller/Display.pm
Criterion Covered Total %
statement 28 31 90.3
branch 4 8 50.0
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 38 47 80.8


line stmt bran cond sub pod time code
1             package Whim::Controller::Display;
2 2     2   7728 use Mojo::Base 'Mojolicious::Controller';
  2         6  
  2         16  
3 2     2   519 use Whim::Mention;
  2         6  
  2         95  
4              
5 2     2   17 use Readonly;
  2         5  
  2         1263  
6             Readonly my $BAD_REQUEST => 400;
7              
8             sub display {
9 1     1 0 8116 my $self = shift;
10              
11 1 50       6 return unless $self->_get_wms;
12              
13 1         10 $self->render('webmentions');
14             }
15              
16             sub summarize {
17 1     1 0 34750 my $self = shift;
18              
19 1 50       4 return unless $self->_get_wms;
20              
21 1         12 $self->render('summary');
22             }
23              
24             sub _get_wms {
25 2     2   5 my $self = shift;
26              
27 2         13 my $url = $self->param('url');
28              
29 2 50       731 unless ($url) {
30 0         0 $self->render(
31             status => $BAD_REQUEST,
32             text => 'No "url" parameter found.',
33             );
34 0         0 return;
35             }
36              
37             # Grab all webmentions, then sort them into a hash-of-lists, keyed on type.
38 2         17 my @webmentions = $self->whim->fetch_webmentions( { target => $url } );
39              
40 2         12 my %webmentions;
41              
42             # Initialize %webmentions with an empty list for each webmention type that
43             # Web::Mention supports (plus one or two extras, perhaps)
44 2         17 foreach (qw( mention reply like repost quotation rsvp bookmark )) {
45 14         35 $webmentions{$_} = [];
46             }
47              
48 2         8 for my $wm (@webmentions) {
49 2 50       51 unless ( $webmentions{ $wm->type } ) {
50 0         0 die "Unknown webmention type: " . $wm->type;
51             }
52 2         58 push $webmentions{ $wm->type }->@*, $wm;
53             }
54              
55 2         27 $self->stash->{webmentions} = \%webmentions;
56 2         26 $self->stash->{webmention_count} = scalar @webmentions;
57              
58 2         20 return 1;
59             }
60              
61             1;