| 1 | package WebService::Hatena::Fotolife; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use base qw(XML::Atom::Client); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use FileHandle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Encode qw(encode_utf8 is_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Image::Info qw(image_info); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use WebService::Hatena::Fotolife::Entry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 14 | 2 | our $PostURI = 'http://f.hatena.ne.jp/atom/post'; | |||||||||||||||||||||||||||
| 15 | 2 | our $FeedURI = 'http://f.hatena.ne.jp/atom/feed'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use constant GEORSS => q<http://www.georss.org/georss>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 21 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $headers = delete $params{headers} || []; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $params{'headers'} || []
MC/DC0%0/1
delete $params{'headers'} || []
delete $params{'headers'}
Truth table0%0/2
A
A= delete $params{'headers'}
| |||||||||||||||||||||||||||||
| 23 | 0 | my $consumer_key = delete $params{consumer_key}; | |||||||||||||||||||||||||||
| 24 | 0 | my $consumer_secret = delete $params{consumer_secret}; | |||||||||||||||||||||||||||
| 25 | 0 | my $access_token = delete $params{access_token}; | |||||||||||||||||||||||||||
| 26 | 0 | my $access_token_secret = delete $params{access_token_secret}; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $self = $class->SUPER::new(%params) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | or return $class->error($class->SUPER::errstr); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | $self->{consumer_key} = $consumer_key; | |||||||||||||||||||||||||||
| 31 | 0 | $self->{consumer_secret} = $consumer_secret; | |||||||||||||||||||||||||||
| 32 | 0 | $self->{access_token} = $access_token; | |||||||||||||||||||||||||||
| 33 | 0 | $self->{access_token_secret} = $access_token_secret; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | $self->{headers} = $headers; | |||||||||||||||||||||||||||
| 36 | 0 | $self->{ua}->agent(__PACKAGE__."/$VERSION"); | |||||||||||||||||||||||||||
| 37 | 0 | $self; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub use_oauth { | ||||||||||||||||||||||||||
|
Subroutine
use_oauth: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 0 | return defined $_[0]->access_token; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub consumer_key { $_[0]->{consumer_key} } | ||||||||||||||||||||||||||
|
Subroutine
consumer_key: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub consumer_secret { $_[0]->{consumer_secret} } | ||||||||||||||||||||||||||
|
Subroutine
consumer_secret: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub access_token { $_[0]->{access_token} } | ||||||||||||||||||||||||||
|
Subroutine
access_token: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub access_token_secret { $_[0]->{access_token_secret} } | ||||||||||||||||||||||||||
|
Subroutine
access_token_secret: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub createEntry { | ||||||||||||||||||||||||||
|
Subroutine
createEntry: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 0 | my ($self, %param) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | return $self->error('title and image source are both required') | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | unless $param{title} || grep {!$_} @param{qw(filename scalarref)}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$param{"title"} or grep {not $_;} @param{"filename", "scalarref"}
MC/DC0%0/2
$param{"title"} or grep {not $_;} @param{"filename", "scalarref"}
$param{"title"}
grep {not $_;} @param{"filename", "scalarref"}
Truth table0%0/3
A or B
A= $param{"title"}B= grep {not $_;} @param{"filename", "scalarref"}
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | my $image = $self->_get_image($param{filename} || $param{scalarref}) | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | or return $self->error($self->errstr); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$param{'filename'} || $param{'scalarref'}
MC/DC0%0/2
$param{'filename'} || $param{'scalarref'}
$param{'filename'}
$param{'scalarref'}
Truth table0%0/3
A || B
A= $param{'filename'}B= $param{'scalarref'}
| |||||||||||||||||||||||||||||
| 57 | 0 | my $entry = $self->_make_entry(create => +{ %param, image => $image }); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | $self->SUPER::createEntry($PostURI, $entry); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub updateEntry { | ||||||||||||||||||||||||||
|
Subroutine
updateEntry: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($self, $EditURI, %param) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | return $self->error('EditURI and title are both required') | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | unless $EditURI || $param{title}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$EditURI or $param{"title"}
MC/DC0%0/2
$EditURI or $param{"title"}
$EditURI
$param{"title"}
Truth table0%0/3
A or B
A= $EditURIB= $param{"title"}
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $entry = $self->_make_entry(update => \%param); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | $self->SUPER::updateEntry($EditURI, $entry); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub munge_request { | ||||||||||||||||||||||||||
|
Subroutine
munge_request: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 0 | my $req = shift; | |||||||||||||||||||||||||||
| 76 | 0 | my $headers = [@{ $self->{headers} }]; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($self->use_oauth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | require OAuth::Lite::Consumer; | |||||||||||||||||||||||||||
| 80 | 0 | require OAuth::Lite::Token; | |||||||||||||||||||||||||||
| 81 | 0 | require OAuth::Lite::AuthMethod; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $consumer = OAuth::Lite::Consumer->new( | |||||||||||||||||||||||||||
| 84 | consumer_key => $self->consumer_key, | ||||||||||||||||||||||||||||
| 85 | consumer_secret => $self->consumer_secret, | ||||||||||||||||||||||||||||
| 86 | auth_method => OAuth::Lite::AuthMethod::URL_QUERY(), | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | 0 | my $access_token = OAuth::Lite::Token->new( | |||||||||||||||||||||||||||
| 89 | token => $self->access_token, | ||||||||||||||||||||||||||||
| 90 | secret => $self->access_token_secret, | ||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||
| 92 | 0 | my $url = $req->uri; | |||||||||||||||||||||||||||
| 93 | 0 | $req->uri($url . '?' . $consumer->gen_auth_query($req->method, $url, $access_token, {})); | |||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||
| 95 | 0 | $self->SUPER::munge_request($req); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | while (my ($key, $value) = splice @$headers, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | $req->header($key => $value); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | $req; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub munge_response { | ||||||||||||||||||||||||||
|
Subroutine
munge_response: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 106 | 0 | my $res = shift; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $status = $res->header('Status'); | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($status and $status =~ s/^(\d+)\s+(?=.+)//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$status and $status =~ s/^(\d+)\s+(?=.+)//
MC/DC0%0/2
$status and $status =~ s/^(\d+)\s+(?=.+)//
$status
$status =~ s/^(\d+)\s+(?=.+)//
Truth table0%0/3
A and B
A= $statusB= $status =~ s/^(\d+)\s+(?=.+)//
| |||||||||||||||||||||||||||||
| 110 | 0 | $res->code($1); | |||||||||||||||||||||||||||
| 111 | 0 | $res->message($status); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | $self->SUPER::munge_response($res, @_); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub getFeed { | ||||||||||||||||||||||||||
|
Subroutine
getFeed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 0 | $self->SUPER::getFeed($FeedURI); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub _make_entry { | ||||||||||||||||||||||||||
|
Subroutine
_make_entry: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($self, $method, $param) = @_; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | $param ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$param ||= {}
MC/DC0%0/1
$param ||= {}
$param
Truth table0%0/2
A
A= $param
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $entry = WebService::Hatena::Fotolife::Entry->new; | |||||||||||||||||||||||||||
| 127 | 0 | $entry->title($param->{title}); | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | $entry->generator($param->{generator}) if defined $param->{generator}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ($method eq 'create') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my $image = $param->{image}; | |||||||||||||||||||||||||||
| 132 | 0 | $entry->content(${$image->{content}}); | |||||||||||||||||||||||||||
| 133 | 0 | $entry->content->type($image->{content_type}); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if (defined $param->{folder}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | my $dc = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/'); | |||||||||||||||||||||||||||
| 138 | 0 | $entry->set($dc, 'subject', $param->{folder}); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (defined $param->{lat} && defined $param->{lon}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $param->{"lat"} and defined $param->{"lon"}
MC/DC0%0/2
defined $param->{"lat"} and defined $param->{"lon"}
defined $param->{"lat"}
defined $param->{"lon"}
Truth table0%0/3
A and B
A= defined $param->{"lat"}B= defined $param->{"lon"}
| |||||||||||||||||||||||||||||
| 142 | 0 | my $georss = XML::Atom::Namespace->new(georss => GEORSS); | |||||||||||||||||||||||||||
| 143 | 0 | $entry->set($georss, 'point', $param->{lat} . ' ' . $param->{lon}); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($param->{hatena} && ref $param->{hatena} eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$param->{"hatena"} and ref $param->{"hatena"} eq "HASH"
MC/DC0%0/2
$param->{"hatena"} and ref $param->{"hatena"} eq "HASH"
$param->{"hatena"}
ref $param->{"hatena"} eq "HASH"
Truth table0%0/3
A and B
A= $param->{"hatena"}B= ref $param->{"hatena"} eq "HASH"
| |||||||||||||||||||||||||||||
| 147 | 0 | my $hatena = XML::Atom::Namespace->new( | |||||||||||||||||||||||||||
| 148 | hatena => 'http://www.hatena.ne.jp/info/xmlns#' | ||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | for my $key (keys %{$param->{hatena}}) { | |||||||||||||||||||||||||||
| 152 | 0 | $entry->set($hatena, $key, $param->{hatena}{$key}); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | $entry; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub _get_image { | ||||||||||||||||||||||||||
|
Subroutine
_get_image: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 160 | 0 | my ($self, $image_source) = @_; | |||||||||||||||||||||||||||
| 161 | 0 | my $image; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if (ref $image_source eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | $image = $image_source; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||
| 167 | 0 | $image = do { | |||||||||||||||||||||||||||
| 168 | 0 | local $/ = undef; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | my $fh = FileHandle->new($image_source) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | or return $self->error("can't open $image_source: $!"); | ||||||||||||||||||||||||||||
| 171 | 0 | my $content = <$fh>; | |||||||||||||||||||||||||||
| 172 | 0 | \$content; | |||||||||||||||||||||||||||
| 173 | }; | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | my $info = Image::Info::image_info($image); | |||||||||||||||||||||||||||
| 177 | return $self->error($info->{error}) | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if $info->{error} and $info->{error} !~ /short read/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$info->{"error"} and not $info->{"error"} =~ /short read/
MC/DC0%0/2
$info->{"error"} and not $info->{"error"} =~ /short read/
$info->{"error"}
not $info->{"error"} =~ /short read/
Truth table0%0/3
A and not(B)
A= $info->{"error"}B= not $info->{"error"} =~ /short read/
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my $mime = $info->{file_media_type}; | |||||||||||||||||||||||||||
| 181 | 0 | ▶ | if (ref $mime eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | $mime = $mime->[0]; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | +{ | ||||||||||||||||||||||||||||
| 186 | 0 | content => $image, | |||||||||||||||||||||||||||
| 187 | content_type => $mime, | ||||||||||||||||||||||||||||
| 188 | }; | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||