SyoboiCalendar/API.pm

filter: Statementstmt 73.4% 69 / 94 Branchbran 37.5% 6 / 16 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 81.8% 18 / 22 Podpod 0.0% 0 / 7 totaltotal 64.9% 100 / 154 CC 13 SCAR 13.2
CC
13
Cov
67%
search
12.0
_datetime_param
7.0
post
6.0
CRAP 3.8
« SyoboiCalendar/API/Search.pm ↑ Summary SyoboiCalendar/Title.pm »
1 package WebService::SyoboiCalendar::API;
2 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Smart::Args;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Web::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use WebService::SyoboiCalendar::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use WebService::SyoboiCalendar::API::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 Readonly my $API_RSS2 => "http://cal.syoboi.jp/rss2.php";
16 2 Readonly my $API_JSON => "http://cal.syoboi.jp/json.php";
17 2 Readonly my $API_LOGIN => "http://cal.syoboi.jp/usr";
18  
19 has ua => (
20 is => 'ro',
21 5 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
22 5 my $ua = LWP::UserAgent->new;
23 5 $ua->cookie_jar( {} );
24 5 $ua;
25 },
26 2 );
27  
28 2 has user => ( is => 'ro' );
29 2 has pass => ( is => 'ro' );
30  
31 2 has is_logging_in => (
32 is => 'rw',
33 default => 0,
34 );
35  
36 4 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod undocumented
37 4 my ($self, $url, $args) = @_;
38 4 $url = URI->new($url);
39 4 $url->query_form(%$args);
40 4 my $res = $self->ua->get($url);
41 4 ▶ WebService::SyoboiCalendar::APIRequestError->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_error 0 4 4
42 error => qq(error while requesting to "$url"),
43 res => $res,
44 ) if $res->is_error;
45 4 $res;
46 }
47  
48 0 ▶sub post {
Subroutine post: not called CC=2 SCAR=17.9
Pod undocumented
49 0 my ($self, $url, $args) = @_;
50 0 $url = URI->new($url);
51 0 my $res = $self->ua->post($url, $args);
52 0 ▶ WebService::SyoboiCalendar::APIRequestError->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_error 0 0 0
53 error => qq(error while requesting to "$url"),
54 res => $res,
55 ) if $res->is_error;
56 0 $res;
57 }
58  
59 4 ▶sub get_json {
Subroutine get_json: called CC=1 SCAR=0.0
Pod undocumented
60 4 my ($self, $url, $args) = @_;
61 4 my $res = $self->get($url, $args);
62 4 my $json = decode_json($res->content);
63 4 $json;
64 }
65  
66 33 ▶sub _param {
Subroutine _param: called CC=3 SCAR=11.0
67 33 args_pos my $hash, my $key;
68 33 ▶ my $val = delete($hash->{$key}) || delete($hash->{lc($key)});
Condition100%3/3
delete $hash->{$key} || delete $$hash{lc $key}
l!l&&r!l&&!r
2526
MC/DC100%2/2
delete $hash->{$key} || delete $$hash{lc $key}
delete $hash->{$key} delete $$hash{lc $key}
Truth table100%3/3
A || B
A= delete $hash->{$key}B= delete $$hash{lc $key}
ABresult
1X1
011
000
69 33 ▶ $val ? ($key => $val) : ();
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 7 26 33
70 }
71  
72 2 ▶sub _datetime_param {
Subroutine _datetime_param: called CC=5 SCAR=19.4
73 2 args_pos my $hash, my $key;
74 2 ▶ my $val = delete($hash->{$key}) || delete($hash->{lc($key)});
Condition33%1/3
delete $hash->{$key} || delete $$hash{lc $key}
l!l&&r!l&&!r
002
MC/DC0%0/2
delete $hash->{$key} || delete $$hash{lc $key}
delete $hash->{$key} delete $$hash{lc $key}
Truth table33%1/3
A || B
A= delete $hash->{$key}B= delete $$hash{lc $key}
ABresult
1X1
011
000
75 2 ▶ $val = $val->strftime('%Y%m%d%H%M') if $val && $val->isa('DateTime');
Branch: 1 branch
BranchTrueFalseTotal
if $val and $val->isa("DateTime") 0 2 2
Condition33%1/3
$val and $val->isa("DateTime")
!ll&&!rl&&r
200
MC/DC0%0/2
$val and $val->isa("DateTime")
$val $val->isa("DateTime")
Truth table33%1/3
A and B
A= $valB= $val->isa("DateTime")
ABresult
0X0
100
111
76 2 ▶ $val ? ($key => $val) : ();
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 2 2
77 }
78  
79 0 ▶sub login {
Subroutine login: not called CC=2 SCAR=17.9
Pod undocumented
80 0 my ($self) = @_;
81 0 ▶ return if $self->is_logging_in;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_logging_in 0 0 0
82  
83 0 my $res = $self->get($API_LOGIN);
84 0 my $frame = wq($res->content)->find('.LoginFrame')->first;
85 0 my $params = {};
86 0 ▶ $frame->find('input[type="hidden"]')->each( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 my ($i, $elem) = @_;
88 0 my $name = $elem->attr('name');
89 0 my $value = $elem->attr('value');
90 0 $params->{$name} = $value;
91 0 });
92 0 $params->{usr} = $self->user;
93 0 $params->{pass} = $self->pass;
94 0 $self->post($API_LOGIN.".php", $params);
95 0 $self->is_logging_in(1);
96 }
97  
98 1 ▶sub rss2 {
Subroutine rss2: called CC=2 SCAR=7.0
Pod undocumented
99 1 my ($self, $args) = @_;
100 $self->get_json($API_RSS2, {
101 (map {
102 6 _param($args, $_);
103 } qw(days titlefmt usr filter usch ssch)),
104 (map {
105 1 ▶ _datetime_param($args, $_);
Branch: 1 branch
BranchTrueFalseTotal
$self->user ? : 0 1 1
106 } qw(start end)),
107 alt => 'json',
108 ($self->user ? (usr => $self->user) : () ),
109 %$args,
110 });
111 }
112  
113 3 ▶sub json {
Subroutine json: called CC=1 SCAR=0.0
Pod undocumented
114 3 my ($self, $args) = @_;
115 my $result = $self->get_json($API_JSON, {
116 (map {
117 3 _param($args, $_);
118 } qw(Req Start Days TID PID ChID Count Search Limit)),
119 %$args,
120 });
121  
122 3 $result;
123 }
124  
125 0 ▶sub search {
Subroutine search: not called CC=3 SCAR=24.8
Pod undocumented
126 0 my ($self, $args) = @_;
127 0 $self->login;
128  
129 my $search = WebService::SyoboiCalendar::API::Search->new(
130 api => $self,
131 mode => $args->{mode},
132 0 );
133 0 for my $key (qw(
134 title channel subtitle comment fresh final special range
135 )) {
136 0 ▶ $search->$key( $args->{$key} ) if exists $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$key} 0 0 0
137 }
138 0 $search->get;
139 }
140  
141 2 1;
« SyoboiCalendar/API/Search.pm ↑ Summary SyoboiCalendar/Title.pm »