Ads/GetMobile.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 195 CC 14 SCAR 34.8
CC
14
Cov
0%
get_getmobile_ad
72.0
parse
20.0
new
12.0
CRAP 32.5
1 # Mobile::Ads::GetMobile.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::GetMobile;
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::GetMobile::VERSION='0.1.0';
16 $Mobile::Ads::GetMobile::ver=$Mobile::Ads::GetMobile::VERSION;
17  
18 use strict 'vars';
19 use Carp();
20 use Mobile::Ads();
21  
22 =head1 NAME
23  
24 Mobile::Ads::GetMobile - module to serve ads.gr ads
25  
26 Version 0.1.0
27  
28 =head1 SYNOPSIS
29  
30 use Mobile::Ads::GetMobile;
31 $ad = new Mobile::Ads::GetMobile
32 ($text,$link,$image) = $ad->get_getmobile_ad({
33 site => 'GetMobile site code',
34 remote => $ENV{'HTTP_USER_AGENT'},
35 address => $ENV{'REMOTE_ADDR'},
36 text => 'default ad text',
37 link => 'default ad link',
38 });
39
40 =head1 DESCRIPTION
41  
42 C<Mobile::Ads::GetMobile> provides an object oriented interface to serve advertisements
43 from Ads.gr in mobile sites.
44 ads.gr was written by me, so it is quite safe to assume it works, despite sloppy writing
45 on this module (all the Mobile::Ads family to be exact)
46  
47 =head1 new Mobile::Ads::GetMobile
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<>=> GetMobile site code, delivered by them. Something in the form off ``site_name'' (ie ``wapamama'')
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_getmobile_ad;
108  
109 sub get_getmobile_ad {
110 my $self = shift;
111
112 my ($site,$sid,$test,$remote,$address,$text,$link) = ('','',0,'','','');
113 if (ref $_[0] eq 'HASH') {
114 $site = $_[0]->{'site'} || $self->{'site'};
115 $sid = $_[0]->{'sid'};
116 $test = $_[0]->{'test'};
117 $remote = $_[0]->{'remote'};
118 $address = $_[0]->{'address'};
119 $text = $_[0]->{'text'};
120 $link = $_[0]->{'link'};
121 }
122 else {
123 ($site,$sid,$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 if ($test) {
133 $test = 1;
134 }
135 else {
136 $test = 0;
137 }
138
139 Carp::croak("cant serve ads without site\n") unless ($site);
140 Carp::croak("cant serve ads without remote user agent\n") unless ($remote);
141 Carp::croak("cant serve ads without remote address\n") unless ($address);
142
143 # fetch data
144 my $res;
145 my $params = {
146 pid => $site,
147 sid => $sid,
148 code => "Mobile::Ads::GetMobile/".$Mobile::Ads::GetMobile::VERSION,
149 test => $test,
150 ua => $remote,
151 uip => $address
152 };
153
154 my $headers = $self->get_headers();
155
156 eval q[$res = $self->{'parent'}->get_ad({
157 url => 'http://ad.qwapi.com/adserver/render',
158 method => 'GET',
159 params => $params,
160 headers => $headers
161 });];
162 if ($@) {
163 return ($text,$link);
164 }
165 else {
166 my $ret = $self->parse($res,$text,$link);
167 if (wantarray) {
168 return ($ret->{'text'},$ret->{'link'},$ret->{'image'});
169 }
170 else {
171 return $ret;
172 }
173 }
174 }
175  
176 sub parse {
177 my $self = shift;
178
179 my ($toparse,$text,$link) = @_;
180 my $ret = { };
181
182 if ($toparse =~ m|\<a.+?href="([^\"]+)\".+?\<img.+?src="([^\"]+).+?alt="([^\"]+)"|s) {
183 #an ad with both text and image (and link of course)
184 (
185 $ret->{'link'} = $1,
186 $ret->{'text'} = $3,
187 $ret->{'image'} = $2
188 );
189 }
190 elsif ($toparse =~ m|\<a.+?href="([^\"]+)\".+?\<img.+?src="([^\"]+).+\>.*?([^\<]+)\</a\>|s) {
191 #an ad with both text and image (and link of course)
192 (
193 $ret->{'link'} = $1,
194 $ret->{'text'} = $3,
195 $ret->{'image'} = $2
196 );
197 }
198 elsif ($toparse =~ m|\<a.+?href="([^\"]+)\".+?\>(.+?)\</a\>|s) {
199 #an ad with only text and link
200 (
201 $ret->{'link'} = $1,
202 $ret->{'text'} = $2
203 );
204 }
205
206 defined($ret->{'link'}) and $ret->{'link'} = $self->{'parent'}->XMLEncode($ret->{'link'});
207 defined($ret->{'text'}) and $ret->{'text'} = $self->{'parent'}->XMLEncode($ret->{'text'});
208 defined($ret->{'image'}) and $ret->{'image'} = $self->{'parent'}->XMLEncode($ret->{'image'});
209
210 return $ret;
211 }
212  
213 sub get_headers {
214 my $self = shift;
215
216 my $ret = { };
217
218 foreach (keys(%ENV)) {
219 if ($_ =~ m|^HTTP_|) {
220 my ($key,$val) = (lc($_),$ENV{$_});
221 $key =~ s|_(\w)|\-\u$1|g;
222 $key =~ s|^http\-|ch_|;
223
224 $ret->{$key} = $val;
225 }
226 }
227
228 return $ret;
229 }
230  
231 =pod
232  
233 =head2 Methods
234  
235 =over 4
236  
237 =item get_getmobile_ad
238  
239 C<>=> Does the actual fetching of the ad for the site given. Refer to new for details
240 Returns a list ($text_for_ad,$link_for_ad,$ad_image) value.
241  
242 =back
243  
244 =cut
245  
246  
247 =head1 Revision History
248  
249 0.0.1
250 Initial Release
251 0.0.2
252 Fixed stupid typo
253 0.0.3
254 Didn't preserve default values on failure
255 0.0.4
256 $ua timeout set to 20 sec
257 0.0.5
258 All ua stuff put in Mobile::Ads
259 0.0.6
260 Aliased get_ad to get_GetMobile_ad
261 0.0.7
262 Option to reuse parent Mobile::Ads instead of creating anew
263 0.0.8/0.0.9
264 Skipped those to have same verion number in all modules
265 0.1.0
266 One could also use a reference to the parent... :)
267  
268 =head1 BUGS
269  
270 Thoughtlessly crafted to avoid having the same piece of code in several places.
271 Could use lots of enhancements.
272  
273 =head1 DISCLAIMER
274  
275 This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00
276 which is available on CPAN.
277  
278 =head1 AUTHOR
279  
280 Thanos Chatziathanassiou <tchatzi@arx.net>
281 http://www.arx.net
282  
283 =head1 COPYRIGHT
284  
285 Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved.
286  
287 This program is free software; you can redistribute it and/or
288 modify it under the same terms as Perl itself.
289  
290 =cut
291  
292 1;