Ads.pm

filter: Statementstmt 33.3% 29 / 87 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 63.6% 7 / 11 Podpod 75.0% 3 / 4 totaltotal 26.9% 39 / 145 CC 19 SCAR 53.6
CC
19
Cov
23%
get_ad
342.0
new
6.0
URLEncode
2.0
CRAP 213.1
↑ Summary Ads/AdMob_v2.pm »
1 # Mobile::Ads.pm version 0.0.2
2 #
3 # Copyright (c) 2008 Thanos Chatziathanassioy <tchatzi@arx.net>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6  
7 package Mobile::Ads;
8 1 local $^W;
9 1 require 'Exporter.pm';
10 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 @ISA = (Exporter);
12 1 @EXPORT = qw(); #&new);
13 1 @EXPORT_OK = qw();
14  
15 1 $Mobile::Ads::VERSION='0.0.2';
16 1 $Mobile::Ads::ver=$Mobile::Ads::VERSION;
17  
18 1 ▶use strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use LWP::UserAgent();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use HTTP::Request::Common();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 NAME
26  
27 Mobile::Ads - base class for Mobile Ads
28  
29 Version 0.0.2
30  
31 =head1 SYNOPSIS
32  
33 use Mobile::Ads::Admob;
34
35 =head1 DESCRIPTION
36  
37 C<Mobile::Ads> provides an object oriented interface to serve advertisements
38 It does nothing by itself and you should probably use one of
39 C<Mobile::Ads::AdMob_v1> (old AdMob implementation, lacks graphical ads)
40 C<Mobile::Ads::AdMob_v2> (newer AdMob, support image ads)
41 C<Mobile::Ads::Admoda>
42 C<Mobile::Ads::AdSense> (Google AdSense for Mobile)
43 C<Mobile::Ads::Adsgr> (ads.gr mobile ads)
44 C<Mobile::Ads::Adultmoda>
45 C<Mobile::Ads::Buzzcity>
46 C<Mobile::Ads::Decktrade>
47 C<Mobile::Ads::GetMobile>
48 C<Mobile::Ads::MoJiva>
49 C<Mobile::Ads::ZastAdz>
50  
51 Refer to their man pages for help (?)
52  
53 =head1 new Mobile::Ads
54  
55 =cut
56  
57 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
58 0 my $this = shift;
59 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
60 0 my $self = {};
61 0 bless $self, $class;
62  
63 #defaults
64 0 $self->{'timeout'} = 2;
65
66 0 $self->{'ua'} = new LWP::UserAgent;
67 0 $self->{'ua'}->timeout($self->{'timeout'});
68 0 $self->{'ua'}->agent("Mobile::Ads/$Mobile::Ads::VERSION/".$self->{'ua'}->_agent);
69 #development aids
70 0 $self->{'DEBUG'} = 0;
71
72 0 return $self;
73 }
74  
75 0 ▶sub get_ad {
Subroutine get_ad: not called CC=18 SCAR=58.3
Pod documented
76 0 my $self = shift;
77
78 0 my ($url,$method,$params) = ('','','');
79 0 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
80 0 $url = $_[0]->{'url'};
81 0 $method = $_[0]->{'method'};
82 0 $params = $_[0]->{'params'};
83 }
84 else {
85 0 ($url,$method,$params) = @_;
86 }
87
88 #test $uri is valid...
89 0 ▶ $url =~ m|^https?://| or Carp::croak("Ads.pm get_ad(): invalid URL $url\n");
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[^https?://] or Carp::croak("Ads.pm get_ad(): invalid URL $url\n") 0 0 0
90
91 0 my $res;
92 # fetch data
93 0 ▶ if ($method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'POST') { } 0 0 0
94 0 ▶ if ($params && ref($params) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($params and ref $params eq 'HASH') { } 0 0 0
Condition0%0/3
$params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$params and ref $params eq "HASH"
$params ref $params eq "HASH"
Truth table0%0/3
A and B
A= $paramsB= ref $params eq "HASH"
ABresult
0X0
100
111
95 0 ▶ $self->{'DEBUG'} and Carp::cluck("POST to $url with $params\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and Carp::cluck("POST to $url with $params\n") 0 0 0
96 0 $res = $self->{'ua'}->request(HTTP::Request::Common::POST $url, $params);
97 }
98 else {
99 0 ▶ $self->{'DEBUG'} and Carp::cluck("POST to $url without params\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and Carp::cluck("POST to $url without params\n") 0 0 0
100 #perhaps no need for $params, but one should still POST to this URL
101 0 $res = $self->{'ua'}->request(HTTP::Request::Common::POST $url);
102 }
103 }
104 else {
105 0 ▶ if ($params && ref($params) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($params and ref $params eq 'HASH') { } 0 0 0
Condition0%0/3
$params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$params and ref $params eq "HASH"
$params ref $params eq "HASH"
Truth table0%0/3
A and B
A= $paramsB= ref $params eq "HASH"
ABresult
0X0
100
111
106 #add $params to the Query_String (remember to URLEncode them, btw)
107 0 ▶ if ($url =~ m|\?|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\?/) { } 0 0 0
108 0 Carp::croak("Ads.pm get_ad() : either construct the QUERY_STRING for $url yourself, or give arguments in \$params, but not both\n");
109 }
110 else {
111 #first add the ``?'' to the URL (making it a URI :)
112 0 my $uri = $url . "?";
113
114 0 my $last = 0; #useful to figure out when to stop adding ``&''s
115
116 0 foreach (keys(%$params)) {
117 0 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) 0 0 0
118 0 $last = 0;
119 0 $uri .= "&";
120 }
121
122 0 $uri .= $self->URLEncode($_);
123 0 ▶ if ($params->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{$_}) 0 0 0
124 0 $uri .= "=".$self->URLEncode($params->{$_});
125 }
126
127 0 $last = 1;
128 }
129 0 ▶ $self->{'DEBUG'} and Carp::cluck("GET to $url with $params -> $uri\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and Carp::cluck("GET to $url with $params -> $uri\n") 0 0 0
130 0 $res = $self->{'ua'}->request(HTTP::Request::Common::GET $uri);
131 }
132 }
133 else {
134 0 ▶ $self->{'DEBUG'} and Carp::cluck("GET to $url without params\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and Carp::cluck("GET to $url without params\n") 0 0 0
135 0 $res = $self->{'ua'}->request(HTTP::Request::Common::GET $url);
136 }
137 }
138
139 0 ▶ if ($res->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
140 0 ▶ $self->{'DEBUG'} and Carp::cluck($res->as_string." is_success\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and Carp::cluck($res->as_string . " is_success\n") 0 0 0
141 0 return($res->content());
142 }
143 else {
144 0 Carp::croak("HTTP Request failed with ".$res->as_string."\n");
145 }
146 }
147  
148 0 ▶sub URLEncode {
Subroutine URLEncode: not called CC=1 SCAR=6.9
Pod documented
149 0 my $self = shift;
150
151 0 my $toencode = shift;
152
153 0 $toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/esg;
154 0 return $toencode;
155 }
156  
157 0 ▶sub XMLEncode {
Subroutine XMLEncode: not called CC=1 SCAR=6.9
Pod documented
158 0 my $self = shift;
159
160 0 my $toencode = shift;
161 0 $toencode =~ s/\&(?!amp\;)/\&amp;/sg;
162 0 $toencode =~ s|\>|\&gt;|sg;
163 0 $toencode =~ s|\<|\&lt;|sg;
164 #only for the sake of completeness...
165 0 $toencode =~ s|\"|\&quot;|sg;
166 0 return $toencode;
167 }
168  
169 =pod
170  
171 =head2 Methods
172  
173 =over 4
174  
175 =item get_ad
176  
177 C<>=> Does the actual HTTP.
178 url is obviously the ad serving site URL,
179 method is either ``POST'' or anything else (in which case a GET is performed)
180 and params is a hash reference with key/value pairs. The module will take care to URLEncode
181 as neccessary or set Content-length and Content-type if POST.
182 Note that you can either construct a GET URI yourself (taking care of encoding and stuff or
183 pass the arguments in params, but not both).
184  
185 Example:
186 $response = $ad->get_ad (
187 {
188 url => 'http://ad.serving.site/ad.php',
189 method => 'GET',
190 params => {
191 'some' => 'params'
192 'can' => 'go here'
193 }
194 });
195  
196 will result in ``http://ad.serving.site/ad.php?some=params&can=go%20here'' being actually sent
197 to the server.
198  
199 Will happily croak() if server is unreachable or not return 200, so eval() as neccessary.
200 Will NOT apply any kind of translation to the returned content. For this, each module should
201 make provisions for itself.
202
203 =item URLEncode
204  
205 C<>=> Shamelessly plugged from Apache::ASP::Server::URLEncode
206  
207 =item XMLEncode
208  
209 C<>=> Just escapes ``&'' where neccessary in its input to make it XML safe.
210 Proably of use to everyone, so put here.
211  
212 =back
213  
214 =cut
215  
216  
217 =head1 Revision History
218  
219 0.0.1
220 Initial Release
221 0.0.2
222 First CPAN released version and the addition of $self->timeout to easily set
223 LWP::UserAgent timeout
224
225 =head1 BUGS
226  
227 Thoughtlessly crafted to avoid having the same piece of code in several places.
228 Could use lots of enhancements.
229  
230 =head1 DISCLAIMER
231  
232 This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
233 which is available on CPAN.
234  
235 =head1 AUTHOR
236  
237 Thanos Chatziathanassiou <tchatzi@arx.net>
238 http://www.arx.net
239  
240 =head1 COPYRIGHT
241  
242 Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
243  
244 This program is free software; you can redistribute it and/or
245 modify it under the same terms as Perl itself.
246  
247 =cut
248  
249 1 1;
↑ Summary Ads/AdMob_v2.pm »