BusyBird/Main/PSGI/View.pm

filter: Statementstmt 95.0% 269 / 283 Branchbran 78.5% 66 / 84 Conditioncond 77.7% 21 / 27 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 94.2% 66 / 70 Podpod 100% 9 / 9 totaltotal 89.9% 439 / 488 CC 62 SCAR 12.5
CC
62
Cov
90%
response_timeline_list
8.0
_render_text_segment
7.8
response_statuses
7.3
CRAP 3.5
« BusyBird/Filter/Twitter.pm ↑ Summary BusyBird/Config.pm »
1 package BusyBird::Main::PSGI::View;
2 7 ▶use v5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use BusyBird::Util qw(set_param split_with_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use JSON qw(to_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Text::Xslate qw(html_builder html_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use JavaScript::Value::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use BusyBird::DateTime::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use BusyBird::Log qw(bblog);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use BusyBird::SafeData qw(safed);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Cache::Memory::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 53 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 53 my ($class, %args) = @_;
24 53 my $self = bless {
25 main_obj => undef,
26 renderer => undef,
27 }, $class;
28 53 $self->set_param(\%args, "main_obj", undef, 1);
29 53 $self->set_param(\%args, "script_name", undef, 1);
30 53 my $sharedir = $self->{main_obj}->get_config('sharedir_path');
31 53 $sharedir =~ s{/+$}{};
32 $self->{renderer} = Text::Xslate->new(
33 path => [ File::Spec->catdir($sharedir, 'www', 'templates') ],
34 cache_dir => File::Spec->tmpdir,
35 syntax => 'Kolon',
36 function => $self->template_functions(),
37 10 ▶ warn_handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 10 bblog("warn", @_);
39 },
40 ## we don't use die_handler because (1) it is called for every
41 ## death even when it's in eval() scope, (2) exceptions are
42 ## caught by Xslate and passed to warn_handler anyway.
43 53 );
44 53 return $self;
45 }
46  
47 15 ▶sub response_notfound {
Subroutine response_notfound: called CC=2 SCAR=6.9
Pod documented
48 15 my ($self, $message) = @_;
49 15 ▶ $message ||= 'Not Found';
Condition100%2/2
$message ||= "Not Found"
l!l
87
MC/DC100%1/1
$message ||= "Not Found"
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
50 15 return ['404',
51 ['Content-Type' => 'text/plain',
52 'Content-Length' => length($message)],
53 [$message]];
54 }
55  
56 9 ▶sub response_error_html {
Subroutine response_error_html: called CC=1 SCAR=0.0
Pod documented
57 9 my ($self, $http_code, $message) = @_;
58 9 return $self->_response_template(
59 template => 'error.tx', args => {error => $message},
60 code => $http_code
61 );
62 }
63  
64  
65 147 ▶sub response_json {
Subroutine response_json: called CC=2 SCAR=6.9
Pod documented
66 147 my ($self, $res_code, $response_object) = @_;
67 147 ▶ my $message = try {
Subroutine __ANON__: called CC=5 SCAR=16.1
68 147 ▶ die "response must not be undef" if not defined $response_object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response_object 0 147 147
69 147 ▶ if($res_code eq '200' && ref($response_object) eq "HASH" && !exists($response_object->{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_code eq 200 and ref $response_object eq "HASH" and not exists $$response_object{"error"}) 74 73 147
Condition100%6/6
$res_code eq 200 and ref $response_object eq "HASH" and not exists $$response_object{"error"}
!ll&&!rl&&r
175674
$res_code eq 200 and ref $response_object eq "HASH"
!ll&&!rl&&r
161130
MC/DC100%3/3
$res_code eq 200 and ref $response_object eq "HASH" and not exists $$response_object{"error"}
$res_code eq 200 ref $response_object eq "HASH" not exists $$response_object{"error"}
Truth table100%4/4
A and B and not(C)
A= $res_code eq 200B= ref $response_object eq "HASH"C= not exists $$response_object{"error"}
ABCresult
0XX0
10X0
1100
1111
70 74 $response_object->{error} = undef;
71 }
72 147 to_json($response_object, {ascii => 1})
73 1 ▶ }catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 undef
75 147 };
76 147 ▶ if(defined($message)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) { } 146 1 147
77 return [
78 146 $res_code, ['Content-Type' => 'application/json; charset=utf-8'],
79 [$message]
80 ];
81 }else {
82 1 return $self->response_json(500, {error => "error while encoding to JSON."});
83 }
84 }
85  
86 36 ▶sub _response_template {
Subroutine _response_template: called CC=5 SCAR=16.3
87 36 my ($self, %args) = @_;
88 36 my $template_name = delete $args{template};
89 36 ▶ croak 'template parameter is mandatory' if not defined $template_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template_name 0 36 36
90 36 my $args = delete $args{args};
91 36 ▶ my $code = delete $args{code} || 200;
Condition100%2/2
delete $args{'code'} || 200
l!l
927
MC/DC100%1/1
delete $args{'code'} || 200
delete $args{'code'}
Truth table100%2/2
A
A= delete $args{'code'}
Aresult
11
00
92 36 ▶ my $headers = delete $args{headers} || [];
Condition50%1/2
delete $args{'headers'} || []
l!l
036
MC/DC0%0/1
delete $args{'headers'} || []
delete $args{'headers'}
Truth table50%1/2
A
A= delete $args{'headers'}
Aresult
11
00
93 36 ▶ if(!Plack::Util::header_exists($headers, 'Content-Type')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Plack::Util::header_exists($headers, "Content-Type")) 36 0 36
94 36 push(@$headers, 'Content-Type', 'text/html; charset=utf8');
95 }
96 36 my $ret = Encode::encode('utf8', $self->{renderer}->render($template_name, $args));
97 36 return [$code, $headers, [$ret]];
98 }
99  
100  
101 7 my $REGEXP_URL_CHAR = qr{[A-Za-z0-9~\/._!\?\&=\-%#\+:\;,\@\']};
102 7 my $REGEXP_HTTP_URL = qr{https?:\/\/$REGEXP_URL_CHAR+};
103 7 my $REGEXP_ABSOLUTE_PATH = qr{/$REGEXP_URL_CHAR*};
104 7 my %URL_ATTRIBUTES = map { $_ => 1 } qw(src href);
105  
106 107 ▶sub _is_valid_link_url {
Subroutine _is_valid_link_url: called CC=2 SCAR=6.9
107 107 my ($url) = @_;
108 107 ▶ $url = "" if not defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 26 81 107
109 107 return ($url =~ /^(?:$REGEXP_HTTP_URL|$REGEXP_ABSOLUTE_PATH)$/);
110 }
111  
112 108 ▶sub _html_attributes_string {
Subroutine _html_attributes_string: called CC=7 SCAR=19.5
113 108 my ($mandatory_attrs_ref, @attr) = @_;
114 108 ▶ for(my $i = 0 ; $i < $#attr ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#attr) 224 108 332
115 224 $attr[$i] = lc($attr[$i]);
116 }
117 108 tie(my %attr, 'Tie::IxHash', @attr);
118 108 foreach my $attr_key (@$mandatory_attrs_ref) {
119 108 ▶ croak "$attr_key attribute is mandatory" if not defined $attr{$attr_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr{$attr_key} 53 55 108
120 }
121 55 my @attr_strings = ();
122 55 foreach my $attr_key (keys %attr) {
123 73 my $attr_value = $attr{$attr_key};
124 73 my $value_str;
125 73 ▶ if($URL_ATTRIBUTES{$attr_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($URL_ATTRIBUTES{$attr_key}) { } 55 18 73
126 55 ▶ croak "$attr_key attribute is invalid as a URL" if not _is_valid_link_url($attr_value);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_valid_link_url($attr_value) 31 24 55
127 24 $value_str = $attr_value;
128 }else {
129 18 $value_str = html_escape($attr_value);
130 }
131 42 push(@attr_strings, html_escape($attr_key) . qq{="$value_str"});
132 }
133 24 return join(" ", @attr_strings);
134 }
135  
136 73 ▶sub _html_link {
Subroutine _html_link: called CC=2 SCAR=6.9
137 73 my ($text, @attr) = @_;
138 73 ▶ $text = "" if not defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 35 38 73
139 73 my $escaped_text = html_escape($text);
140 73 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
141 73 my $attr_str = _html_attributes_string(['href'], @attr);
142 19 return qq{<a $attr_str>$escaped_text</a>};
143 54 ▶ }catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 54 return $escaped_text;
145 73 };
146 }
147  
148 13 ▶sub _html_link_status_text {
Subroutine _html_link_status_text: called CC=1 SCAR=0.0
149 13 my ($text, $url) = @_;
150 13 return _html_link($text, href => $url, target => "_blank");
151 }
152  
153 488 ▶sub _make_path {
Subroutine _make_path: called CC=2 SCAR=6.9
154 488 my ($script_name, $given_path) = @_;
155 488 ▶ if(substr($given_path, 0, 1) eq "/") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($given_path, 0, 1) eq '/') { } 486 2 488
156 486 return "$script_name$given_path";
157 }else {
158 2 return $given_path;
159 }
160 }
161  
162 54 ▶sub template_functions {
Subroutine template_functions: called CC=1 SCAR=0.0
Pod documented
163 54 my $script_name = $_[0]->{script_name};
164 return {
165 js => \&JavaScript::Value::Escape::js,
166 link => html_builder(\&_html_link),
167 35 ▶ image => html_builder {
Subroutine __ANON__: called CC=1 SCAR=0.0
168 35 my (@attr) = @_;
169 35 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
170 35 my $attr_str = _html_attributes_string(['src'], @attr);
171 5 return qq{<img $attr_str />}
172 30 ▶ }catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
173 30 return "";
174 35 };
175 },
176 54 ▶ bb_level => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
177 54 my $level = shift;
178 54 ▶ $level = 0 if not defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 39 15 54
179 54 return $level;
180 },
181 322 ▶ path => sub { _make_path($script_name, $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
182 55 ▶ script_name => sub { $script_name },
Subroutine __ANON__: called CC=1 SCAR=0.0
183 54 };
184 }
185  
186 24 ▶sub _render_text_segment {
Subroutine _render_text_segment: called CC=7 SCAR=20.5
187 24 my ($self, $timeline_name, $segment, $status) = @_;
188 24 ▶ return undef if !defined($segment->{entity}) || !defined($segment->{type});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$segment{"entity"} and defined $$segment{"type"} 14 10 24
Condition66%2/3
defined $$segment{"entity"} and defined $$segment{"type"}
!ll&&!rl&&r
14010
MC/DC50%1/2
defined $$segment{"entity"} and defined $$segment{"type"}
defined $$segment{"entity"} defined $$segment{"type"}
Truth table66%2/3
A and B
A= defined $$segment{"entity"}B= defined $$segment{"type"}
ABresult
0X0
100
111
189 10 my $url_builder = $self->{main_obj}->get_timeline_config($timeline_name, "$segment->{type}_entity_url_builder");
190 10 my $text_builder = $self->{main_obj}->get_timeline_config($timeline_name, "$segment->{type}_entity_text_builder");
191 10 ▶ return undef if !defined($url_builder) || !defined($text_builder);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url_builder and defined $text_builder 0 10 10
Condition33%1/3
defined $url_builder and defined $text_builder
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $url_builder and defined $text_builder
defined $url_builder defined $text_builder
Truth table33%1/3
A and B
A= defined $url_builderB= defined $text_builder
ABresult
0X0
100
111
192 10 my $url_str = $url_builder->($segment->{text}, $segment->{entity}, $status);
193 10 ▶ return undef if !_is_valid_link_url($url_str);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_valid_link_url($url_str) 0 10 10
194 10 my $text_str = $text_builder->($segment->{text}, $segment->{entity}, $status);
195 10 ▶ $text_str = "" if not defined $text_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text_str 0 10 10
196 10 return _html_link_status_text($text_str, $url_str);
197 }
198  
199 27 ▶sub template_functions_for_timeline {
Subroutine template_functions_for_timeline: called CC=1 SCAR=0.1
Pod documented
200 27 my ($self, $timeline_name) = @_;
201 27 weaken $self; ## in case the functions are kept by $self
202 return {
203 28 ▶ bb_timestamp => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
204 28 my ($timestamp_string) = @_;
205 28 ▶ return "" if !$timestamp_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $timestamp_string 16 12 28
206 12 my $timezone = $self->_get_timezone($self->{main_obj}->get_timeline_config($timeline_name, "time_zone"));
207 2 my $dt = BusyBird::DateTime::Format->parse_datetime($timestamp_string);
208 2 ▶ return "" if !defined($dt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dt 0 2 2
209 2 $dt->set_time_zone($timezone);
210 2 $dt->set_locale($self->{main_obj}->get_timeline_config($timeline_name, "time_locale"));
211 2 return $dt->strftime($self->{main_obj}->get_timeline_config($timeline_name, "time_format"));
212 },
213 31 ▶ bb_status_permalink => sub {
Subroutine __ANON__: called CC=2 SCAR=7.5
214 31 my ($status) = @_;
215 31 my $builder = $self->{main_obj}->get_timeline_config($timeline_name, "status_permalink_builder");
216 31 ▶ my $url = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
217 31 $builder->($status);
218 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 my ($e) = @_;
220 0 bblog("error", "Error in status_permalink_builder: $e");
221 0 undef;
222 31 };
223 31 ▶ return (_is_valid_link_url($url) ? $url : "");
Branch: 1 branch
BranchTrueFalseTotal
_is_valid_link_url($url) ? : 3 28 31
224 },
225 31 ▶ bb_text => html_builder {
Subroutine __ANON__: called CC=4 SCAR=14.1
226 31 my $status = shift;
227 31 ▶ return "" if not defined $status->{text};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$status{"text"} 24 7 31
228 7 my $segments_ref = split_with_entities($status->{text}, $status->{entities});
229 7 my $result_text = "";
230 7 foreach my $segment (@$segments_ref) {
231 24 ▶ my $rendered_text = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
232 24 $self->_render_text_segment($timeline_name, $segment, $status)
233 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 0 my ($e) = @_;
235 0 bblog("error", "Error while rendering text: $e");
236 0 undef;
237 24 };
238 24 ▶ $result_text .= defined($rendered_text) ? $rendered_text : _escape_and_linkify_status_text($segment->{text});
Branch: 1 branch
BranchTrueFalseTotal
defined $rendered_text ? : 10 14 24
239 }
240 7 return $result_text;
241 },
242 31 ▶ bb_attached_image_urls => sub {
Subroutine __ANON__: called CC=1 SCAR=0.4
243 31 my ($status) = @_;
244 31 my $urls_builder = $self->{main_obj}->get_timeline_config($timeline_name, "attached_image_urls_builder");
245 31 ▶ my @image_urls = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
246 31 $urls_builder->($status);
247 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
248 0 my ($e) = @_;
249 0 bblog("error", "Error in attached_image_urls_builder: $e");
250 0 ();
251 31 };
252 31 return [grep { _is_valid_link_url($_) } @image_urls ];
253 },
254 27 };
255 }
256  
257 {
258 7 my $timezone_cache = Cache::Memory::Simple->new();
259 7 my $CACHE_EXPIRATION_TIME = 3600 * 24;
260 7 my $CACHE_SIZE_LIMIT = 100;
261 12 ▶ sub _get_timezone {
Subroutine _get_timezone: called CC=3 SCAR=15.0
262 12 my ($self, $timezone_string) = @_;
263 12 ▶ if($timezone_cache->count > $CACHE_SIZE_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timezone_cache->count > $CACHE_SIZE_LIMIT) 0 12 12
264 0 $timezone_cache->purge();
265 0 ▶ if($timezone_cache->count > $CACHE_SIZE_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timezone_cache->count > $CACHE_SIZE_LIMIT) 0 0 0
266 0 $timezone_cache->delete_all();
267 }
268 }
269 12 ▶ return $timezone_cache->get_or_set($timezone_string, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
270 12 return DateTime::TimeZone->new(name => $timezone_string),
271 12 }, $CACHE_EXPIRATION_TIME);
272 }
273 }
274  
275 7 ▶sub _escape_and_linkify_status_text {
Subroutine _escape_and_linkify_status_text: called CC=2 SCAR=6.9
276 14 my ($text) = @_;
277 14 my $result_text = "";
278 14 my $remaining_index = 0;
279 14 ▶ while($text =~ m/\G(.*?)($REGEXP_HTTP_URL)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G(.*?)($REGEXP_HTTP_URL)/gs) 3 14 17
280 3 my ($other_text, $url) = ($1, $2);
281 3 $result_text .= html_escape($other_text);
282 3 $result_text .= _html_link_status_text($url, $url);
283 3 $remaining_index = pos($text);
284 }
285 14 $result_text .= html_escape(substr($text, $remaining_index));
286 14 return $result_text;
287 }
288  
289 25 ▶sub _format_status_html_destructive {
Subroutine _format_status_html_destructive: called CC=6 SCAR=18.0
290 25 my ($self, $status, $timeline_name) = @_;
291 25 ▶ $timeline_name = "" if not defined $timeline_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline_name 0 25 25
292 25 ▶ if(ref($status->{retweeted_status}) eq "HASH" && (!defined($status->{busybird}) || ref($status->{busybird}) eq 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$status{"retweeted_status"} eq "HASH" and !defined($$status{"busybird"}) || ref $$status{"busybird"} eq "HASH") 1 24 25
Condition83%5/6
ref $$status{"retweeted_status"} eq "HASH" and !defined($$status{"busybird"}) || ref $$status{"busybird"} eq "HASH"
!ll&&!rl&&r
2311
not defined $$status{"busybird"} or ref $$status{"busybird"} eq "HASH"
l!l&&r!l&&!r
011
MC/DC66%2/3
ref $$status{"retweeted_status"} eq "HASH" and !defined($$status{"busybird"}) || ref $$status{"busybird"} eq "HASH"
ref $$status{"retweeted_status"} eq "HASH" not defined $$status{"busybird"} ref $$status{"busybird"} eq "HASH"
Truth table75%3/4
A and not(B) or C
A= ref $$status{"retweeted_status"} eq "HASH"B= not defined $$status{"busybird"}C= ref $$status{"busybird"} eq "HASH"
ABCresult
0XX0
1000
11X1
1011
293 1 my $retweet = $status->{retweeted_status};
294 1 $status->{busybird}{retweeted_by_user} = $status->{user};
295 1 foreach my $key (qw(text created_at user entities)) {
296 4 $status->{$key} = $retweet->{$key};
297 }
298 }
299 return $self->{renderer}->render(
300 "status.tx",
301 {ss => safed($status),
302 25 %{$self->template_functions_for_timeline($timeline_name)}}
303 );
304 }
305  
306 my %RESPONSE_FORMATTER_FOR_TL_GET_STATUSES = (
307 24 ▶ html => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
308 24 my ($self, $timeline_name, $code, $response_object) = @_;
309 24 ▶ if($code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 200) { } 22 2 24
310 22 my $result = "";
311 22 foreach my $status (@{$response_object->{statuses}}) {
312 25 $result .= $self->_format_status_html_destructive($status, $timeline_name);
313 }
314 22 $result = Encode::encode('utf8', $result);
315 22 return [200, ['Content-Type', 'text/html; charset=utf8'], [$result]];
316 }else {
317 2 return $self->response_error_html($code, $response_object->{error});
318 }
319 },
320 59 ▶ json => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
321 59 my ($self, $timeline_name, $code, $response_object) = @_;
322 59 return $self->response_json($code, $response_object);
323 },
324 4 ▶ json_only_statuses => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
325 4 my ($self, $timeline_name, $code, $response_object) = @_;
326 4 ▶ my $res_array = defined($response_object->{error}) ? [] : $response_object->{statuses};
Branch: 1 branch
BranchTrueFalseTotal
defined $$response_object{'error'} ? : 2 2 4
327 4 ▶ my $res_msg = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
328 4 to_json($res_array, {ascii => 1});
329 0 ▶ }catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
330 0 "[]"
331 4 };
332 return [
333 4 $code, ['Content-Type' => 'application/json; charset=utf-8'],
334 [$res_msg]
335 ];
336 },
337 7 );
338  
339 87 ▶sub response_statuses {
Subroutine response_statuses: called CC=7 SCAR=19.8
Pod documented
340 87 my ($self, %args) = @_;
341 87 ▶ if(!defined($args{statuses}) && !defined($args{error})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"statuses"} or defined $args{"error"}) 0 87 87
Condition66%2/3
defined $args{"statuses"} or defined $args{"error"}
l!l&&r!l&&!r
8160
MC/DC0%0/2
defined $args{"statuses"} or defined $args{"error"}
defined $args{"statuses"} defined $args{"error"}
Truth table66%2/3
A or B
A= defined $args{"statuses"}B= defined $args{"error"}
ABresult
1X1
011
000
342 0 croak "stautses or error parameter is mandatory";
343 }
344 87 foreach my $param_key (qw(http_code format)) {
345 174 ▶ croak "$param_key parameter is mandatory" if not defined($args{$param_key});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$param_key} 0 174 174
346 }
347 87 my $formatter = $RESPONSE_FORMATTER_FOR_TL_GET_STATUSES{lc($args{format})};
348 87 ▶ if(!defined($formatter)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $formatter) 1 86 87
349 1 $formatter = $RESPONSE_FORMATTER_FOR_TL_GET_STATUSES{html};
350 1 delete $args{statuses};
351 1 $args{error} = "Unknown format: $args{format}";
352 1 $args{http_code} = 400;
353 }
354 return $formatter->($self, $args{timeline_name}, $args{http_code},
355 defined($args{error}) ? {error => $args{error}}
356 87 ▶ : {error => undef, statuses => $args{statuses}});
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'error'} ? : 7 80 87
357 }
358  
359 my %TIMELINE_CONFIG_FILTER_FOR = (
360 13 ▶ timeline_web_notifications => sub { defined($_[0]) ? "$_[0]" : ""},
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 13 0 13
361 13 ▶ acked_statuses_load_count => sub { $_[0] =~ /^\d+$/ ? $_[0] : undef },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\d+$/ ? : 13 0 13
362 13 ▶ default_level_threshold => sub { $_[0] =~ /^\d+$/ ? $_[0] : undef},
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\d+$/ ? : 13 0 13
363 7 );
364  
365 13 ▶sub _create_timeline_config_json {
Subroutine _create_timeline_config_json: called CC=2 SCAR=6.9
366 13 my ($self, $timeline_name) = @_;
367 13 my %config = ();
368 13 foreach my $key (keys %TIMELINE_CONFIG_FILTER_FOR) {
369 39 my $config_filter = $TIMELINE_CONFIG_FILTER_FOR{$key};
370 39 my $orig_value = $self->{main_obj}->get_timeline_config($timeline_name, $key);
371 39 $config{$key} = $config_filter->($orig_value);
372 }
373 13 return to_json(\%config);
374 }
375  
376 21 ▶sub response_timeline {
Subroutine response_timeline: called CC=2 SCAR=6.9
Pod documented
377 21 my ($self, $timeline_name) = @_;
378 21 my $timeline = $self->{main_obj}->get_timeline($timeline_name);
379 21 ▶ return $self->response_notfound("Cannot find $timeline_name") if not defined($timeline);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeline 8 13 21
380
381 return $self->_response_template(
382 template => "timeline.tx",
383 args => {
384 timeline_name => $timeline_name,
385 timeline_config_json => $self->_create_timeline_config_json($timeline_name),
386 post_button_url => $self->{main_obj}->get_timeline_config($timeline_name, "post_button_url"),
387 attached_image_max_height => $self->{main_obj}->get_timeline_config($timeline_name, "attached_image_max_height"),
388 attached_image_show_default_bool =>
389 13 ($self->{main_obj}->get_timeline_config($timeline_name, "attached_image_show_default") eq "visible")
390 }
391 );
392 }
393  
394 14 ▶sub response_timeline_list {
Subroutine response_timeline_list: called CC=8 SCAR=20.8
Pod documented
395 14 my ($self, %args) = @_;
396 14 foreach my $key (qw(timeline_unacked_counts total_page_num cur_page)) {
397 42 ▶ croak "$key parameter is mandatory" if not defined $args{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$key} 0 42 42
398 }
399 14 ▶ croak "timeline_unacked_counts must be an array-ref" if ref($args{timeline_unacked_counts}) ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{"timeline_unacked_counts"} ne "ARRAY" 0 14 14
400
401 14 my %input_args = (last_page => $args{total_page_num} - 1);
402 14 foreach my $input_key (qw(cur_page)) {
403 14 $input_args{$input_key} = $args{$input_key};
404 }
405
406 $input_args{timeline_unacked_counts_json} = [map {
407 35 +{name => $_->{name}, counts_json => to_json($_->{counts})}
408 14 } @{$args{timeline_unacked_counts}}];
409
410 14 my $pager_entry_max = $self->{main_obj}->get_config('timeline_list_pager_entry_max');
411 14 my $left_margin = int($pager_entry_max / 2);
412 14 my $right_margin = $pager_entry_max - $left_margin;
413 $input_args{page_list} =
414 $args{total_page_num} <= $pager_entry_max ? [0 .. ($args{total_page_num} - 1)]
415 : $args{cur_page} <= $left_margin ? [0 .. ($pager_entry_max - 1)]
416 : $args{cur_page} >= ($args{total_page_num} - $right_margin)
417 ? [($args{total_page_num} - $pager_entry_max) .. ($args{total_page_num} - 1)]
418 14 ▶ : [($args{cur_page} - $left_margin) .. ($args{cur_page} + $right_margin - 1)];
Branch: 3 branches
BranchTrueFalseTotal
$args{'total_page_num'} <= $pager_entry_max ? : 11 3 14
$args{'cur_page'} <= $left_margin ? : 1 2 3
$args{'cur_page'} >= $args{'total_page_num'} - $right_margin ? : 1 1 2
419 166 ▶ $input_args{page_path} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
420 166 my ($page) = @_;
421 166 return _make_path($self->{script_name}, "/?page=$page");
422 14 };
423 14 return $self->_response_template(
424 template => "timeline_list.tx",
425 args => \%input_args,
426 );
427 }
428  
429 7 1;
430  
431 __END__
« BusyBird/Filter/Twitter.pm ↑ Summary BusyBird/Config.pm »