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.4';
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.4
161  
162 =cut
163  
164 =head1 SYNOPSIS
165  
166 use HTML::SocialMeta;
167 # summary or featured image
168 my $social = HTML::SocialCards->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 my $meta_tags = $social->create('summary | featured_image | app | player');
181  
182 # returns meta tags specificly for a single provider
183 my $twitter_tags = $social->twitter;
184 my $opengraph_tags = $social->opengraph;
185  
186 my $twitter->create('summary' | 'featured_image' | 'player' | 'app');
187  
188  
189 =head1 SUBROUTINES/METHODS
190  
191 =head2 create
192  
193 Generates meta tags for all providers, takes a card_type and converts it into a provider specific card
194  
195 twitter opengraph
196 * summary summary thumbnail
197 * featured_image summary_large_image article
198 * player player video
199 * app app product
200  
201 =cut
202  
203 =head2 build_meta_tags
204  
205 This builds the meta tags for the meta providers
206  
207 It takes an array of fields, which loops through firstly checking
208 that we have a value set and then actually building the specific tag
209 for that field.
210  
211 =cut
212  
213 =head2 required_fields
214  
215 returns an array of the fields that are required to build a specific card
216  
217 =cut
218  
219 =head1 AUTHOR
220  
221 Robert Acock <ThisUsedToBeAnEmail@gmail.com>
222 Robert Haliday <robh@cpan.org>
223  
224 =head1 TODO
225
226 * Improve tests
227 * Add support for more social Card Types / Meta Providers
228
229 =head1 BUGS AND LIMITATIONS
230
231 Most probably. Please report any bugs at http://rt.cpan.org/.
232  
233 =head1 INCOMPATIBILITIES
234  
235 =head1 DEPENDENCIES
236  
237 Moose - Version 2.0604
238 Namespace::Autoclean - Verstion 0.15
239 List::MoreUtils - Version 0.413
240  
241 =head1 CONFIGURATION AND ENVIRONMENT
242  
243 =head1 DIAGNOSTICS
244  
245 =head1 LICENSE AND COPYRIGHT
246
247 Copyright 2015 Robert Acock.
248
249 This program is free software; you can redistribute it and/or modify it
250 under the terms of the the Artistic License (2.0). You may obtain a
251 copy of the full license at:
252
253 L<http://www.perlfoundation.org/artistic_license_2_0>
254
255 Any use, modification, and distribution of the Standard or Modified
256 Versions is governed by this Artistic License. By using, modifying or
257 distributing the Package, you accept this license. Do not use, modify,
258 or distribute the Package, if you do not accept this license.
259
260 If your Modified Version has been derived from a Modified Version made
261 by someone other than you, you are nevertheless required to ensure that
262 your Modified Version complies with the requirements of this license.
263
264 This license does not grant you the right to use any trademark, service
265 mark, tradename, or logo of the Copyright Holder.
266
267 This license includes the non-exclusive, worldwide, free-of-charge
268 patent license to make, have made, use, offer to sell, sell, import and
269 otherwise transfer the Package with respect to any patent claims
270 licensable by the Copyright Holder that are necessarily infringed by the
271 Package. If you institute patent litigation (including a cross-claim or
272 counterclaim) against any party alleging that the Package constitutes
273 direct or contributory patent infringement, then this Artistic License
274 to you shall terminate on the date that such litigation is filed.
275
276 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
277 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
278 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
279 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
280 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
281 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
282 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
283 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284  
« SocialMeta/Twitter.pm ↑ Summary SocialMeta/OpenGraph.pm »