blib/lib/WWW/Yahoo/Smushit.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package WWW::Yahoo::Smushit;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use LWP::UserAgent;
7 use JSON;
8  
9 our $VERSION = '0.03';
10  
11 has _ua => (
12 is => 'rw',
13 lazy => 1,
14 required => 1,
15 default => sub { LWP::UserAgent->new; }
16 );
17  
18 has _service_url => (
19 is => 'ro',
20 isa => 'Str',
21 default => 'http://www.smushit.com/ysmush.it/ws.php?img=',
22 required => 1
23 );
24  
25 sub upload_by_url {
26 my ($self, $img_url) = @_;
27  
28 my $req = $self->_ua->get($self->_service_url . $img_url);
29  
30 my $json = JSON->new->allow_nonref;
31 my $resp = $json->decode($req->content);
32 return 1
33 if($self->_create_attrs_from_json($resp));
34  
35 return 0;
36 }
37  
38 sub _create_attrs_from_json {
39 my ($self, $json) = @_;
40  
41 for (keys %{$json}) {
42 $self->meta->add_attribute($_, is => 'rw');
43 $self->$_($json->{$_});
44 }
45  
46 $self->meta->make_immutable;
47  
48 return 0
49 if not keys %{$json}
50 or defined $json->{error};
51 return 1;
52 }
53  
54 1;
55  
56 __END__
↑ Summary