blib/lib/POE/Component/IRC/Plugin/WWW/Weather/US.pm

filter: Statementstmt 44.1% 19 / 43 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 46.1% 6 / 13 Podpod 0.0% 0 / 4 totaltotal 37.8% 25 / 66 CC 4 SCAR 13.9
CC
4
Cov
39%
S_public
12.0
_get_weather
6.0
new
2.0
CRAP 4.0
↑ Summary
1 package POE::Component::IRC::Plugin::WWW::Weather::US;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Cache::Memory::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.04';
12  
13 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
14 0 my $class = shift;
15 0 my $self = bless {
16 cache => Cache::Memory::Simple->new,
17 }, $class;
18 0 return $self;
19 }
20  
21 0 ▶sub PCI_register {
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
22 0 my ($self, $irc) = splice @_, 0, 2;
23  
24 0 $irc->plugin_register($self, 'SERVER', qw(public));
25 0 return 1;
26 }
27  
28 # This is method is mandatory but we don't actually have anything to do.
29 0 ▶sub PCI_unregister {
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
30 0 return 1;
31 }
32  
33 0 ▶sub S_public {
Subroutine S_public: not called CC=3 SCAR=24.8
Pod undocumented
34 0 my ($self, $irc) = splice @_, 0, 2;
35  
36 # Parameters are passed as scalar-refs including arrayrefs.
37 0 my $nick = (split /!/, ${$_[0]})[0];
38 0 my $channel = ${$_[1]}->[0];
39 0 my $msg = ${$_[2]};
40  
41 0 ▶ if (my ($zip) = $msg =~ /^!weather\s+(\d{5})/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($zip) = $msg =~ /^!weather\s+(\d{5})/i) 0 0 0
42 0 my $reply = $self->_get_weather($zip);
43 0 ▶ $irc->yield(privmsg => $channel => "$nick: $reply") if $reply;
Branch: 1 branch
BranchTrueFalseTotal
if $reply 0 0 0
44 0 return PCI_EAT_PLUGIN;
45 }
46  
47 # Default action is to allow other plugins to process it.
48 0 return PCI_EAT_NONE;
49 }
50  
51 # the link I use for zip redirects, so set max_redirects to 1
52 0 ▶sub _get_weather {
Subroutine _get_weather: not called CC=2 SCAR=17.9
53 0 ▶ my ($self, $zip) = @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
my($self, $zip) = or return 0 0 0
54 0 $self->{cache}->purge(); # purge any expired items
55 return $self->{cache}->get_or_set(
56 $zip,
57 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 Mojo::UserAgent->new->max_redirects(1)
59 ->get("http://forecast.weather.gov/zipcity.php?inputstring=$zip")
60 ->res->dom->find('.point-forecast-7-day .row-odd')
61 0 ▶ ->map(sub { $_->find('span')->pluck('text') . ': ' . $_->text })->[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 },
63 0 3600 # cache for 1 hour
64 );
65 }
66  
67 1 1;
68 __END__
↑ Summary