Ads/AdultModa.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 93 MC/DCmcdc 0.0% 0 / 49 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 246 CC 9 SCAR 32.6
CC
9
Cov
0%
get_adultmoda_ad
56.0
new
12.0
get_image_ad
2.0
CRAP 26.0
1 # Mobile::Ads::AdultModa.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::AdultModa;
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::AdultModa::VERSION='0.1.0';
16 $Mobile::Ads::AdultModa::ver=$Mobile::Ads::AdultModa::VERSION;
17  
18 use strict 'vars';
19 use Carp();
20 use Mobile::Ads();
21  
22 =head1 NAME
23  
24 Mobile::Ads::AdultModa - module to serve AdMob ads
25  
26 Version 0.1.0
27  
28 =head1 SYNOPSIS
29  
30 use Mobile::Ads::AdultModa;
31 $ad = new Mobile::Ads::AdultModa
32 ($text,$link) = $ad->get_text_ad({
33 site => 'AdultModa 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::AdultModa> provides an object oriented interface to serve advertisements
44 from AdultModa in mobile sites.
45 This is just a slightly altered version of the perl code found on AdultModa's site.
46  
47 =head1 new Mobile::Ads::AdultModa
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<>=> AdultModa site code, delivered by them. Something in the form off ``a1471c9db1c2d27''
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_image_ad;
108  
109 sub get_adultmoda_ad {
110 my $self = shift;
111
112 my $url = shift;
113
114 my ($site,$remote,$address,$text,$link,$image) = ('','','','','','','');
115 if (ref $_[0] eq 'HASH') {
116 $site = $_[0]->{'site'} || $self->{'site'};
117 $remote = $_[0]->{'remote'};
118 $address = $_[0]->{'address'};
119 $text = $_[0]->{'text'};
120 $link = $_[0]->{'link'};
121 }
122 else {
123 ($site,$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 z => $site,
140 ua => $remote,
141 a => $address,
142 };
143
144 eval q[$res = $self->{'parent'}->get_ad({
145 url => $url,
146 method => 'GET',
147 params => $params
148 });];
149 if ($@) {
150 $link and $link = $self->{'parent'}->XMLEncode($link);
151 $text and $text = $self->{'parent'}->XMLEncode($text);
152 return ($text,$link);
153 }
154 else {
155 chomp($res);
156 my ( $bannerid, $image_url, $click_url, $texte ) = split( /\|/, $res );
157
158 if ( $bannerid ) {
159 $image_url ||= '';
160 $text ||= 'Click here...';
161 ($link,$text,$image) = ($click_url,$texte,$image_url);
162 }
163 $link and $link = $self->{'parent'}->XMLEncode($link);
164 $text and $text = $self->{'parent'}->XMLEncode($text);
165 $image and $text = $self->{'parent'}->XMLEncode($image);
166 return ($text,$link,$image);
167 }
168 }
169  
170 sub get_image_ad {
171 my $self = shift;
172
173 my $url = 'http://www.adultmoda.com/ads/fetch.php';
174
175 my ($text,$link,$image) = $self->get_adultmoda_ad($url,@_);
176
177 ($text and $link and $image and return ($text,$link,$image)) or
178 ($text and $link and return ($text,$link)) or
179 Carp::croak("Cant get ad: no text and link\n");
180 }
181  
182 sub get_text_ad {
183 my $self = shift;
184
185 my $url = 'http://www.adultmoda.com/ads/textfetch.php';
186
187 my ($text,$link,$image) = $self->get_adultmoda_ad($url,@_);
188
189 ($text and $link and $image and return ($text,$link,$image)) or
190 ($text and $link and return ($text,$link)) or
191 Carp::croak("Cant get ad: no text and link\n");
192 }
193  
194 =pod
195  
196 =head2 Methods
197  
198 =over 4
199  
200 =item get_text_ad
201  
202 C<>=> Does the actual fetching of the ad for the site given. Refer to new for details
203 Returns a list ($text_for_ad,$link_for_ad) value.
204  
205 =item get_image_ad
206  
207 C<>=> Does the actual fetching of the ad for the site given. Refer to new for details
208 Returns a list ($text_for_ad,$link_for_ad,$image_url) value.
209  
210 =back
211  
212 =cut
213  
214  
215 =head1 Revision History
216  
217 0.0.1
218 Initial Release
219 0.0.2
220 Fixed stupid typo
221 0.0.3
222 Didn't preserve default values on failure
223 0.0.4
224 $ua timeout set to 20 sec
225 0.0.5
226 All ua stuff put in Mobile::Ads
227 0.0.6
228 Aliased get_ad to get_(adultmoda)image_ad
229 0.0.7
230 Option to reuse parent Mobile::Ads instead of creating anew
231 0.0.8/0.0.9
232 Skipped those to have same verion number in all modules
233 0.1.0
234 One could also use a reference to the parent... :)
235  
236 =head1 BUGS
237  
238 Thoughtlessly crafted to avoid having the same piece of code in several places.
239 Could use lots of enhancements.
240  
241 =head1 DISCLAIMER
242  
243 This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
244 which is available on CPAN.
245  
246 =head1 AUTHOR
247  
248 Thanos Chatziathanassiou <tchatzi@arx.net>
249 http://www.arx.net
250  
251 =head1 COPYRIGHT
252  
253 Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
254  
255 This program is free software; you can redistribute it and/or
256 modify it under the same terms as Perl itself.
257  
258 =cut
259  
260 1;