SocialMeta.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 2 / 2 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 50.0% 2 / 4 totaltotal 91.3% 53 / 58 CC 3 SCAR 3.1
CC
3
Cov
98%
create
2.0
build_opengraph
2.0
BEGIN
1.0
CRAP 1.4
« SocialMeta/OpenGraph.pm ↑ Summary
1 package HTML::SocialMeta;
2 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use HTML::SocialMeta::Twitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use HTML::SocialMeta::OpenGraph;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '0.4';
10  
11 5 has 'card_type' => ( isa => 'Str', is => 'rw', lazy => 1, default => q{} );
12 5 has [
13 qw(card site site_name title description image url creator operatingSystem app_country app_name app_id app_url player player_height player_width fb_app_id)
14 ] => (
15 is => 'ro',
16 isa => 'Str',
17 lazy => 1,
18 default => q{},
19 );
20  
21 5 has 'twitter' => (
22 isa => 'HTML::SocialMeta::Twitter',
23 is => 'ro',
24 lazy => 1,
25 builder => 'build_twitter',
26 );
27  
28 5 has 'opengraph' => (
29 isa => 'HTML::SocialMeta::OpenGraph',
30 is => 'ro',
31 lazy => 1,
32 builder => 'build_opengraph',
33 );
34  
35 sub create {
36 5 ▶ my ( $self, $card_type ) = @_;
Subroutine create: called CC=2 SCAR=7.0
Pod documented
37  
38 5 ▶ $card_type ||= $self->card_type;
Condition66%2/3
$card_type ||= $self->card_type
l!l&&r!l&&!r
410
MC/DC0%0/2
$card_type ||= $self->card_type
$card_type $self->card_type
Truth table66%2/3
A ||= B
A= $card_typeB= $self->card_type
ABresult
1X1
011
000
39  
40 5 $self->card_type($card_type);
41  
42 my @meta_tags =
43 5 map { $self->$_->create( $self->card_type ) } qw/twitter opengraph/;
44  
45 5 return join "\n", @meta_tags;
46 }
47  
48 sub required_fields {
49 3 ▶ my ( $self, $card_type ) = @_;
Subroutine required_fields: called CC=1 SCAR=0.0
Pod documented
50  
51 my @meta_tags =
52 3 map { $self->$_->required_fields( $self->$_->meta_option($card_type) ) }
53 qw/twitter opengraph/;
54  
55 3 my @required_fields = uniq(@meta_tags);
56  
57 3 return @required_fields;
58 }
59  
60 sub build_twitter {
61 8 ▶ my $self = shift;
Subroutine build_twitter: called CC=1 SCAR=0.0
Pod undocumented
62  
63 8 return HTML::SocialMeta::Twitter->new(
64 card_type => $self->card_type,
65 site => $self->site,
66 title => $self->title,
67 description => $self->description,
68 image => $self->image,
69 url => $self->url,
70 creator => $self->creator,
71 operatingSystem => $self->operatingSystem,
72 app_country => $self->app_country,
73 app_name => $self->app_name,
74 app_id => $self->app_id,
75 app_url => $self->app_url,
76 player => $self->player,
77 player_width => $self->player_width,
78 player_height => $self->player_height,
79 );
80 }
81  
82 sub build_opengraph {
83 7 ▶ my $self = shift;
Subroutine build_opengraph: called CC=2 SCAR=6.9
Pod undocumented
84  
85 7 ▶ my $url = $self->app_url ? $self->app_url : $self->url;
Branch: 1 branch
BranchTrueFalseTotal
$self->app_url ? : 3 4 7
86  
87 7 return HTML::SocialMeta::OpenGraph->new(
88 card_type => $self->card_type,
89 site_name => $self->site_name,
90 title => $self->title,
91 description => $self->description,
92 image => $self->image,
93 url => $url,
94 operatingSystem => $self->operatingSystem,
95 player => $self->player,
96 player_width => $self->player_width,
97 player_height => $self->player_height,
98 fb_app_id => $self->fb_app_id,
99 );
100 }
101  
102 #
103 # The End
104 #
105 5 __PACKAGE__->meta->make_immutable;
106  
107 5 1;
108  
109 __END__
110  
111 =head1 NAME
112  
113 HTML::SocialMeta - Module to generate Social Media Meta Tags,
114  
115 =head1 VERSION
116  
117 Version 0.4
118  
119 =head1 SYNOPSIS
120  
121 use HTML::SocialMeta;
122 # summary or featured image card setup
123 my $social = HTML::SocialCards->new(
124 site => '',
125 site_name => '',
126 title => '',
127 description => '',
128 image => '',
129 fb_app_id => '',
130 url => '', # optional
131 ... => '',
132 ... => '',
133 );
134  
135 # returns meta tags for all providers
136 my $meta_tags = $social->create('summary | featured_image | app | player');
137  
138 # returns meta tags specificly for a single provider
139 my $twitter_tags = $social->twitter;
140 my $opengraph_tags = $social->opengraph;
141  
142 my $twitter->create('summary' | 'featured_image' | 'player' | 'app');
143
144 # Alternatively call a card directly
145 my $summary_card = $meta_tags->twitter->create_summary;
146
147 ....
148 # You then need to insert these meta tags in the head of your html,
149 # one way of implementing this if you are using Catalyst and Template Toolkit would be ..
150
151 # controller
152 $c->stash->{meta_tags} = $meta_tags;
153
154 # template
155 [% meta_tags | html %]
156  
157 =head1 DESCRIPTION
158  
159 This module generates social meta tags.
160  
161 i.e $social->create('summary') will generate:
162
163 <meta name="twitter:card" content="summary"/>
164 <meta name="twitter:site" content="@example_twitter"/>
165 <meta name="twitter:title" content="You can have any title you wish here"/>
166 <meta name="twitter:description" content="Description goes here may have to do a little validation"/>
167 <meta name="twitter:image" content="www.urltoimage.com/blah.jpg"/>
168 <meta property="og:type" content="thumbnail"/>
169 <meta property="og:title" content="You can have any title you wish here"/>
170 <meta property="og:description" content="Description goes here may have to do a little validation"/>
171 <meta property="og:url" content="www.someurl.com"/>
172 <meta property="og:image" content="www.urltoimage.com/blah.jpg"/>
173 <meta property="og:site_name" content="Example Site, anything"/>
174 <meta property="fb:app_id" content="123433223543"/>'
175  
176 It allows you to optimize sharing on several social media platforms such as Twitter, Facebook, Google+
177 and Pinerest by defining exactly how titles, descriptions, images and more appear in social streams.
178  
179 It generates all the required META data for the following Providers:
180  
181 * Twitter
182 * OpenGraph
183  
184 This module currently allows you to generate the following meta cards:
185  
186 $social->create() $twitter->create_ $opengraph->create_
187 summary summary thumbnail
188 featured_image summary_large_image article
189 player player video
190 app app product
191  
192 =head1 SUBROUTINES/METHODS
193  
194 =head2 Constructor
195  
196 Returns an instance of this class. Requires C<$url> as an argument;
197  
198 =over
199  
200 =item card
201  
202 OPTIONAL - if you always want the same card type you can set it
203  
204 =item site
205  
206 The Twitter @username the card should be attributed to. Required for Twitter Card analytics.
207  
208 =item site name
209  
210 This is Used by Facebook, you can just set it as your organisations name.
211  
212 =item title
213  
214 The title of your content as it should appear in the card
215  
216 =item description
217  
218 A description of the content in a maximum of 200 characters
219  
220 =item image
221  
222 A URL to a unique image representing the content of the page
223  
224 =item url
225  
226 OPTIONAL OPENGRAPH - allows you to specify an alternative url link you want the reader to be redirected
227  
228 =item player
229  
230 HTTPS URL to iframe player. This must be a HTTPS URL which does not generate active mixed content warnings in a web browser
231  
232 =item player_width
233  
234 Width of IFRAME specified in twitter:player in pixels
235  
236 =item player_height
237  
238 Height of IFRAME specified in twitter:player in pixels
239  
240 =item operating_system
241  
242 IOS or Android
243  
244 =item app_country
245  
246 UK/US ect
247  
248 =item app_name
249  
250 The applications name
251  
252 =item app_id
253  
254 String value, and should be the numeric representation of your app ID in the App Store (.i.e. 307234931)
255  
256 =item app_url
257  
258 Application store url - direct link to App store page
259  
260 =item fb_app_id
261  
262 This field is required to use social meta with facebook, you must register your website/app/company with facebook.
263 They will then provide you with a unique app_id.
264  
265 =back
266  
267 =head2 Summary Card
268  
269 The Summary Card can be used for many kinds of web content, from blog posts and news articles, to products and restaurants.
270 It is designed to give the reader a preview of the content before clicking through to your website.
271  
272 ,-----------------------------------,
273 | TITLE *-------* |
274 | | | |
275 | DESCRIPTION | | |
276 | *-------* |
277 *-----------------------------------*
278  
279 Returns an instance for the summary card:
280
281 $meta->create('summary');
282 # call meta provider specifically
283 $card->twitter->create_summary;
284 $card->opengraph->create_thumbnail;
285  
286 fields required:
287  
288 * card
289 * site_name - OpenGraph
290 * site - Twitter Site
291 * title
292 * description
293 * image
294  
295 =head2 Featured Image Card
296  
297 The Featured Image Card features a large, full-width prominent image.
298 It is designed to give the reader a rich photo experience, clicking on the image brings the user to your website.
299  
300 ,-----------------------------------,
301 | *-------------------------------* |
302 | | | |
303 | | | |
304 | | | |
305 | | | |
306 | | | |
307 | | | |
308 | *-------------------------------* |
309 | TITLE |
310 | DESCRIPTION |
311 *-----------------------------------*
312  
313 Returns an instance for the featured image card:
314  
315 $card->create('featured_image');
316 # call meta provider specifically
317 $card->twitter->create_featured_image;
318 $card->opengraph->create_article;
319  
320 Fields Required:
321  
322 * card - Twitter
323 * site - Twitter
324 * site_name - Open Graph
325 * creator - Twitter
326 * title
327 * image
328 * url - Open Graph
329  
330 =cut
331  
332 =head2 Player Card
333  
334 The Player Card allows you to share Video clips and audio stream.
335  
336 ,-----------------------------------,
337 | Title |
338 | link |
339 | *-------------------------------* |
340 | | | |
341 | | | |
342 | | | |
343 | | <play> | |
344 | | | |
345 | | | |
346 | *-------------------------------* |
347 *-----------------------------------*
348  
349 Returns an instance for the player card:
350  
351 $card->create('player');
352 # call meta provider specifically
353 $card->twitter->create_player;
354 $card->opengraph->create_video;
355  
356 Fields Required:
357
358 * site
359 * title
360 * description
361 * image
362 * player
363 * player_width
364 * player_height
365  
366 image to be displayed in place of the player on platforms that does not support iframes or inline players. You should make this image the same dimensions
367 as your player. Images with fewer than 68,600 pixels (a 262 x 262 square image, or a 350 x 196 16:9 image) will cause the player card not to render.
368 Image must be less than 1MB in size
369  
370 =cut
371  
372 =head2 App Card
373  
374 The App Card is a great way to represent mobile applications on Social Media Platforms and to drive installs.
375  
376 ,-----------------------------------,
377 | APP NAME *-------* |
378 | APP INFO | app | |
379 | | image | |
380 | *-------* |
381 | DESCRIPTION |
382 *-----------------------------------*
383  
384 Return an instance for the provider specific app card:
385  
386 $card->create('app);
387 # call meta provider specifically
388 $card->twitter->create_app;
389 $card->opengraph->create_product;
390  
391 Fields Required
392  
393 * site
394 * title
395 * description
396 * operatingSystem
397 * app_country
398 * app_name
399 * app_id
400 * app_url
401  
402 =cut
403  
404 =head2 create
405  
406 Create the Meta Tags - this returns the meta information for all the providers:
407
408 * Twitter
409 * OpenGraph
410 * Google
411
412 You just need to specify the card type on create
413  
414 $social->create('summary | featured_image | app | player');
415  
416 =cut
417  
418 =head2 required_fields
419  
420 Returns a list of fields that are required to build the meta tags
421  
422 $social = HTML->SocialMeta->new();
423 # @fields = qw{}
424 my @fields = $social->required_fields('summary');
425  
426 =cut
427  
428 =head1 BUGS AND LIMITATIONS
429
430 Please report any bugs at http://rt.cpan.org/.
431  
432 =todo
433  
434 Add support for Schema.org Rich Snippets
435 Improve Unit Tests
436 Add support for additional card types
437  
438 =head1 DEPENDENCIES
439  
440 Moose - Version 2.0604
441 Namespace::Autoclean - Verstion 0.15
442 List::MoreUtils - Version 0.413
443  
444 =head1 DIAGNOSTICS
445  
446 A. Twitter Validation Tool
447  
448 https://dev.twitter.com/docs/cards/validation/validator
449  
450 Before your cards show on Twitter, you must first have your domain approved. Fortunately,
451 it's a super-easy process. After you implement your cards, simply enter your sample URL into
452 the validation tool. After checking your markup, select the "Submit for Approval" button.
453  
454 B. Facebook Debugger
455  
456 https://developers.facebook.com/tools/debug
457  
458 You do not need prior approval for your meta information to show on Facebook,
459 but the debugging tool they offer gives you a wealth of information about all your
460 tags and can also analyze your Twitter tags.
461  
462 C. Google Structured Data Testing Tool
463  
464 http://www.google.com/webmasters/tools/richsnippets
465  
466 Webmasters traditionally use the structured data testing tool to test authorship markup and preview
467 how snippets will appear in search results, but you can also use see what other types of
468 meta data Google is able to extract from each page.
469  
470 =head1 AUTHOR
471  
472 Robert Acock <ThisUsedToBeAnEmail@gmail.com>
473 Robert Haliday <robh@cpan.org>
474  
475 =head1 CONFIGURATION AND ENVIRONMENT
476  
477 =head1 INCOMPATIBILITIES
478  
479 =head1 LICENSE AND COPYRIGHT
480
481 Copyright 2015 Robert Acock.
482
483 This program is free software; you can redistribute it and/or modify it
484 under the terms of the the Artistic License (2.0). You may obtain a
485 copy of the full license at:
486
487 L<http://www.perlfoundation.org/artistic_license_2_0>
488
489 Any use, modification, and distribution of the Standard or Modified
490 Versions is governed by this Artistic License. By using, modifying or
491 distributing the Package, you accept this license. Do not use, modify,
492 or distribute the Package, if you do not accept this license.
493
494 If your Modified Version has been derived from a Modified Version made
495 by someone other than you, you are nevertheless required to ensure that
496 your Modified Version complies with the requirements of this license.
497
498 This license does not grant you the right to use any trademark, service
499 mark, tradename, or logo of the Copyright Holder.
500
501 This license includes the non-exclusive, worldwide, free-of-charge
502 patent license to make, have made, use, offer to sell, sell, import and
503 otherwise transfer the Package with respect to any patent claims
504 licensable by the Copyright Holder that are necessarily infringed by the
505 Package. If you institute patent litigation (including a cross-claim or
506 counterclaim) against any party alleging that the Package constitutes
507 direct or contributory patent infringement, then this Artistic License
508 to you shall terminate on the date that such litigation is filed.
509
510 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
511 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
512 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
513 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
514 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
515 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
516 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
517 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
518  
519  
520  
« SocialMeta/OpenGraph.pm ↑ Summary