InstantArticle.pm

filter: Statementstmt 80.2% 138 / 172 Branchbran 40.5% 30 / 74 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.8% 32 / 36 Podpod 100% 16 / 16 totaltotal 71.6% 217 / 303 SCAR 44.9
CC
39
Cov
68%
add_lead_asset_video
12.0
add_image
12.0
add_heading
12.0
CRAP 89.4
↑ Summary InstantArticle/Figure.pm »
1 package Facebook::InstantArticle;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use XML::Generator;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Facebook::InstantArticle::Analytics;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Facebook::InstantArticle::Author;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Facebook::InstantArticle::Blockquote;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Facebook::InstantArticle::Copyright;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Facebook::InstantArticle::Embed;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Facebook::InstantArticle::Figure::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Facebook::InstantArticle::Figure::Video;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Facebook::InstantArticle::Heading;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Facebook::InstantArticle::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Facebook::InstantArticle::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Facebook::InstantArticle::Paragraph;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 our $VERSION = '0.13';
21  
22 =encoding utf-8
23  
24 =head1 NAME
25  
26 Facebook::InstantArticle - Helper class for generating Facebook Instant Articles
27 markup.
28  
29 =head1 DESCRIPTION
30  
31 Facebook::InstantArticle is a simple helper class for generating L<Facebook
32 Instant Articles markup|https://developers.facebook.com/docs/instant-articles/reference>.
33  
34 At the moment it doesn't support all of the features, and both the internal and
35 external API are subject to change in upcoming releases, so use with care.
36  
37 =head1 SYNOPSIS
38  
39 use Facebook::InstantArticle;
40 use DateTime;
41  
42 my $now = DateTime->now,
43  
44 my $ia = Facebook::InstantArticle->new(
45 language => 'en',
46 url => 'http://www.example.com/2016/08/17/some-article',
47 title => 'Some title',
48 subtitle => 'Got one?',
49 kicker => 'Nobody needs a kicker, but...',
50 published => "$now",
51 modified => "$now",
52 auto_ad_placement => 0, # defaults to true
53 style => 'MyStyleName',
54 );
55  
56 $ia->add_author(
57 name => 'Me Myself',
58 description => 'A little bit about myself',
59 );
60  
61 $ia->add_author(
62 name => 'Someone Else',
63 description => 'A little bit about someone else',
64 );
65  
66 $ia->add_lead_asset_image(
67 source => 'http://www.example.com/some_image.png',
68 caption => 'Nice image, eh?',
69 );
70  
71 # or
72  
73 $ia->add_lead_asset_video(
74 source => 'http://www.example.com/some_video.mp4',
75 caption => 'Nice video, eh?',
76 );
77  
78 $ia->add_paragraph(
79 'Will be wrapped in a P element, conversion of inner HTML might be
80 done, explained later in this documentation.'
81 );
82  
83 $ia->add_image(
84 source => 'http://www.example.com/some_image.png',
85 caption => 'Nice picture, eh?',
86 enable_comments => 1, # default false
87 enable_likes => 1, # default false
88 );
89  
90 $ia->add_video(
91 source => 'http://www.example.com/some_video.mp4',
92 caption => 'Nice video, eh?',
93 );
94  
95 say $ia->to_string;
96  
97 =cut
98  
99 2 has 'language' => ( isa => 'Str', is => 'rw', required => 1 );
100 2 has 'url' => ( isa => 'Str', is => 'rw', required => 1 );
101 2 has 'title' => ( isa => 'Str', is => 'rw', required => 1 );
102 2 has 'subtitle' => ( isa => 'Str', is => 'rw', required => 0 );
103 2 has 'kicker' => ( isa => 'Str', is => 'rw', required => 0 );
104 2 has 'published' => ( isa => 'Str', is => 'rw', required => 1 );
105 2 has 'modified' => ( isa => 'Str', is => 'rw', required => 1 );
106 2 has 'style' => ( isa => 'Str', is => 'rw', required => 0 );
107 2 has 'auto_ad_placement' => ( isa => 'Bool', is => 'rw', required => 0, default => 1 );
108  
109 1 ▶has '_header_elements' => ( isa => 'ArrayRef[Object]', is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
110 1 ▶has '_body_elements' => ( isa => 'ArrayRef[Object]', is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
111 1 ▶has '_footer_elements' => ( isa => 'ArrayRef[Object]', is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
112 1 ▶has '_credit_elements' => ( isa => 'ArrayRef[Object]', is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
113  
114 1 ▶has '_log' => ( isa => 'Mojo::Log', is => 'ro', default => sub { Mojo::Log->new } );
Subroutine __ANON__: called CC=1 SCAR=0.0
115  
116 =head1 METHODS
117  
118 =head2 add_lead_asset_image
119  
120 Adds a lead asset image to the article.
121  
122 $ia->add_lead_asset_image(
123 source => 'http://www.example.com/lead_image.png',
124 caption => 'Something wicked this way comes...',
125 );
126  
127 =cut
128  
129 sub add_lead_asset_image {
130 1 ▶ my $self = shift;
Subroutine add_lead_asset_image: called CC=3 SCAR=11.8
Pod documented
131  
132 1 ▶ if ( my $e = Facebook::InstantArticle::Figure::Image->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Figure::Image"->new(@_)) 1 0 1
133 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
134 1 return push( @{$self->_header_elements}, $e );
135 }
136 }
137  
138 0 $self->_log->warn( 'Failed to add lead asset image to article header!' );
139 }
140  
141 =head2 add_lead_asset_video
142  
143 Adds a lead asset video to the article.
144  
145 $ia->add_lead_asset_video(
146 source => 'http://www.example.com/lead_video.mp4',
147 caption => 'Something wicked this way comes...',
148 );
149  
150 =cut
151  
152 sub add_lead_asset_video {
153 0 ▶ my $self = shift;
Subroutine add_lead_asset_video: not called CC=3 SCAR=24.8
Pod documented
154  
155 0 ▶ if ( my $e = Facebook::InstantArticle::Figure::Video->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Figure::Video"->new(@_)) 0 0 0
156 0 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 0 0 0
157 0 return push( @{$self->_header_elements}, $e );
158 }
159 }
160  
161 0 $self->_log->warn( 'Failed to add lead asset video to article header!' );
162 }
163  
164 =head2 add_author
165  
166 Adds an author to the article.
167  
168 $ia->add_author(
169 name => 'Oscar Wilde',
170 );
171  
172 =cut
173  
174 sub add_author {
175 1 ▶ my $self = shift;
Subroutine add_author: called CC=3 SCAR=11.8
Pod documented
176  
177 1 ▶ if ( my $e = Facebook::InstantArticle::Author->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Author"->new(@_)) 1 0 1
178 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
179 1 return push( @{$self->_header_elements}, $e );
180 }
181 }
182  
183 0 $self->_log->warn( 'Failed to add author to article header!' );
184 }
185  
186 =head2 add_paragraph
187  
188 Adds a paragraph to the article.
189  
190 $ia->add_paragraph( 'This is a paragraph' );
191  
192 =cut
193  
194 sub add_paragraph {
195 3 ▶ my $self = shift;
Subroutine add_paragraph: called CC=3 SCAR=11.0
Pod documented
196  
197 3 ▶ if ( my $e = Facebook::InstantArticle::Paragraph->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Paragraph"->new(@_)) 3 0 3
198 3 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 2 1 3
199 2 return push( @{$self->_body_elements}, $e );
200 }
201 }
202  
203 1 $self->_log->warn( 'Failed to add paragraph to article body!' );
204 }
205  
206 =head2 add_image
207  
208 Adds an image to the article.
209  
210 $ia->add_image(
211 source => 'http://www.example.com/image.png',
212 caption => 'Some caption...',
213 );
214  
215 =cut
216  
217 sub add_image {
218 0 ▶ my $self = shift;
Subroutine add_image: not called CC=3 SCAR=24.8
Pod documented
219  
220 0 ▶ if ( my $e = Facebook::InstantArticle::Figure::Image->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Figure::Image"->new(@_)) 0 0 0
221 0 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 0 0 0
222 0 return push( @{$self->_body_elements}, $e );
223 }
224 }
225  
226 0 $self->_log->warn( 'Failed to add image to article body!' );
227 }
228  
229 =head2 add_video
230  
231 Adds a video to the article.
232  
233 $ia->add_video(
234 source => 'http://www.example.com/video.mp4',
235 caption => 'Some caption...',
236 );
237  
238 =cut
239  
240 sub add_video {
241 1 ▶ my $self = shift;
Subroutine add_video: called CC=3 SCAR=11.8
Pod documented
242  
243 1 ▶ if ( my $e = Facebook::InstantArticle::Figure::Video->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Figure::Video"->new(@_)) 1 0 1
244 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
245 1 return push( @{$self->_body_elements}, $e );
246 }
247 }
248  
249 0 $self->_log->warn( 'Failed to add video to article body!' );
250 }
251  
252 =head2 add_slideshow
253  
254 Adds a Facebook::InstantArticle::Slideshow object to the article.
255  
256 my $ss = Facebook::InstantArticle::Slideshow->new;
257  
258 $ss->add_image(
259 source => 'http://www.example.com/image_01.png',
260 caption => 'Image #1',
261 );
262  
263 $ss->add_image(
264 source => 'http://www.example.com/image_02.png',
265 caption => 'Image #2',
266 );
267  
268 $ia->add_slideshow( $ss );
269  
270 =cut
271  
272 sub add_slideshow {
273 1 ▶ my $self = shift;
Subroutine add_slideshow: called CC=3 SCAR=12.3
Pod documented
274 1 my $slideshow = shift;
275  
276 1 ▶ if ( defined $slideshow && $slideshow->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slideshow and $slideshow->is_valid) 1 0 1
Condition33%1/3
defined $slideshow and $slideshow->is_valid
!ll&&!rl&&r
001
MC/DC0%0/2
defined $slideshow and $slideshow->is_valid
defined $slideshow $slideshow->is_valid
Truth table33%1/3
A and B
A= defined $slideshowB= $slideshow->is_valid
ABresult
0X0
100
111
277 1 return push( @{$self->_body_elements}, $slideshow );
278 }
279  
280 0 $self->_log->warn( 'Failed to add slideshow to article body!' );
281 }
282  
283 =head2 add_credit
284  
285 Adds a credit to the article.
286  
287 $ia->add_credit( 'Thanks for helping me write this article, someone!' );
288  
289 =cut
290  
291 sub add_credit {
292 1 ▶ my $self = shift;
Subroutine add_credit: called CC=3 SCAR=11.8
Pod documented
293  
294 1 ▶ if ( my $e = Facebook::InstantArticle::Paragraph->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Paragraph"->new(@_)) 1 0 1
295 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
296 1 return push( @{$self->_credit_elements}, $e );
297 }
298 }
299  
300 0 $self->_log->warn( 'Failed to add credit to article credits!' );
301 }
302  
303 =head2 add_copyright
304  
305 Adds a copyright to the article.
306  
307 $ia->add_copyright( 'Copyright 2016, Fubar Inc.' );
308  
309 =cut
310  
311 sub add_copyright {
312 1 ▶ my $self = shift;
Subroutine add_copyright: called CC=3 SCAR=11.8
Pod documented
313  
314 1 ▶ if ( my $e = Facebook::InstantArticle::Copyright->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Copyright"->new(@_)) 1 0 1
315 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
316 1 return push( @{$self->_footer_elements}, $e );
317 }
318 }
319  
320 0 $self->_log->warn( 'Failed to add copyright to article footer!' );
321 }
322  
323 =head2 add_list
324  
325 Adds a Facebook::InstantArticle::List object to the article.
326  
327 $ia->add_list(
328 ordered => 1, # default 0
329 elements => [ 'Element #1', 'Element #2', 'Element 3' ],
330 );
331  
332 =cut
333  
334 sub add_list {
335 1 ▶ my $self = shift;
Subroutine add_list: called CC=3 SCAR=11.8
Pod documented
336  
337 1 ▶ if ( my $e = Facebook::InstantArticle::List->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::List"->new(@_)) 1 0 1
338 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
339 1 return push( @{$self->_body_elements}, $e );
340 }
341 }
342  
343 0 $self->_log->warn( 'Failed to add list to article body!' );
344 }
345  
346 =head2 add_blockquote
347  
348 Adds a blockquote to the article.
349  
350 $ia->add_blockquote( 'This is blockquoted.' );
351  
352 =cut
353  
354 sub add_blockquote {
355 1 ▶ my $self = shift;
Subroutine add_blockquote: called CC=3 SCAR=11.8
Pod documented
356  
357 1 ▶ if ( my $e = Facebook::InstantArticle::Blockquote->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Blockquote"->new(@_)) 1 0 1
358 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
359 1 return push( @{$self->_body_elements}, $e );
360 }
361 }
362  
363 0 $self->_log->warn( 'Failed to add blockquote to article body!' );
364 }
365  
366 =head2 add_embed
367  
368 Adds an embed to the article.
369  
370 $ia->add_embed( 'code' );
371  
372 =cut
373  
374 sub add_embed {
375 1 ▶ my $self = shift;
Subroutine add_embed: called CC=3 SCAR=11.8
Pod documented
376  
377 1 ▶ if ( my $e = Facebook::InstantArticle::Embed->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Embed"->new(@_)) 1 0 1
378 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
379 1 return push( @{$self->_body_elements}, $e );
380 }
381 }
382  
383 0 $self->_log->warn( 'Failed to add embed to article body!' );
384 }
385  
386 =head2 add_heading
387  
388 Adds a heading to the article BODY.
389  
390 $ia->add_heading(
391 level => 1,
392 text => 'Heading',
393 );
394  
395 =cut
396  
397 sub add_heading {
398 0 ▶ my $self = shift;
Subroutine add_heading: not called CC=3 SCAR=24.8
Pod documented
399  
400 0 ▶ if ( my $e = Facebook::InstantArticle::Heading->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Heading"->new(@_)) 0 0 0
401 0 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 0 0 0
402 0 return push( @{$self->_body_elements}, $e );
403 }
404 }
405  
406 0 $self->_log->warn( 'Failed to add heading to article body!' );
407 }
408  
409 =head2 add_map
410  
411 Adds a map to the article BODY.
412  
413 $ia->add_map(
414 latitude => 56.1341342,
415 longitude => 23.253474,
416 );
417  
418 =cut
419  
420 sub add_map {
421 1 ▶ my $self = shift;
Subroutine add_map: called CC=3 SCAR=11.8
Pod documented
422  
423 1 ▶ if ( my $e = Facebook::InstantArticle::Map->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Map"->new(@_)) 1 0 1
424 1 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 1 0 1
425 1 return push( @{$self->_body_elements}, $e );
426 }
427 }
428  
429 0 $self->_log->warn( 'Failed to add map to article body!' );
430 }
431  
432 =head2 add_analytics
433  
434 Adds an analytics iframe to the article body.
435  
436 =cut
437  
438 sub add_analytics {
439 0 ▶ my $self = shift;
Subroutine add_analytics: not called CC=3 SCAR=24.8
Pod documented
440  
441 0 ▶ if ( my $e = Facebook::InstantArticle::Analytics->new(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Facebook::InstantArticle::Analytics"->new(@_)) 0 0 0
442 0 ▶ if ( $e->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_valid) 0 0 0
443 0 return push( @{$self->_body_elements}, $e );
444 }
445 }
446  
447 0 $self->_log->warn( 'Failed to add analytics/tracking to article body!' );
448 }
449  
450 =head2 to_string
451  
452 Generates the instant article and returns it as a string.
453  
454 =cut
455  
456 sub to_string {
457 1 ▶ my $self = shift;
Subroutine to_string: called CC=9 SCAR=23.3
Pod documented
458  
459 # TODO: Validate
460  
461 1 my $gen = XML::Generator->new( ':pretty' );
462  
463 my $xml = $gen->html(
464 { lang => $self->language, prefix => 'op:http://media.facebook.com/op#' },
465  
466 $gen->head(
467 $gen->meta( { charset => 'utf-8' } ),
468 $gen->meta( { property => 'op:markup_version', version => 'v1.0' } ),
469 $gen->meta( { property => 'fb:likes_and_comments', content => 'enable' } ),
470 $gen->meta( { property => 'fb:use_automatic_ad_placement', content => ( $self->auto_ad_placement ? 'enable=true ad_density=default' : 'enable=false' ) } ),
471 ( length $self->style ? $gen->meta( { property => 'fb:article_style', content => $self->style } ) : undef ),
472 $gen->link( { rel => 'canonical', href => $self->url } ),
473 ),
474  
475 $gen->body(
476 $gen->article(
477 $gen->header(
478 $gen->h1( $self->title ),
479 ( length $self->subtitle ? $gen->h2($self->subtitle) : undef ),
480 ( length $self->kicker ? $gen->h3({ class => 'op-kicker' }, $self->kicker) : undef ),
481 $gen->time( { class => 'op-published', datetime => $self->published } ),
482 $gen->time( { class => 'op-modified', datetime => $self->modified } ),
483 1 ▶ ( @{$self->_header_elements} ? map { $_->as_xml_gen } @{$self->_header_elements} : undef ),
Branch: 1 branch
BranchTrueFalseTotal
@{$self->_body_elements;} ? : 1 0 1
484 ),
485 1 ▶ ( @{$self->_body_elements} ? map { $_->as_xml_gen } @{$self->_body_elements} : undef ),
Branch: 1 branch
BranchTrueFalseTotal
@{$self->_credit_elements;} ? : 1 0 1
486 $gen->footer(
487 1 ▶ ( @{$self->_credit_elements} ? $gen->aside(map { $_->as_xml_gen } @{$self->_credit_elements}) : undef ),
Branch: 1 branch
BranchTrueFalseTotal
@{$self->_footer_elements;} ? : 1 0 1
488 1 ▶ ( @{$self->_footer_elements} ? map { $_->as_xml_gen } @{$self->_footer_elements} : undef ),
Branch: 5 branches
BranchTrueFalseTotal
$self->auto_ad_placement ? : 1 0 1
length $self->style ? : 0 1 1
length $self->subtitle ? : 1 0 1
length $self->kicker ? : 1 0 1
@{$self->_header_elements;} ? : 1 0 1
489 ),
490 ),
491 ),
492 );
493  
494 1 return "<!doctype html>\n" . $xml;
495 }
496  
497 2 1;
498  
499 __END__
500  
501 =head1 AUTHOR
502  
503 Tore Aursand E<lt>toreau@gmail.comE<gt>
504  
505 =head1 COPYRIGHT
506  
507 Copyright 2016 - Tore Aursand
508  
509 =head1 LICENSE
510  
511 This library is free software; you can redistribute it and/or modify it under
512 the same terms as Perl itself.
513  
514 =cut
↑ Summary InstantArticle/Figure.pm »