blib/lib/WebService/Connpass.pm

filter: Statementstmt 89.2% 108 / 121 Branchbran 69.4% 25 / 36 Conditioncond 42.1% 8 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 78.3% 163 / 208 CC 28 SCAR 18.6
CC
28
Cov
80%
next
10.6
fetch
9.9
new
8.2
CRAP 6.4
↑ Summary
1 package WebService::Connpass;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 our $VERSION = qv('0.0.1');
10  
11 3 ▶use base qw/Class::Accessor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Data::Recursive::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Hash::AsObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Accessors
20 3 __PACKAGE__->mk_accessors( qw/ iter / );
21  
22 # Constructor
23 2 ▶sub new {
Subroutine new: called CC=7 SCAR=21.1
Pod documented
24 2 my ($class, %param) = @_;
25 2 my $self = bless({}, $class);
26  
27 # Parameter - Base URL (API Endpoint)
28 2 ▶ if(defined($param{baseurl})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'baseurl'}) { } 1 1 2
29 1 $self->{baseurl} = $param{baseurl};
30 1 delete $param{baseurl};
31 }else{
32 1 $self->{baseurl} = 'http://connpass.com/api/v1/';
33 }
34  
35 # Parameter - Encoding (Char-set)
36 2 ▶ if(defined($param{encoding})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"encoding"}) 0 2 2
37 0 $self->{encoding} = $param{encoding};
38 0 delete $param{encoding};
39 }
40  
41 # Parameter - Automatic next page fetch
42 2 ▶ if(defined($param{disable_nextpage_fetch}) && $param{disable_nextpage_fetch}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'disable_nextpage_fetch'} and $param{'disable_nextpage_fetch'}) { } 0 2 2
Condition33%1/3
defined $param{"disable_nextpage_fetch"} and $param{"disable_nextpage_fetch"}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $param{"disable_nextpage_fetch"} and $param{"disable_nextpage_fetch"}
defined $param{"disable_nextpage_fetch"} $param{"disable_nextpage_fetch"}
Truth table33%1/3
A and B
A= defined $param{"disable_nextpage_fetch"}B= $param{"disable_nextpage_fetch"}
ABresult
0X0
100
111
43 0 $self->{nextpage_fetch} = 0;
44 0 delete $param{disable_nextpage_fetch};
45 }else{
46 2 $self->{nextpage_fetch} = 1;
47 }
48  
49 # Parameter - Timeout
50 2 ▶ $param{timeout} = $param{timeout} || 10;
Condition50%1/2
$param{'timeout'} || 10
l!l
02
MC/DC0%0/1
$param{'timeout'} || 10
$param{'timeout'}
Truth table50%1/2
A
A= $param{'timeout'}
Aresult
11
00
51  
52 # Parameter - UserAgent string
53 2 ▶ $param{agent} = $param{agent} || __PACKAGE__.'/'.$VERSION;
Condition50%1/2
$param{'agent'} || 'WebService::Connpass/' . $WebService::Connpass::VERSION
l!l
02
MC/DC0%0/1
$param{'agent'} || 'WebService::Connpass/' . $WebService::Connpass::VERSION
$param{'agent'}
Truth table50%1/2
A
A= $param{'agent'}
Aresult
11
00
54  
55 # ----------
56  
57 # Prepare a LWP::UA instance
58 2 $self->{ua} = LWP::UserAgent->new(%param);
59  
60 # Prepare a Date parser instance
61 2 $self->{datetime_parser} = DateTime::Format::ISO8601->new();
62  
63 # Prepare events store array
64 2 $self->{events} = [];
65  
66 2 $self->{current_request_path} = '';
67 2 $self->{current_query} = ();
68 2 return $self;
69 }
70  
71 # Fetch events
72 2 ▶sub fetch {
Subroutine fetch: called CC=9 SCAR=22.9
Pod documented
73 2 my ($self, $request_path, %query) = @_;
74  
75 2 my $is_auto_fetch = 0;
76 2 ▶ if(defined($query{_is_auto_fetch})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query{"_is_auto_fetch"}) 0 2 2
77 0 $is_auto_fetch = 1;
78 0 delete $query{_is_auto_fetch};
79 }
80  
81 2 $self->{current_request_path} = $request_path;
82 2 ▶ $self->{current_query} = \%query || {};
Condition50%1/2
\%query || {}
l!l
20
MC/DC0%0/1
\%query || {}
\%query
Truth table50%1/2
A
A= \%query
Aresult
11
00
83 2 ▶ $self->{current_query}->{count} = $self->{current_query}->{count} || 10; # Each fetch num of item
Condition50%1/2
$self->{'current_query'}{'count'} || 10
l!l
02
MC/DC0%0/1
$self->{'current_query'}{'count'} || 10
$self->{'current_query'}{'count'}
Truth table50%1/2
A
A= $self->{'current_query'}{'count'}
Aresult
11
00
84  
85 # Request
86 2 my $url = $self->_generate_get_url($self->{baseurl}.$request_path.'/', %{$self->{current_query}});
87 2 my $response = $self->{ua}->get($url);
88 2 ▶ unless($response->is_success){
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 1 1 2
89 1 die 'Fetch error: '.$response->status_line;
90 }
91  
92 # Decode JSON
93 1 my $js_hash = JSON->new->utf8->decode($response->content);
94  
95 # Encoding
96 1 ▶ if(defined($self->{encoding})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"encoding"}) 0 1 1
97 0 $js_hash = Data::Recursive::Encode->encode($self->{encoding}, $js_hash);
98 }
99  
100 # Initialize the events store array
101 1 ▶ unless($is_auto_fetch){ # If not auto-fetch...
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_auto_fetch) 1 0 1
102 1 $self->{events} = [];
103 }
104  
105 # Store events
106 1 foreach my $item(@{$js_hash->{events}}){
107 2 my $item_id = $item->{event_id};
108 2 push(@{$self->{events}}, $item);
109 }
110  
111 # Reset iterator
112 1 ▶ unless($is_auto_fetch){
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_auto_fetch) 1 0 1
113 1 $self->iter(0);
114 }
115  
116 1 return;
117 }
118  
119 # Put to next a Iterator
120 4 ▶sub next {
Subroutine next: called CC=7 SCAR=23.6
Pod documented
121 4 my $self = shift;
122 4 ▶ my $_is_disable_autofetch = shift || 0;
Condition50%1/2
shift() || 0
l!l
04
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
123  
124 4 my $i = $self->iter();
125 0 ▶ if($i < 0){ $i = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) 0 4 4
126  
127 4 ▶ if($i < @{$self->{events}}){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{'events'};}) { } 3 1 4
128 # Next one
129 3 $self->iter($i + 1);
130 # Return one event object
131 3 return $self->_generate_event_object($self->{events}->[$i]);
132 }else{
133 # Fetch next page automatically
134 1 ▶ if($self->{nextpage_fetch} == 1 && $_is_disable_autofetch == 0 && @{$self->{events}} % $self->{current_query}->{count} == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nextpage_fetch"} == 1 and $_is_disable_autofetch == 0 and @{$self->{"events"};} % $self->{"current_query"}{"count"} == 0) 0 1 1
Condition33%2/6
$self->{"nextpage_fetch"} == 1 and $_is_disable_autofetch == 0 and @{$self->{"events"};} % $self->{"current_query"}{"count"} == 0
!ll&&!rl&&r
010
$self->{"nextpage_fetch"} == 1 and $_is_disable_autofetch == 0
!ll&&!rl&&r
001
MC/DC0%0/3
$self->{"nextpage_fetch"} == 1 and $_is_disable_autofetch == 0 and @{$self->{"events"};} % $self->{"current_query"}{"count"} == 0
$self->{"nextpage_fetch"} == 1 $_is_disable_autofetch == 0 @{$self->{"events"};} % $self->{"current_query"}{"count"} == 0
Truth table25%1/4
A and B and C
A= $self->{"nextpage_fetch"} == 1B= $_is_disable_autofetch == 0C= @{$self->{"events"};} % $self->{"current_query"}{"count"} == 0
ABCresult
0XX0
10X0
1100
1111
135 0 $self->{current_query}->{start} = $i;
136 0 $self->{current_query}->{_is_auto_fetch} = 1;
137 # Auto fetch
138 0 $self->fetch($self->{current_request_path}, %{$self->{current_query}});
139 0 return $self->next(1);
140 }
141 }
142 1 return;
143 }
144  
145 # prev a Iterator
146 4 ▶sub prev {
Subroutine prev: called CC=2 SCAR=6.9
Pod documented
147 4 my $self = shift;
148  
149 4 my $i = $self->iter() - 1;
150  
151 4 ▶ if(0 <= $i){
Branch: 1 branch
BranchTrueFalseTotal
if (0 <= $i) 3 1 4
152 # Prev one
153 3 $self->iter($i);
154 # Return one event object
155 3 return $self->_generate_event_object($self->{events}->[$i]);
156 }
157 1 return;
158 }
159  
160 # Generate Event object from Hash
161 6 ▶sub _generate_event_object {
Subroutine _generate_event_object: called CC=7 SCAR=19.7
162 6 my ($self, $hash) = @_;
163
164 # Date parse
165 6 ▶ unless(defined($hash->{started})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"started"}) 2 4 6
166 2 ▶ $hash->{started} = defined($hash->{started_at}) ? $self->{datetime_parser}->parse_datetime($hash->{started_at}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $hash->{'started_at'} ? : 2 0 2
167 }
168  
169 6 ▶ unless(defined($hash->{ended})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"ended"}) 2 4 6
170 2 ▶ $hash->{ended} = defined($hash->{ended_at}) ? $self->{datetime_parser}->parse_datetime($hash->{ended_at}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $hash->{'ended_at'} ? : 2 0 2
171 }
172  
173 6 ▶ unless(defined($hash->{updated})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash->{"updated"}) 2 4 6
174 2 ▶ $hash->{updated} = defined($hash->{updated_at}) ? $self->{datetime_parser}->parse_datetime($hash->{updated_at}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $hash->{'updated_at'} ? : 2 0 2
175 }
176  
177 6 return Hash::AsObject->new($hash);
178 }
179  
180 # Generate URL from URL And Query parameters
181 2 ▶sub _generate_get_url {
Subroutine _generate_get_url: called CC=1 SCAR=0.0
182 2 my ($self, $url, %params) = @_;
183 2 my $uri = URI->new($url);
184 2 $uri->query_form(\%params);
185 2 return $uri->as_string();
186 }
187  
188 3 1;
189 __END__
↑ Summary