Ads/MoJiva.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 129 MC/DCmcdc 0.0% 0 / 67 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 320 CC 11 SCAR 35.1
CC
11
Cov
0%
get_mojiva_ad
72.0
new
12.0
parse
6.0
CRAP 33.5
1 # Mobile::Ads::MoJiva.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::MoJiva;
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::MoJiva::VERSION='0.1.0';
16 $Mobile::Ads::MoJiva::ver=$Mobile::Ads::MoJiva::VERSION;
17  
18 use strict 'vars';
19 use Carp();
20 use Mobile::Ads();
21 use Digest::MD5 qw();
22  
23 =head1 NAME
24  
25 Mobile::Ads::MoJiva - module to serve MoJiva ads
26  
27 Version 0.1.0
28  
29 =head1 SYNOPSIS
30  
31 use Mobile::Ads::MoJiva;
32 $ad = new Mobile::Ads::MoJiva
33 ($text,$link,$image) = $ad->get_mojiva_ad({
34 site => 'MoJiva site code',
35 remote => $ENV{'HTTP_USER_AGENT'},
36 address => $ENV{'REMOTE_ADDR'},
37 text => 'default ad text',
38 link => 'default ad link',
39 test => 'set this if this is a test ad',
40 zone => 'ad zone',
41 });
42
43 =head1 DESCRIPTION
44  
45 C<Mobile::Ads::MoJiva> provides an object oriented interface to serve advertisements
46 from AdMob in mobile sites.
47 This is just a slightly altered version of the perl code found on AdMob's site.
48  
49 =head1 new Mobile::Ads::MoJiva
50  
51 =over 4
52  
53 =item [$parent]
54  
55 To reuse Mobile::Ads in multiple (subsequent) ad requests, you can pass a C<Mobile::Ads>
56 reference here. Instead of creating a new Mobile::Ads object, we will use the one you
57 pass instead. This might save a little C<LWP::UserAgent> creation/destruction time.
58  
59 =head2 Parameters/Properties
60  
61 =over 4
62  
63 =item site
64  
65 C<>=> MoJiva site code, delivered by them. Something in the form off ``123''
66  
67 =item remote
68  
69 C<>=> Remote User Agent ($ENV{'HTTP_USER_AGENT'}). In fact $ENV{'HTTP_USER_AGENT'} will be used
70 if not supplied.
71  
72 =item address
73  
74 C<>=> $ENV{'REMOTE_ADDR'}. All things about HTTP_USER_AGENT also apply here.
75  
76 =item text
77  
78 C<>=> Should we fail to retrieve a real ad, this is the text of the ad displayed instead
79  
80 =item link
81  
82 C<>=> Same with text, but for the ad's link.
83  
84 =back
85  
86 =cut
87  
88 sub new {
89 my $this = shift;
90 my $class = ref($this) || $this;
91 my $self = {};
92 bless $self, $class;
93  
94 my $parent = shift;
95
96 if ($parent && ref($parent) && ref($$parent) && ref($$parent) eq "Mobile::Ads") {
97 $self->{'parent'} = $$parent;
98 }
99 elsif ($parent && ref($parent) && ref($parent) eq "Mobile::Ads") {
100 $self->{'parent'} = $parent;
101 }
102 else {
103 $self->{'parent'} = new Mobile::Ads;
104 }
105
106 return $self;
107 }
108  
109 *get_ad = \&get_mojiva_ad;
110  
111 sub get_mojiva_ad {
112 my $self = shift;
113
114 my $mojiva_endpoint = "http://ads.mojiva.com/ad";
115
116 my ($site,$test,$remote,$address,$uri,$markup,$text,$link,$key,$count,$border,$header,$bgcolor,$textcol,$linkcol,$adtype,$zone) =
117 ('','','','','','','','','','','','','','','','','');
118
119 if (ref $_[0] eq 'HASH') {
120 $site = $_[0]->{'site'} || $self->{'site'};
121 $test = $_[0]->{'test'} || '';
122 $remote = $_[0]->{'remote'} || $ENV{'HTTP_USER_AGENT'};
123 $address = $_[0]->{'address'} || $ENV{'REMOTE_ADDR'};
124 $uri = $_[0]->{'uri'} || 'http://'.$ENV{'HTTP_HOST'}.$ENV{'REQUEST_URI'};
125 $text = $_[0]->{'text'} || $self->{'text'} || '';
126 $link = $_[0]->{'link'} || $self->{'link'} || '';
127 #alterations from admob start here..
128 $adtype = $_[0]->{'adtype'} || $self->{'adtype'} || 3;
129 $key = $_[0]->{'key'} || $self->{'key'} || 1;
130 $zone = $_[0]->{'zone'} || $self->{'zone'} || 143;
131 $count = $_[0]->{'count'} || $self->{'count'} || 1;
132 $border = $_[0]->{'border'} || $self->{'border'} || '#000000';
133 $header = $_[0]->{'header'} || $self->{'header'} || '#cccccc';
134 $bgcolor = $_[0]->{'bgcolor'} || $self->{'bgcolor'} || '#eeeeee';
135 $textcol = $_[0]->{'textcol'} || $self->{'textcol'} || '#000000';
136 $linkcol = $_[0]->{'linkcol'} || $self->{'linkcol'} || '#ff0000';
137 }
138 else {
139 ($site,$test,$remote,$address,$uri,$markup,$text,$link,$key,$count,$border,$header,$bgcolor,$textcol,$linkcol,$adtype,$zone) = @_;
140 }
141
142 $site ||= $self->{'site'};
143 $remote ||= $ENV{'HTTP_USER_AGENT'};
144 $address ||= $ENV{'REMOTE_ADDR'};
145 $text ||= $self->{'text'};
146 $link ||= $self->{'link'};
147
148 Carp::croak("cant serve ads without site\n") unless ($site);
149 Carp::croak("cant serve ads without remote user agent\n") unless ($remote);
150 Carp::croak("cant serve ads without remote address\n") unless ($address);
151
152 my $mojiva_post = {
153 'site' => $site,
154 'ua' => $remote,
155 'ip' => $address,
156 'url' => $uri,
157 'zone' => $zone, #is 143 constant ? i'd venture to guess not
158 'adstype' => $adtype, #could also use 1 for text or 2 for image only (3==text+image)
159 'key' => $key,
160 'count' => $count,
161 'paramBORDER' => $border, # ads border color
162 'paramHEADER' => $header, # header color
163 'paramBG' => $bgcolor, # background color
164 'paramTEXT' => $textcol, # text color
165 'paramLINK' => $linkcol # url color
166 };
167
168 #test ads need just find POSTs ``m'' field.
169 if ($test eq 'test') {
170 $mojiva_post->{'test'} = '1';
171 }
172
173 #do the POST
174 my $res;
175 #through $self->{parent} prefrably...
176 eval q[$res = $self->{'parent'}->get_ad({
177 url => $mojiva_endpoint,
178 method => 'POST',
179 params => $mojiva_post
180 });];
181 if ($@) {
182 return ($text,$link);
183 }
184 else {
185 my $ret = $self->parse($res,$text,$link);
186 if (wantarray) {
187 return ($ret->{'text'},$ret->{'link'},$ret->{'image'});
188 }
189 else {
190 return $ret;
191 }
192 }
193 }
194  
195 sub parse {
196 my $self = shift;
197
198 my ($toparse,$text,$link) = @_;
199  
200 my $ret = { };
201
202 $toparse =~ m|\<a.+?href=[\"\']([^\'\"]+)[\'\"]|s and $ret->{'link'} = $1;
203 $toparse =~ m|\<img.+?src=[\"\']([^\'\"]+)[\'\"]|s and $ret->{'image'} = $1;
204 $toparse =~ m|\>([^\<\>]+?)\</a\>|s and $ret->{'text'} = $1;
205
206 #we need at least link and text to exist...
207 if ($ret->{'link'} && ($ret->{'text'} || $ret->{'image'}) ) {
208 $ret->{'image'} ||= '';
209 $ret->{'text'} ||= '';
210 }
211 else {
212 $ret->{'link'} = $link;
213 $ret->{'text'} = $text;
214 $ret->{'image'} = '';
215 }
216
217 defined($ret->{'link'}) and $ret->{'link'} = $self->{'parent'}->XMLEncode($ret->{'link'});
218 defined($ret->{'text'}) and $ret->{'text'} = $self->{'parent'}->XMLEncode($ret->{'text'});
219 defined($ret->{'image'}) and $ret->{'image'} = $self->{'parent'}->XMLEncode($ret->{'image'});
220
221 return $ret;
222 }
223  
224 =pod
225  
226 =head2 Methods
227  
228 =over 4
229  
230 =item get_mojiva_ad
231  
232 C<>=> Does the actual fetching of the ad for the site given. Refer to new for details
233 Returns a list ($text_for_ad,$link_for_ad) value in list context or an
234 ``<a href="$link">$text</a>'' if called in scalar context.
235  
236 =back
237  
238 =cut
239  
240  
241 =head1 Revision History
242  
243 0.0.1
244 Initial Release
245 0.0.2
246 Fixed stupid typo
247 0.0.3
248 Didn't preserve default values on failure
249 0.0.4
250 $ua timeout set to 20 sec
251 0.0.5
252 $ua timeout set to 2 sec
253 Implemented the new version AdMob code
254 (still some funky parts in there, but seems to work)
255 0.0.6
256 Aliased get_ad to get_v2_ad
257 0.0.7
258 Option to reuse parent Mobile::Ads instead of creating anew
259 0.0.8/0.0.9
260 Skipped those to have same verion number in all modules
261 0.1.0
262 One could also use a reference to the parent... :)
263  
264 =head1 BUGS
265  
266 Thoughtlessly crafted to avoid having the same piece of code in several places.
267 Could use lots of enhancements.
268  
269 =head1 DISCLAIMER
270  
271 This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
272 which is available on CPAN.
273  
274 =head1 AUTHOR
275  
276 Thanos Chatziathanassiou <tchatzi@arx.net>
277 http://www.arx.net
278  
279 =head1 COPYRIGHT
280  
281 Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
282  
283 This program is free software; you can redistribute it and/or
284 modify it under the same terms as Perl itself.
285  
286 =cut
287  
288 1;