| 1 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | App::LinkSite::Link | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | (You probably want to just look at the L<linksite> application.) | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | A class to model a link on a link site (part of App::LinkSite). | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 18 | ▶ | use Feature::Compat::Class; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 18 | class App::LinkSite::Link { | |||||||||||||||||||||||||||||||
| 18 | 18 | our $VERSION = '0.2.0'; | |||||||||||||||||||||||||||||||
| 19 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 18 | ▶ | no if $] >= 5.038, 'warnings', 'experimental::class'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 18 | ▶ | use URI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 18 | ▶ | use URI::Escape qw(uri_escape_utf8 uri_unescape); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 18 | field $title :reader :param; | |||||||||||||||||||||||||||||||
| 27 | 18 | field $subtitle :reader :param = ''; | |||||||||||||||||||||||||||||||
| 28 | 18 | field $link :reader :param; | |||||||||||||||||||||||||||||||
| 29 | 18 | field $new_link :reader(is_new) :param(new) = 0; | |||||||||||||||||||||||||||||||
| 30 | 18 | field $utm :param = undef; | |||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 128 | ▶ | ADJUST { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||
| 33 | 128 | ▶ | if ($utm) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 37 | ▶ | die "utm must be an object or false\n" unless ref $utm eq 'HASH'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | 35 | for my $key (keys %$utm) { | |||||||||||||||||||||||||||||||
| 36 | 26 | ▶ | die "Invalid UTM parameter '$key'; use utm_* names\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | unless $key =~ /\Autm_[a-z][a-z0-9_]*\z/; | ||||||||||||||||||||||||||||||||
| 38 | die "UTM parameter '$key' must be a string, number, or null\n" | ||||||||||||||||||||||||||||||||
| 39 | 25 | ▶ | if defined $utm->{$key} && ref $utm->{$key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $utm->{$key} and ref $utm->{$key}
MC/DC100%2/2
defined $utm->{$key} and ref $utm->{$key}
defined $utm->{$key}
ref $utm->{$key}
Truth table100%3/3
A and B
A= defined $utm->{$key}B= ref $utm->{$key}
| |||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | =head2 tracked_link | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | Returns the destination used for clicks. An optional C<utm> constructor | ||||||||||||||||||||||||||||||||
| 49 | parameter enables tracking when it is a hash reference, even an empty one. | ||||||||||||||||||||||||||||||||
| 50 | Defaults are C<utm_source=linksite>, C<utm_medium=referral>, | ||||||||||||||||||||||||||||||||
| 51 | C<utm_campaign=profile>, and C<utm_content> from a slug of the link title. | ||||||||||||||||||||||||||||||||
| 52 | Slugs use lowercase Unicode letters and numbers, with other runs replaced | ||||||||||||||||||||||||||||||||
| 53 | by hyphens and leading/trailing hyphens removed. Empty slugs are omitted. | ||||||||||||||||||||||||||||||||
| 54 | Supplied values override them; null or empty values omit a parameter. | ||||||||||||||||||||||||||||||||
| 55 | False or undefined C<utm> leaves the destination unchanged. | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | Only absolute HTTP(S) URLs are tagged. Existing query parameters, including | ||||||||||||||||||||||||||||||||
| 58 | UTM values, and fragments are preserved. C<link> still returns the original | ||||||||||||||||||||||||||||||||
| 59 | destination, which is also used for JSON-LD. | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 57 | ▶ | method tracked_link { | ||||||||||||||||||||||||||||||
|
Subroutine
tracked_link: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 64 | 57 | ▶ | return $link unless $utm; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | 43 | my $uri = URI->new($link); | |||||||||||||||||||||||||||||||
| 66 | 43 | ▶ | return $link unless ($uri->scheme // '') =~ /\Ahttps?\z/i && $uri->host; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
($uri->scheme // "") =~ /\Ahttps?\z/i and $uri->host
$uri->scheme // ""
MC/DC100%2/2
($uri->scheme // "") =~ /\Ahttps?\z/i and $uri->host
$uri->scheme
$uri->host
Truth table100%3/3
A and B
A= $uri->schemeB= $uri->host
| |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 35 | ▶ | my $content = lc($title // ''); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$title // ""
MC/DC0%0/1
$title // ""
$title
Truth table50%1/2
A
A= $title
| |||||||||||||||||||||||||||||||||
| 69 | 35 | $content =~ s/[^\p{L}\p{N}]+/-/g; | |||||||||||||||||||||||||||||||
| 70 | 35 | $content =~ s/\A-|-\z//g; | |||||||||||||||||||||||||||||||
| 71 | 35 | my %parameters = ( | |||||||||||||||||||||||||||||||
| 72 | utm_source => 'linksite', utm_medium => 'referral', | ||||||||||||||||||||||||||||||||
| 73 | utm_campaign => 'profile', utm_content => $content, %$utm, | ||||||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||||||
| 75 | 35 | my %existing; | |||||||||||||||||||||||||||||||
| 76 | 35 | ▶ | for my $pair (split /[&;]/, $uri->query // '') { | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$uri->query // ""
MC/DC100%1/1
$uri->query // ""
$uri->query
Truth table100%2/2
A
A= $uri->query
| |||||||||||||||||||||||||||||||||
| 77 | 10 | ▶ | next unless length $pair; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 8 | my ($key) = split /=/, $pair, 2; | |||||||||||||||||||||||||||||||
| 79 | 8 | $key =~ tr/+/ /; | |||||||||||||||||||||||||||||||
| 80 | 8 | $existing{uri_unescape($key)} = 1; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 35 | my @added; | |||||||||||||||||||||||||||||||
| 83 | 35 | for my $key (sort keys %parameters) { | |||||||||||||||||||||||||||||||
| 84 | 142 | ▶ | next if exists $existing{$key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 139 | ▶ | next unless defined $parameters{$key} && length $parameters{$key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $parameters{$key} and length $parameters{$key}
MC/DC100%2/2
defined $parameters{$key} and length $parameters{$key}
defined $parameters{$key}
length $parameters{$key}
Truth table100%3/3
A and B
A= defined $parameters{$key}B= length $parameters{$key}
| |||||||||||||||||||||||||||||||||
| 86 | 125 | push @added, uri_escape_utf8($key) . '=' . uri_escape_utf8($parameters{$key}); | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | 35 | ▶ | return $link unless @added; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | # Append to the original query so its ordering, encoding, and duplicates survive. | ||||||||||||||||||||||||||||||||
| 91 | 34 | my ($base, $fragment) = split /#/, $link, 2; | |||||||||||||||||||||||||||||||
| 92 | 34 | ▶ | my $separator = index($base, '?') < 0 ? '?' : $base =~ /[?&]\z/ ? '' : '&'; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 93 | 34 | ▶ | return $base . $separator . join('&', @added) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | . (defined $fragment ? "#$fragment" : ''); | ||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | =head2 mk_link | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | Returns a fragment of HTML that is used to display this link on the site. | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | It's actually an `<li>` that wraps an `<a>`. So this method probably needs | ||||||||||||||||||||||||||||||||
| 102 | to be renamed or refactored. Maybe it should just be called `render()`. | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 17 | ▶ | method mk_link { | ||||||||||||||||||||||||||||||
|
Subroutine
mk_link: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 107 | 17 | my $destination = $self->tracked_link; | |||||||||||||||||||||||||||||||
| 108 | 17 | ▶ | if ($utm) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | 10 | $destination =~ s/&/&/g; | |||||||||||||||||||||||||||||||
| 110 | 10 | $destination =~ s/"/"/g; | |||||||||||||||||||||||||||||||
| 111 | 10 | $destination =~ s/</</g; | |||||||||||||||||||||||||||||||
| 112 | 10 | $destination =~ s/>/>/g; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | 17 | my $a_tag = q[<a href="] . $destination . q[" class="text-decoration-none">] . $self->title . q[</a>]; | |||||||||||||||||||||||||||||||
| 115 | 17 | ▶ | my $subtitle = $self->subtitle ? q[<br><small class="text-muted">] . $self->subtitle . q[</small>] : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 17 | my @li_classes = qw[list-group-item list-group-item-action]; | |||||||||||||||||||||||||||||||
| 118 | 17 | ▶ | push @li_classes, 'new-link' if $self->is_new; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 17 | return qq[<li class='@li_classes'>$a_tag$subtitle</li>]; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | Dave Cross <dave@davecross.co.uk> | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | Copyright (c) 2024, Magnum Solutions Ltd. All Rights Reserved. | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | This library is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 133 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 18 | 1; | |||||||||||||||||||||||||||||||