| 1 | package WebService::OkiLab::ExtractPlace; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | our $VERSION = 0.02; | |||||||||||
| 8 | |||||||||||||
| 9 | 2 | ▶ | use HTTP::Request::Common qw(GET POST); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 14 | 0 | my $class = shift; | |||||||||||
| 15 | 0 | my $self = bless {}, $class; | |||||||||||
| 16 | 0 | $self->url('http://okilab.jp/project/location/api/v1/extract_place/'); | |||||||||||
| 17 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||
| 18 | 0 | $ua->env_proxy(); | |||||||||||
| 19 | 0 | $self->ua($ua); | |||||||||||
| 20 | 0 | return $self; | |||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | 0 | ▶ | sub param { | ||||||||||
|
Subroutine
param: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 24 | 0 | my $self = shift; | |||||||||||
| 25 | 0 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | 0 | my $key = shift; | |||||||||||
| 27 | 0 | ▶ | $self->{"_$key"} = shift if (@_); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 0 | return $self->{"_$key"}; | |||||||||||
| 29 | } else { | ||||||||||||
| 30 | 0 | my @keys = sort map { substr($_, 1) } grep { /^_/ } keys(%{$self}); | |||||||||||
| 31 | 0 | return @keys; | |||||||||||
| 32 | } | ||||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 0 | ▶ | sub ua { my $self = shift; return $self->param('ua', @_); } | ||||||||||
|
Subroutine
ua: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 36 | 0 | ▶ | sub url { my $self = shift; return $self->param('url', @_); } | ||||||||||
|
Subroutine
url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 37 | 0 | ▶ | sub res { my $self = shift; return $self->param('res', @_); } | ||||||||||
|
Subroutine
res: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 38 | 0 | ▶ | sub req { my $self = shift; return $self->param('req', @_); } | ||||||||||
|
Subroutine
req: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 39 | |||||||||||||
| 40 | 0 | ▶ | sub extract { | ||||||||||
|
Subroutine
extract: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||
| 42 | 0 | my $text = shift; | |||||||||||
| 43 | 0 | my $ua = $self->ua(); | |||||||||||
| 44 | 0 | my $url = URI->new($self->url()); | |||||||||||
| 45 | 0 | ▶ | return $self->error("Can't load LWP::UserAgent object.") if (not UNIVERSAL::isa($ua, 'LWP::UserAgent')); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 0 | ▶ | return $self->error("Web service url is invalid. ($url)") if ($url->scheme ne 'http'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 0 | ▶ | return $self->error("Text is empty.") if ($text !~ /\S/); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | # create HTTP::Response | ||||||||||||
| 49 | 0 | ▶ | my $method = uc($self->param('method')) || 'POST'; | ||||||||||
|
Condition0%0/2
uc $self->param('method') || 'POST'
MC/DC0%0/1
uc $self->param('method') || 'POST'
uc $self->param('method')
Truth table0%0/2
A
A= uc $self->param('method')
| |||||||||||||
| 50 | 0 | ▶ | $url->query_form('text' => $text) if ($method eq 'GET'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 0 | ▶ | my $req = ($method eq 'GET') ? GET($url) : POST($url, ['text' => $text]); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | # do reqeust | ||||||||||||
| 53 | 0 | $self->req($req); | |||||||||||
| 54 | 0 | my $res = $ua->request($req); | |||||||||||
| 55 | 0 | ▶ | $self->res($res) || return $self->error('Response is empty.'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 0 | ▶ | $res->is_success || return $self->error(sprintf('Request failed. (%s)', $res->status_line)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | # parse result | ||||||||||||
| 58 | 0 | ▶ | my $json = $res->content() || return $self->error('Response content is empty.'); | ||||||||||
|
Condition0%0/2
$res->content || (return $self->error('Response content is empty.'))
MC/DC0%0/1
$res->content || (return $self->error('Response content is empty.'))
$res->content
Truth table0%0/2
A
A= $res->content
| |||||||||||||
| 59 | 0 | ▶ | my $result = jsonToObj($json) || return $self->error("Can't parse response content."); | ||||||||||
|
Condition0%0/2
jsonToObj($json) || (return $self->error(q[Can't parse response content.]))
MC/DC0%0/1
jsonToObj($json) || (return $self->error(q[Can't parse response content.]))
jsonToObj($json)
Truth table0%0/2
A
A= jsonToObj($json)
| |||||||||||||
| 60 | 0 | return $result; | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 2 | 1; | |||||||||||
| 64 | __END__ | ||||||||||||