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