Ads/Zestadz.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 194 CC 14 SCAR 34.9
CC
14
Cov
0%
get_zestadz_ad
56.0
parse
42.0
new
12.0
CRAP 32.9
1 # Mobile::Ads::Zestadz.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::Zestadz;
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::Zestadz::VERSION='0.1.0';
16 $Mobile::Ads::Zestadz::ver=$Mobile::Ads::Zestadz::VERSION;
17  
18 use strict 'vars';
19 use Carp();
20 use Mobile::Ads();
21  
22 =head1 NAME
23  
24 Mobile::Ads::Zestadz - module to serve Zestadz ads
25  
26 Version 0.1.0
27  
28 =head1 SYNOPSIS
29  
30 use Mobile::Ads::Zestadz([$parent]);
31 $ad = new Mobile::Ads::Zestadz
32 ($text,$link,$image) = $ad->get_zestadz_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::Zestadz> provides an object oriented interface to serve advertisements
44 from AdMob in mobile sites.
45 This was ported from VB code in their site
46  
47 =head1 new Mobile::Ads::Zestadz
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<>=> Zestadz site code, delivered by them. Something in the form off ``01234abcdef12345''
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_zestadz_ad;
108  
109 sub get_zestadz_ad {
110 my $self = shift;
111
112 my ($site,$metadata,$request,$remote,$address,$text,$link) = ('','','','','','');
113 if (ref $_[0] eq 'HASH') {
114 $site = $_[0]->{'site'} || $self->{'site'};
115 $metadata = $_[0]->{'metadata'};
116 $request = $_[0]->{'request'};
117 $remote = $_[0]->{'remote'};
118 $address = $_[0]->{'address'};
119 $text = $_[0]->{'text'};
120 $link = $_[0]->{'link'};
121 }
122 else {
123 ($site,$metadata,$request,$remote,$address,$text,$link) = @_;
124 }
125
126 $site ||= $self->{'site'};
127 $remote ||= $ENV{'HTTP_USER_AGENT'};
128 $address ||= $ENV{'REMOTE_ADDR'};
129 $metadata ||= 'http://'.$ENV{'HTTP_HOST'}.$ENV{'REQUEST_URI'};
130 $request ||="impression";
131 $text ||= $self->{'text'};
132 $link ||= $self->{'link'};
133
134 Carp::croak("cant serve ads without site\n") unless ($site);
135 Carp::croak("cant serve ads without remote user agent\n") unless ($remote);
136 Carp::croak("cant serve ads without remote address\n") unless ($address);
137
138 # fetch data
139 my $res;
140 #"?cid="&cid&"&ua="&ua&"&ip="&ip&"&mt="&mt&"&request="&req&""
141 my $params = {
142 cid => $site,
143 ua => $remote,
144 ip => $address,
145 mt => $metadata,
146 request => $request
147 };
148
149 eval q[$res = $self->{'parent'}->get_ad({
150 url => 'http://www.zestadz.com/waphandler',
151 method => 'GET',
152 params => $params
153 });];
154 if ($@) {
155 return ($text,$link);
156 }
157 else {
158 my $ret = $self->parse($res,$text,$link);
159 if (wantarray) {
160 return ($ret->{'text'},$ret->{'link'},$ret->{'image'});
161 }
162 else {
163 return $ret;
164 }
165 }
166 }
167  
168 sub parse {
169 my $self = shift;
170
171 my ($toparse,$text,$link) = @_;
172  
173 my $ret = { };
174 my $type = 0;
175 if ($toparse =~ m|^(.+?)\~(\d+)\~(.+?)~(.+?)~(.+?)$|s) {
176 #an ad with both text and image (and link of course)
177 (
178 $ret->{'link'} = 'http://www.zestadz.com/waplanding?lm='.$1.'~ck',
179 $type = $2,
180 $ret->{'image'} = $3,
181 $ret->{'text'} = $5
182 );
183 }
184 elsif ($toparse =~ m|^(.+?)\~(\d+)\~(.+?)~(.+?)$|s) {
185 #an ad with either only text or only image (and link of course) ($2 will tell us which is which)
186 (
187 $ret->{'link'} = 'http://www.zestadz.com/waplanding?lm='.$1.'~ck',
188 $type = $2,
189 $ret->{'image'} = $3,
190 $ret->{'text'} = $3,
191
192 );
193 }
194
195 if ($type == 4) {
196 $ret->{'link'} = $link;
197 $ret->{'text'} = $text;
198 $ret->{'image'} = '';
199 }
200 elsif ($type == 2) {
201 $ret->{'image'} = '';
202 }
203 elsif ($type == 1) {
204 $ret->{'text'} = '';
205 }
206
207 defined($ret->{'link'}) and $ret->{'link'} = $self->{'parent'}->XMLEncode($ret->{'link'});
208 defined($ret->{'text'}) and $ret->{'text'} = $self->{'parent'}->XMLEncode($ret->{'text'});
209 defined($ret->{'image'}) and $ret->{'image'} = $self->{'parent'}->XMLEncode($ret->{'image'});
210
211 return $ret;
212 }
213  
214 =pod
215  
216 =head2 Methods
217  
218 =over 4
219  
220 =item get_zestadz_ad
221  
222 C<>=> Does the actual fetching of the ad for the site given. Refer to new for details
223 Returns a list ($text_for_ad,$link_for_ad) value in list context or an
224 ``<a href="$link">$text</a>'' if called in scalar context.
225  
226 =back
227  
228 =cut
229  
230  
231 =head1 Revision History
232  
233 0.0.1
234 Initial Release
235 0.0.2
236 Fixed stupid typo
237 0.0.3
238 Didn't preserve default values on failure
239 0.0.4
240 $ua timeout set to 20 sec
241 0.0.5
242 All ua stuff put in Mobile::Ads
243 0.0.6
244 Aliased get_ad to get_zestadz_ad
245 0.0.7
246 Option to reuse parent Mobile::Ads instead of creating anew
247 0.0.8/0.0.9
248 Skipped those to have same verion number in all modules
249 0.1.0
250 One could also use a reference to the parent... :)
251  
252 =head1 BUGS
253  
254 Thoughtlessly crafted to avoid having the same piece of code in several places.
255 Could use lots of enhancements.
256  
257 =head1 DISCLAIMER
258  
259 This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
260 which is available on CPAN.
261  
262 =head1 AUTHOR
263  
264 Thanos Chatziathanassiou <tchatzi@arx.net>
265 http://www.arx.net
266  
267 =head1 COPYRIGHT
268  
269 Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
270  
271 This program is free software; you can redistribute it and/or
272 modify it under the same terms as Perl itself.
273  
274 =cut
275  
276 1;