Ads/AdMob_v1.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 175 CC 10 SCAR 36.6
CC
10
Cov
0%
get_v1_ad
72.0
new
12.0
BEGIN
2.0
CRAP 38.9
1 # Mobile::Ads::AdMob_v1.pm version 0.1.0
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::AdMob_v1;
8 local $^W;
9 require 'Exporter.pm';
10 use vars qw(@ISA @EXPORT @EXPORT_OK);
11 @ISA = (Exporter);
12 @EXPORT = qw(); #&new);
13 @EXPORT_OK = qw();
14  
15 $Mobile::Ads::AdMob_v1::VERSION='0.1.0';
16 $Mobile::Ads::AdMob_v1::ver=$Mobile::Ads::AdMob_v1::VERSION;
17  
18 use strict 'vars';
19 use Carp();
20 use Mobile::Ads();
21  
22 =head1 NAME
23  
24 Mobile::Ads::AdMob_v1 - module to serve AdMob ads
25  
26 Version 0.1.0
27  
28 =head1 SYNOPSIS
29  
30 use Mobile::Ads::AdMob_v1;
31 $ad = new Mobile::Ads::AdMob_v1
32 ($text,$link) = $ad->get_v1_ad({
33 site => 'AdMob site code',
34 remote => $ENV{'HTTP_USER_AGENT'},
35 address => $ENV{'REMOTE_ADDR'},
36 text => 'default ad text',
37 link => 'default ad link',
38 test => 'set this if this is a test ad',
39 });
40
41 =head1 DESCRIPTION
42  
43 C<Mobile::Ads::AdMob_v1> provides an object oriented interface to serve advertisements
44 from AdMob in mobile sites.
45 This is just a slightly altered version of the perl code found on AdMob's site.
46  
47 =head1 new Mobile::Ads::AdMob_v1
48  
49 =over 4
50  
51 =item [$parent]
52  
53 To reuse Mobile::Ads in multiple (subsequent) ad requests, you can pass a C<Mobile::Ads>
54 reference here. Instead of creating a new Mobile::Ads object, we will use the one you
55 pass instead. This might save a little C<LWP::UserAgent> creation/destruction time.
56  
57 =head2 Parameters/Properties
58  
59 =over 4
60  
61 =item site
62  
63 C<>=> AdMob site code, delivered by them. Something in the form off ``0123456789abcde''
64  
65 =item remote
66  
67 C<>=> Remote User Agent ($ENV{'HTTP_USER_AGENT'}). In fact $ENV{'HTTP_USER_AGENT'} will be used
68 if not supplied.
69  
70 =item address
71  
72 C<>=> $ENV{'REMOTE_ADDR'}. All things about HTTP_USER_AGENT also apply here.
73  
74 =item text
75  
76 C<>=> Should we fail to retrieve a real ad, this is the text of the ad displayed instead
77  
78 =item link
79  
80 C<>=> Same with text, but for the ad's link.
81  
82 =back
83  
84 =cut
85  
86 sub new {
87 my $this = shift;
88 my $class = ref($this) || $this;
89 my $self = {};
90 bless $self, $class;
91  
92 my $parent = shift;
93
94 if ($parent && ref($parent) && ref($$parent) && ref($$parent) eq "Mobile::Ads") {
95 $self->{'parent'} = $$parent;
96 }
97 elsif ($parent && ref($parent) && ref($parent) eq "Mobile::Ads") {
98 $self->{'parent'} = $parent;
99 }
100 else {
101 $self->{'parent'} = new Mobile::Ads;
102 }
103
104 return $self;
105 }
106  
107 *get_ad = \&get_v1_ad;
108  
109 sub get_v1_ad {
110 my $self = shift;
111
112 my ($site,$test,$remote,$address,$text,$link) = ('','','','','','');
113 if (ref $_[0] eq 'HASH') {
114 $site = $_[0]->{'site'} || $self->{'site'};
115 $test = $_[0]->{'test'};
116 $remote = $_[0]->{'remote'};
117 $address = $_[0]->{'address'};
118 $text = $_[0]->{'text'};
119 $link = $_[0]->{'link'};
120
121 }
122 else {
123 ($site,$test,$remote,$address,$text,$link) = @_;
124 }
125
126 $site ||= $self->{'site'};
127 $remote ||= $ENV{'HTTP_USER_AGENT'};
128 $address ||= $ENV{'REMOTE_ADDR'};
129 $text ||= $self->{'text'};
130 $link ||= $self->{'link'};
131
132 Carp::croak("cant serve ads without site\n") unless ($site);
133 Carp::croak("cant serve ads without remote user agent\n") unless ($remote);
134 Carp::croak("cant serve ads without remote address\n") unless ($address);
135
136 # fetch data
137 my $res;
138 my $params = {
139 s => $site,
140 u => $remote,
141 i => $address,
142 };
143 if ($test) {
144 $params->{'m'} = '';
145 }
146
147 eval q[$res = $self->{'parent'}->get_ad({
148 url => 'http://ads.admob.com/ad_source.php',
149 method => 'GET',
150 params => $params
151 });];
152 if ($@) {
153 $link and $link = $self->{'parent'}->XMLEncode($link);
154 $text and $text = $self->{'parent'}->XMLEncode($text);
155 return ($text,$link);
156 }
157 else {
158 #some error collecting the ad...
159 my ($texta, $linka) = split(/\>\</, $res) ;
160 if ($texta and $linka) {
161 chomp ($texta,$linka);
162 ($text,$link) = ($texta,$linka);
163 }
164 $link and $link = $self->{'parent'}->XMLEncode($link);
165 $text and $text = $self->{'parent'}->XMLEncode($text);
166 return ($text,$link);
167 }
168 }
169  
170 =pod
171  
172 =head2 Methods
173  
174 =over 4
175  
176 =item get_v1_ad
177  
178 C<>=> Does the actual fetching of the ad for the site given. Refer to new for details
179 Returns a list ($text_for_ad,$link_for_ad) value in list context or an
180 ``<a href="$link">$text</a>'' if called in scalar context.
181  
182 =back
183  
184 =cut
185  
186  
187 =head1 Revision History
188  
189 0.0.1
190 Initial Release
191 0.0.2
192 Fixed stupid typo
193 0.0.3
194 Didn't preserve default values on failure
195 0.0.4
196 $ua timeout set to 20 sec
197 0.0.5
198 All ua stuff put in Mobile::Ads
199 0.0.6
200 Aliased get_ad to get_v1_ad
201 0.0.7
202 Option to reuse parent Mobile::Ads instead of creating anew
203 0.0.8/0.0.9
204 Skipped those to have same verion number in all modules
205 0.1.0
206 One could also use a reference to the parent... :)
207  
208 =head1 BUGS
209  
210 Thoughtlessly crafted to avoid having the same piece of code in several places.
211 Could use lots of enhancements.
212  
213 =head1 DISCLAIMER
214  
215 This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
216 which is available on CPAN.
217  
218 =head1 AUTHOR
219  
220 Thanos Chatziathanassiou <tchatzi@arx.net>
221 http://www.arx.net
222  
223 =head1 COPYRIGHT
224  
225 Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
226  
227 This program is free software; you can redistribute it and/or
228 modify it under the same terms as Perl itself.
229  
230 =cut
231  
232 1;