| 1 | # http://blogs.perl.org/users/sawyer_x/2010/08/distzilla-strictures-tip.html | ||||||||||
| 2 | 5 | ▶ | use strictures 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package WebService::NotifyMyAndroid; | ||||||||||
| 5 | { | ||||||||||
| 6 | 5 | $WebService::NotifyMyAndroid::VERSION = '0.0.6'; | |||||||||
| 7 | } | ||||||||||
| 8 | { | ||||||||||
| 9 | 5 | $WebService::NotifyMyAndroid::DIST = 'WebService-NotifyMyAndroid'; | |||||||||
| 10 | } | ||||||||||
| 11 | 5 | ▶ | use base qw( WebService::Simple ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | binmode STDOUT, ":encoding(UTF-8)"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 5 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 5 | ▶ | use Params::Validate qw( :all ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 5 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 5 | ▶ | use Regexp::Common qw( number ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | # make sure we have the https protocol available to us; | ||||||||||
| 21 | # we need this to do anything this the API url | ||||||||||
| 22 | 5 | ▶ | use LWP::Protocol::https; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | # Module implementation here | ||||||||||
| 25 | |||||||||||
| 26 | # constants | ||||||||||
| 27 | 5 | Readonly my $NMA_URL => 'https://www.notifymyandroid.com/publicapi/'; | |||||||||
| 28 | |||||||||||
| 29 | # string lengths in characters | ||||||||||
| 30 | 5 | Readonly my $KEYLENGTH => 48; | |||||||||
| 31 | 5 | Readonly my $APPLENGTH => 256; | |||||||||
| 32 | 5 | Readonly my $EVENTLENGTH => 1000; | |||||||||
| 33 | 5 | Readonly my $DESCLENGTH => 10000; | |||||||||
| 34 | |||||||||||
| 35 | # validation regexes | ||||||||||
| 36 | ## we're using the 'weird' format for setting flags because strictures | ||||||||||
| 37 | ## complains about 'use of multidimensional array emulation' with the | ||||||||||
| 38 | ## "{-base => 16}" syntax | ||||||||||
| 39 | ## for more information read the documentation for strictures, | ||||||||||
| 40 | ## multidimensional and Regexp::Common (Flag syntax) | ||||||||||
| 41 | 5 | Readonly my $KEYREGEX => $RE{num}{int}{"-base$;16"}{"-places$;$KEYLENGTH"}; | |||||||||
| 42 | 5 | Readonly my $APPREGEX => ".{1,$APPLENGTH}"; | |||||||||
| 43 | 5 | Readonly my $EVENTREGEX => ".{1,$EVENTLENGTH}"; | |||||||||
| 44 | 5 | Readonly my $DESCREGEX => ".{1,$DESCLENGTH}"; | |||||||||
| 45 | 5 | Readonly my $PRIOREGEX => "(?:-?[12]+|0)"; | |||||||||
| 46 | |||||||||||
| 47 | # NMA-specific configuration | ||||||||||
| 48 | 5 | __PACKAGE__->config( | |||||||||
| 49 | base_url => $NMA_URL, | ||||||||||
| 50 | response_parser => 'XML::Simple', | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | # public functions | ||||||||||
| 54 | |||||||||||
| 55 | 5 | my %verify_spec = ( | |||||||||
| 56 | apikey => { | ||||||||||
| 57 | type => SCALAR, | ||||||||||
| 58 | callbacks => { | ||||||||||
| 59 | 'valid API key' => \&_valid_api_key, | ||||||||||
| 60 | }, | ||||||||||
| 61 | }, | ||||||||||
| 62 | developerkey => { | ||||||||||
| 63 | optional => 1, | ||||||||||
| 64 | type => SCALAR, | ||||||||||
| 65 | callbacks => { | ||||||||||
| 66 | 'valid API key' => \&_valid_api_key, | ||||||||||
| 67 | }, | ||||||||||
| 68 | }, | ||||||||||
| 69 | ); | ||||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | sub verify { | ||||||||
|
Subroutine
verify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 72 | 1 | my ($self, @args) = @_; | |||||||||
| 73 | 1 | my %params = validate( @args, \%verify_spec ); | |||||||||
| 74 | 1 | return $self->get( 'verify', \%params )->parse_response; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 5 | my %notify_spec = ( | |||||||||
| 78 | apikey => { | ||||||||||
| 79 | type => SCALAR | ARRAYREF, | ||||||||||
| 80 | callbacks => { | ||||||||||
| 81 | 'valid API key' => \&_valid_api_key, | ||||||||||
| 82 | }, | ||||||||||
| 83 | }, | ||||||||||
| 84 | application => { | ||||||||||
| 85 | type => SCALAR, | ||||||||||
| 86 | regex => qr/^$APPREGEX$/xms, | ||||||||||
| 87 | }, | ||||||||||
| 88 | event => { | ||||||||||
| 89 | type => SCALAR, | ||||||||||
| 90 | regex => qr/^$EVENTREGEX$/xms, | ||||||||||
| 91 | }, | ||||||||||
| 92 | description => { | ||||||||||
| 93 | type => SCALAR, | ||||||||||
| 94 | regex => qr/^$DESCREGEX$/xms, | ||||||||||
| 95 | }, | ||||||||||
| 96 | priority => { | ||||||||||
| 97 | optional => 1, | ||||||||||
| 98 | type => SCALAR, | ||||||||||
| 99 | regex => qr/^$PRIOREGEX$/xms, | ||||||||||
| 100 | default => 0, | ||||||||||
| 101 | }, | ||||||||||
| 102 | developerkey => { | ||||||||||
| 103 | optional => 1, | ||||||||||
| 104 | type => SCALAR, | ||||||||||
| 105 | callbacks => { | ||||||||||
| 106 | 'valid API key' => \&_valid_api_key, | ||||||||||
| 107 | }, | ||||||||||
| 108 | }, | ||||||||||
| 109 | ); | ||||||||||
| 110 | |||||||||||
| 111 | 0 | ▶ | sub notify { | ||||||||
|
Subroutine
notify: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 112 | 0 | my ($self, @args) = @_; | |||||||||
| 113 | 0 | my %params = validate( @args, \%notify_spec ); | |||||||||
| 114 | 0 | return $self->post( 'notify', \%params )->parse_response; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | # private functions | ||||||||||
| 118 | |||||||||||
| 119 | 1 | ▶ | sub _valid_api_key { | ||||||||
|
Subroutine
_valid_api_key: called CC=5 SCAR=21.9
| |||||||||||
| 120 | 1 | my( $candidate, $params ) = @_; | |||||||||
| 121 | |||||||||||
| 122 | 1 | ▶ | if ( ref( $candidate ) eq 'ARRAY' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 0 | foreach my $key ( @{$candidate} ) { | |||||||||
| 124 | 0 | ▶ | _valid_api_key( $key ) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | } | ||||||||||
| 126 | } | ||||||||||
| 127 | else { | ||||||||||
| 128 | 1 | ▶ | $candidate =~ /^$KEYREGEX$/ixms or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | } | ||||||||||
| 130 | 1 | return( $candidate ); | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 5 | 1; # Magic true value required at end of module | |||||||||
| 134 | # ABSTRACT: Perl interface to Notify My Android web API | ||||||||||
| 135 | |||||||||||
| 136 | __END__ | ||||||||||