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