SocialMeta/Base.pm

filter: Statementstmt 91.2% 52 / 57 Branchbran 66.6% 8 / 12 Conditioncond 66.6% 6 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 84.6% 11 / 13 Podpod 75.0% 3 / 4 totaltotal 79.2% 80 / 101 CC 12 SCAR 8.7
CC
12
Cov
85%
create
3.2
build_meta_tags
3.1
_build_field
3.1
CRAP 2.4
« SocialMeta/Twitter.pm ↑ Summary SocialMeta/OpenGraph.pm »
1 package HTML::SocialMeta::Base;
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 our $VERSION = '0.5';
6  
7 # A list of fields which the cards may possibly use
8 5 has [qw(card_type card type name url)] => (
9 isa => 'Str',
10 is => 'rw',
11 lazy => 1,
12 default => q{},
13 );
14  
15 5 has [
16 qw(site fb_app_id site_name title description image creator operatingSystem app_country app_name app_id app_url player player_height player_width)
17 ] => (
18 is => 'ro',
19 isa => 'Str',
20 lazy => 1,
21 default => q{},
22 );
23  
24 # default should be overridden by sub-classes
25 has [qw(card_options build_fields)] => (
26 is => 'ro',
27 isa => 'HashRef',
28 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 5 );
30  
31 sub create {
32 21 ▶ my ( $self, $card_type ) = @_;
Subroutine create: called CC=3 SCAR=11.8
Pod documented
33  
34 21 ▶ $card_type ||= $self->card_type;
Condition66%2/3
$card_type ||= $self->card_type
l!l&&r!l&&!r
2010
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
35  
36 21 ▶ if ( my $option = $self->card_options->{$card_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = $self->card_options->{$card_type}) 21 0 21
37  
38 21 return $self->$option;
39 }
40  
41 0 return $self->_no_card_type($card_type);
42 }
43  
44 sub build_meta_tags {
45 31 ▶ my ( $self, $field_type ) = @_;
Subroutine build_meta_tags: called CC=3 SCAR=11.2
Pod documented
46  
47 31 my @meta_tags;
48  
49 31 ▶ if ( $self->meta_attribute eq q{itemprop} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->meta_attribute eq "itemprop") 0 31 31
50 0 push @meta_tags, $self->item_type;
51 }
52  
53 31 foreach my $field ( $self->required_fields($field_type) ) {
54  
55 # check the field has a value set
56 201 $self->_validate_field_value($field);
57  
58 199 push @meta_tags, $self->_generate_meta_tag($field);
59 }
60  
61 29 return join "\n", @meta_tags;
62 }
63  
64 sub required_fields {
65 37 ▶ my ( $self, $field ) = @_;
Subroutine required_fields: called CC=2 SCAR=7.1
Pod documented
66  
67 return
68 exists $self->build_fields->{$field}
69 37 ▶ ? @{ $self->build_fields->{$field} }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->build_fields->{$field} ? : 37 0 37
70 : ();
71 }
72  
73 sub _validate_field_value {
74 201 ▶ my ( $self, $field ) = @_;
Subroutine _validate_field_value: called CC=2 SCAR=6.9
75  
76 # look to see we have the fields attribute set
77 201 ▶ croak q{you have not set this field value } . $field
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$field 2 199 201
78 if !$self->$field;
79  
80 199 return;
81 }
82  
83 sub _generate_meta_tag {
84 199 ▶ my ( $self, $field ) = @_;
Subroutine _generate_meta_tag: called CC=3 SCAR=11.0
85  
86 # fields that don't start with app or player generate a single tag
87 199 ▶ return $self->_build_field( { field => $field } )
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /(?:app|player|fb)/msx 145 54 199
88 if $field !~ m{(?:app|player|fb)}xms;
89  
90 # fields that start with app or player generate multiple tags
91 54 my @tags = ();
92  
93 54 for ( @{ $self->_convert_field($field) } ) {
94 66 push @tags, $self->_build_field( { field => $field, %{$_} } );
95 }
96  
97 54 return @tags;
98 }
99  
100 sub _build_field {
101 211 ▶ my ( $self, $args ) = @_;
Subroutine _build_field: called CC=3 SCAR=11.2
102  
103 211 ▶ my $field_type = $args->{field_type} || $args->{field};
Condition66%2/3
$args->{'field_type'} || $args->{'field'}
l!l&&r!l&&!r
661450
MC/DC0%0/2
$args->{'field_type'} || $args->{'field'}
$args->{'field_type'} $args->{'field'}
Truth table66%2/3
A || B
A= $args->{'field_type'}B= $args->{'field'}
ABresult
1X1
011
000
104 my $meta_namespace =
105 211 ▶ $args->{ignore_meta_namespace} || $self->meta_namespace;
Condition66%2/3
$args->{'ignore_meta_namespace'} || $self->meta_namespace
l!l&&r!l&&!r
121990
MC/DC0%0/2
$args->{'ignore_meta_namespace'} || $self->meta_namespace
$args->{'ignore_meta_namespace'} $self->meta_namespace
Truth table66%2/3
A || B
A= $args->{'ignore_meta_namespace'}B= $self->meta_namespace
ABresult
1X1
011
000
106 211 my $field = $args->{field};
107  
108 211 return sprintf q{<meta %s="%s:%s" content="%s"/>},
109 $self->meta_attribute, $meta_namespace, $field_type, $self->$field;
110 }
111  
112 sub _convert_field {
113 54 ▶ my ( $self, $field ) = @_;
Subroutine _convert_field: called CC=1 SCAR=0.0
114  
115 54 $field =~ tr/_/:/;
116  
117 54 return $self->provider_convert($field);
118 }
119  
120 sub meta_option {
121 6 ▶ my ( $self, $card_type ) = @_;
Subroutine meta_option: called CC=2 SCAR=7.0
Pod undocumented
122  
123 6 ▶ if ( my $option = $self->card_options->{$card_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = $self->card_options->{$card_type}) 6 0 6
124  
125 # remove create_ and we have the card type
126 6 $option =~ s{^create_}{}xms;
127 6 return $option;
128 }
129 }
130  
131 sub _no_card_type {
132 0 ▶ my ( $self, $card_type ) = @_;
Subroutine _no_card_type: not called CC=1 SCAR=6.9
133 0 return croak
134 q{this card type does not exist try one of these summary, featured_image, app, player};
135 }
136  
137 #
138 # The End
139 #
140 5 __PACKAGE__->meta->make_immutable;
141  
142 5 1;
143  
144 __END__
145  
146 =head1 NAME
147  
148 HTML::SocialMeta::Base
149
150 =head1 DESCRIPTION
151  
152 Base class for the different meta classes.
153  
154 builds and returns the Meta Tags
155  
156 =cut
157  
158 =head1 VERSION
159  
160 Version 0.5
161  
162 =cut
163  
164 =head1 SYNOPSIS
165  
166 use HTML::SocialMeta;
167 # summary or featured image
168 my $social = HTML::SocialMeta->new(
169 site => '',
170 site_name => '',
171 title => '',
172 description => '',
173 image => '',
174 url => '', # optional
175 ... => '',
176 ... => '',
177 );
178  
179 # returns meta tags for all providers
180 # 'summary', 'featured_image', 'app', 'player'
181 my $meta_tags = $social->create('summary');
182  
183 # returns meta tags specificly for a single provider
184 my $twitter_tags = $social->twitter;
185 my $opengraph_tags = $social->opengraph;
186  
187 # 'summary', 'featured_image', 'app', 'player'
188 my $twitter->create('summary');
189  
190  
191 =head1 SUBROUTINES/METHODS
192  
193 =head2 create
194  
195 Generates meta tags for all providers, takes a card_type and converts it into a provider specific card
196  
197 twitter opengraph
198 * summary summary thumbnail
199 * featured_image summary_large_image article
200 * player player video
201 * app app product
202  
203 =cut
204  
205 =head2 build_meta_tags
206  
207 This builds the meta tags for the meta providers
208  
209 It takes an array of fields, which loops through firstly checking
210 that we have a value set and then actually building the specific tag
211 for that field.
212  
213 =cut
214  
215 =head2 required_fields
216  
217 returns an array of the fields that are required to build a specific card
218  
219 =cut
220  
221 =head1 AUTHOR
222  
223 Robert Acock <ThisUsedToBeAnEmail@gmail.com>
224 Robert Haliday <robh@cpan.org>
225  
226 =head1 TODO
227
228 * Improve tests
229 * Add support for more social Card Types / Meta Providers
230
231 =head1 BUGS AND LIMITATIONS
232
233 Most probably. Please report any bugs at http://rt.cpan.org/.
234  
235 =head1 INCOMPATIBILITIES
236  
237 =head1 DEPENDENCIES
238  
239 Moose - Version 2.0604
240 Namespace::Autoclean - Verstion 0.15
241 List::MoreUtils - Version 0.413
242  
243 =head1 CONFIGURATION AND ENVIRONMENT
244  
245 =head1 DIAGNOSTICS
246  
247 =head1 LICENSE AND COPYRIGHT
248
249 Copyright 2015 Robert Acock.
250
251 This program is free software; you can redistribute it and/or modify it
252 under the terms of the the Artistic License (2.0). You may obtain a
253 copy of the full license at:
254
255 L<http://www.perlfoundation.org/artistic_license_2_0>
256
257 Any use, modification, and distribution of the Standard or Modified
258 Versions is governed by this Artistic License. By using, modifying or
259 distributing the Package, you accept this license. Do not use, modify,
260 or distribute the Package, if you do not accept this license.
261
262 If your Modified Version has been derived from a Modified Version made
263 by someone other than you, you are nevertheless required to ensure that
264 your Modified Version complies with the requirements of this license.
265
266 This license does not grant you the right to use any trademark, service
267 mark, tradename, or logo of the Copyright Holder.
268
269 This license includes the non-exclusive, worldwide, free-of-charge
270 patent license to make, have made, use, offer to sell, sell, import and
271 otherwise transfer the Package with respect to any patent claims
272 licensable by the Copyright Holder that are necessarily infringed by the
273 Package. If you institute patent litigation (including a cross-claim or
274 counterclaim) against any party alleging that the Package constitutes
275 direct or contributory patent infringement, then this Artistic License
276 to you shall terminate on the date that such litigation is filed.
277
278 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
279 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
280 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
281 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
282 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
283 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
284 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
285 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286  
« SocialMeta/Twitter.pm ↑ Summary SocialMeta/OpenGraph.pm »