| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of App-SpreadRevolutionaryDate | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2019-2026 by Gérald Sédrati. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | package App::SpreadRevolutionaryDate::MsgMaker::PromptUser; | ||||||||||||||||||||||||||||
| 13 | 2 | $App::SpreadRevolutionaryDate::MsgMaker::PromptUser::VERSION = '0.54'; | |||||||||||||||||||||||||||
| 14 | # ABSTRACT: MsgMaker class for L<App::SpreadRevolutionaryDate> to build message by prompting user | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | with 'App::SpreadRevolutionaryDate::MsgMaker'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use open qw(:std :encoding(UTF-8)); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use IO::Prompt::Hooked; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use File::Temp qw/tempfile/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use Locale::TextDomain 'App-SpreadRevolutionaryDate'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | has 'default' => ( | |||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | isa => 'Str', | ||||||||||||||||||||||||||||
| 32 | required => 1, | ||||||||||||||||||||||||||||
| 33 | default => 'Goodbye old world, hello revolutionary worlds', | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | has 'img_path' => ( | |||||||||||||||||||||||||||
| 37 | is => 'rw', | ||||||||||||||||||||||||||||
| 38 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 39 | default => '', | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | has 'img_alt' => ( | |||||||||||||||||||||||||||
| 43 | is => 'rw', | ||||||||||||||||||||||||||||
| 44 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 45 | default => '', | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | has 'img_url' => ( | |||||||||||||||||||||||||||
| 49 | is => 'rw', | ||||||||||||||||||||||||||||
| 50 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 51 | default => '', | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 6 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.8
| |||||||||||||||||||||||||||||
| 55 | 6 | my ($orig, $class, %args) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 6 | ▶ | if ($args{locale}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | # Get sure locale has .mo file | ||||||||||||||||||||||||||||
| 59 | 6 | my ($volume, $directory, $file) = File::Spec->splitpath(__FILE__); | |||||||||||||||||||||||||||
| 60 | 6 | my $locale_mo = File::Spec->catfile($directory, '..', '..', '..', 'LocaleData', $args{locale}, 'LC_MESSAGES', 'App-SpreadRevolutionaryDate.mo'); | |||||||||||||||||||||||||||
| 61 | 6 | ▶ | $args{locale} = 'en' unless -f $locale_mo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Do not pass default => undef to force default in attribute definition | ||||||||||||||||||||||||||||
| 65 | delete $args{default} | ||||||||||||||||||||||||||||
| 66 | 6 | ▶ | if exists $args{default} && !defined $args{default}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args{"default"} and not defined $args{"default"}
MC/DC0%0/2
exists $args{"default"} and not defined $args{"default"}
exists $args{"default"}
not defined $args{"default"}
Truth table33%1/3
A and not(B)
A= exists $args{"default"}B= not defined $args{"default"}
| |||||||||||||||||||||||||||||
| 67 | 6 | return $class->$orig(%args); | |||||||||||||||||||||||||||
| 68 | 2 | }; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 6 | ▶ | sub compute { | ||||||||||||||||||||||||||
|
Subroutine
compute: called CC=12 SCAR=33.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | my $question = __"Please, enter message to spread"; | |||||||||||||||||||||||||||
| 75 | #TRANSLATORS: initial used in case insensitive context | ||||||||||||||||||||||||||||
| 76 | 6 | my $confirm_ok = lc(substr(__("yes"), 0, 1)); | |||||||||||||||||||||||||||
| 77 | #TRANSLATORS: initial used case insensitive context | ||||||||||||||||||||||||||||
| 78 | 6 | my $confirm_nok = lc(substr(__("no"), 0, 1)); | |||||||||||||||||||||||||||
| 79 | #TRANSLATORS: initial used in case sensitive context | ||||||||||||||||||||||||||||
| 80 | 6 | my $confirm_abort = substr(__("Abort"), 0, 1); | |||||||||||||||||||||||||||
| 81 | 6 | my $confirm_abort_text = __x("or {abort} to abort", abort => $confirm_abort); | |||||||||||||||||||||||||||
| 82 | 6 | my $confirm_intro = __"Spread"; | |||||||||||||||||||||||||||
| 83 | 6 | my $confirm_question = __x("confirm ({confirm_ok}/{confirm_nok} {confirm_abort_text})?", confirm_ok => $confirm_ok, confirm_nok => $confirm_nok, confirm_abort_text => $confirm_abort_text); | |||||||||||||||||||||||||||
| 84 | 6 | my $confirm_error = __x("Input must be \"{confirm_ok}\" or \"{confirm_nok}\"\n", confirm_ok => $confirm_ok, confirm_nok => $confirm_nok); | |||||||||||||||||||||||||||
| 85 | 6 | my $abort = __"OK not spreading"; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | ▶ | if ($self->img_path) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $self->img_alt(ucfirst(fileparse($self->img_path, qr/\.[^.]*/))) unless $self->img_alt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $confirm_question = __x("with image file"). ' ' . $self->{img_path} . ' (alt:' . $self->img_alt . '), ' . $confirm_question; | |||||||||||||||||||||||||||
| 90 | } elsif ($self->img_url) { | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $self->img_alt(ucfirst(fileparse($self->img_url, qr/\.[^.]*/))) unless $self->img_alt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $confirm_question = __x("with image from url:"). ' ' . $self->{img_url} . ' (alt:' . $self->img_alt . '), ' . $confirm_question; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 6 | my $confirm = $confirm_nok; | |||||||||||||||||||||||||||
| 96 | 6 | my $msg; | |||||||||||||||||||||||||||
| 97 | 6 | ▶ | while (defined $confirm && $confirm !~ qr($confirm_ok)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $confirm and not $confirm =~ qr/$confirm_ok/u
MC/DC50%1/2
defined $confirm and not $confirm =~ qr/$confirm_ok/u
defined $confirm
not $confirm =~ qr/$confirm_ok/u
Truth table66%2/3
A and not(B)
A= defined $confirmB= not $confirm =~ qr/$confirm_ok/u
| |||||||||||||||||||||||||||||
| 98 | 6 | $msg = prompt($question, $self->default); | |||||||||||||||||||||||||||
| 99 | 6 | $confirm = prompt( | |||||||||||||||||||||||||||
| 100 | message => $confirm_intro . ' "' . $msg . '", ' . $confirm_question, | ||||||||||||||||||||||||||||
| 101 | default => $confirm_ok, | ||||||||||||||||||||||||||||
| 102 | validate => qr/^[$confirm_ok$confirm_nok]$/i, | ||||||||||||||||||||||||||||
| 103 | escape => qr/^$confirm_abort$/, | ||||||||||||||||||||||||||||
| 104 | error => $confirm_error, | ||||||||||||||||||||||||||||
| 105 | tries => 2, | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 6 | ▶ | die "$abort\n" unless defined $confirm && $confirm =~ qr($confirm_ok); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $confirm and $confirm =~ qr/$confirm_ok/u
MC/DC0%0/2
defined $confirm and $confirm =~ qr/$confirm_ok/u
defined $confirm
$confirm =~ qr/$confirm_ok/u
Truth table33%1/3
A and B
A= defined $confirmB= $confirm =~ qr/$confirm_ok/u
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 6 | ▶ | if ($self->img_path) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 111 | 0 | return ($msg, {path => $self->img_path, alt => $self->img_alt}); | |||||||||||||||||||||||||||
| 112 | } elsif ($self->img_url) { | ||||||||||||||||||||||||||||
| 113 | 0 | my $ua = LWP::UserAgent->new(env_proxy => 1, timeout => 10, agent =>'App::SpreadRevolutionaryDate bot'); | |||||||||||||||||||||||||||
| 114 | 0 | my $response = $ua->get($self->img_url); | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | die "Cannot download image from " . $self->img_url . ": " . $response->status_line . "\n" unless $response->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my ($fh, $filename) = tempfile(UNLINK => 1); | |||||||||||||||||||||||||||
| 118 | 0 | print $fh $response->content; | |||||||||||||||||||||||||||
| 119 | 0 | close $fh; | |||||||||||||||||||||||||||
| 120 | 0 | return ($msg, {path => $filename, alt => $self->img_alt}); | |||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||
| 122 | 6 | return ($msg, undef); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # A module must return a true value. Traditionally, a module returns 1. | ||||||||||||||||||||||||||||
| 131 | # But this module is a revolutionary one, so it discards all old traditions. | ||||||||||||||||||||||||||||
| 132 | # Idea borrowed from Jean Forget's DateTime::Calendar::FrenchRevolutionary. | ||||||||||||||||||||||||||||
| 133 | 2 | "Quand le gouvernement viole les droits du peuple, | |||||||||||||||||||||||||||
| 134 | l'insurrection est pour le peuple le plus sacré | ||||||||||||||||||||||||||||
| 135 | et le plus indispensable des devoirs"; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||