| 1 | # Mobile::Ads::Adsgr.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::Adsgr; | ||
| 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::Adsgr::VERSION='0.1.0'; | ||
| 16 | $Mobile::Ads::Adsgr::ver=$Mobile::Ads::Adsgr::VERSION; | ||
| 17 | |||
| 18 | use strict 'vars'; | ||
| 19 | use Carp(); | ||
| 20 | use Mobile::Ads(); | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Mobile::Ads::Adsgr - module to serve ads.gr ads | ||
| 25 | |||
| 26 | Version 0.1.0 | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Mobile::Ads::Adsgr; | ||
| 31 | $ad = new Mobile::Ads::Adsgr | ||
| 32 | ($text,$link,$image) = $ad->get_adsgr_ad({ | ||
| 33 | site => 'Ads.gr 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::Adsgr> 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::Adsgr | ||
| 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<>=> Ads.gr 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_adsgr_ad; | ||
| 108 | |||
| 109 | sub get_adsgr_ad { | ||
| 110 | my $self = shift; | ||
| 111 | |||
| 112 | my ($site,$remote,$address,$text,$link) = ('','','','','',''); | ||
| 113 | if (ref $_[0] eq 'HASH') { | ||
| 114 | $site = $_[0]->{'site'} || $self->{'site'}; | ||
| 115 | $remote = $_[0]->{'remote'}; | ||
| 116 | $address = $_[0]->{'address'}; | ||
| 117 | $text = $_[0]->{'text'}; | ||
| 118 | $link = $_[0]->{'link'}; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | ($site,$remote,$address,$text,$link) = @_; | ||
| 122 | } | ||
| 123 | |||
| 124 | $site ||= $self->{'site'}; | ||
| 125 | $remote ||= $ENV{'HTTP_USER_AGENT'}; | ||
| 126 | $address ||= $ENV{'REMOTE_ADDR'}; | ||
| 127 | $text ||= $self->{'text'}; | ||
| 128 | $link ||= $self->{'link'}; | ||
| 129 | |||
| 130 | Carp::croak("cant serve ads without site\n") unless ($site); | ||
| 131 | Carp::croak("cant serve ads without remote user agent\n") unless ($remote); | ||
| 132 | Carp::croak("cant serve ads without remote address\n") unless ($address); | ||
| 133 | |||
| 134 | # fetch data | ||
| 135 | my $res; | ||
| 136 | my $params = { | ||
| 137 | site => $site, | ||
| 138 | HTTP_USER_AGENT => $remote, | ||
| 139 | REMOTE_ADDR => $address | ||
| 140 | }; | ||
| 141 | |||
| 142 | eval q[$res = $self->{'parent'}->get_ad({ | ||
| 143 | url => 'http://serve.ads.gr/mobi/mobi.asp', | ||
| 144 | method => 'GET', | ||
| 145 | params => $params | ||
| 146 | });]; | ||
| 147 | if ($@) { | ||
| 148 | return ($text,$link); | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | my $ret = $self->parse($res,$text,$link); | ||
| 152 | if (wantarray) { | ||
| 153 | return ($ret->{'text'},$ret->{'link'},$ret->{'image'}); | ||
| 154 | } | ||
| 155 | else { | ||
| 156 | return $ret; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub parse { | ||
| 162 | my $self = shift; | ||
| 163 | |||
| 164 | my ($toparse,$text,$link) = @_; | ||
| 165 | my $ret = { }; | ||
| 166 | |||
| 167 | if ($toparse =~ m|^(.+?)\*\*\*(.+?)\*\*\*(.+?)$|s) { | ||
| 168 | #an ad with both text and image (and link of course) | ||
| 169 | ( | ||
| 170 | $ret->{'link'} = $1, | ||
| 171 | $ret->{'text'} = $2, | ||
| 172 | $ret->{'image'} = $3 | ||
| 173 | ); | ||
| 174 | } | ||
| 175 | elsif ($toparse =~ m|^(.+?)\*\*\*(.+?)$|s) { | ||
| 176 | #an ad with only text and link | ||
| 177 | ( | ||
| 178 | $ret->{'link'} = $1, | ||
| 179 | $ret->{'text'} = $2 | ||
| 180 | ); | ||
| 181 | } | ||
| 182 | |||
| 183 | defined($ret->{'link'}) and $ret->{'link'} = $self->{'parent'}->XMLEncode($ret->{'link'}); | ||
| 184 | defined($ret->{'text'}) and $ret->{'text'} = $self->{'parent'}->XMLEncode($ret->{'text'}); | ||
| 185 | defined($ret->{'image'}) and $ret->{'image'} = $self->{'parent'}->XMLEncode($ret->{'image'}); | ||
| 186 | |||
| 187 | return $ret; | ||
| 188 | } | ||
| 189 | |||
| 190 | =pod | ||
| 191 | |||
| 192 | =head2 Methods | ||
| 193 | |||
| 194 | =over 4 | ||
| 195 | |||
| 196 | =item get_adsgr_ad | ||
| 197 | |||
| 198 | C<>=> Does the actual fetching of the ad for the site given. Refer to new for details | ||
| 199 | Returns a list ($text_for_ad,$link_for_ad,$ad_image) value. | ||
| 200 | |||
| 201 | =back | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | |||
| 206 | =head1 Revision History | ||
| 207 | |||
| 208 | 0.0.1 | ||
| 209 | Initial Release | ||
| 210 | 0.0.2 | ||
| 211 | Fixed stupid typo | ||
| 212 | 0.0.3 | ||
| 213 | Didn't preserve default values on failure | ||
| 214 | 0.0.4 | ||
| 215 | $ua timeout set to 20 sec | ||
| 216 | 0.0.5 | ||
| 217 | All ua stuff put in Mobile::Ads | ||
| 218 | 0.0.6 | ||
| 219 | Aliased get_ad to get_adsgr_ad | ||
| 220 | 0.0.7 | ||
| 221 | Option to reuse parent Mobile::Ads instead of creating anew | ||
| 222 | 0.0.8/0.0.9 | ||
| 223 | Skipped those to have same verion number in all modules | ||
| 224 | 0.1.0 | ||
| 225 | One could also use a reference to the parent... :) | ||
| 226 | |||
| 227 | =head1 BUGS | ||
| 228 | |||
| 229 | Thoughtlessly crafted to avoid having the same piece of code in several places. | ||
| 230 | Could use lots of enhancements. | ||
| 231 | |||
| 232 | =head1 DISCLAIMER | ||
| 233 | |||
| 234 | This module borrowed its OO interface from Mail::Sender.pm Version : 0.8.00 | ||
| 235 | which is available on CPAN. | ||
| 236 | |||
| 237 | =head1 AUTHOR | ||
| 238 | |||
| 239 | Thanos Chatziathanassiou <tchatzi@arx.net> | ||
| 240 | http://www.arx.net | ||
| 241 | |||
| 242 | =head1 COPYRIGHT | ||
| 243 | |||
| 244 | Copyright (c) 2008 arx.net - Thanos Chatziathanassiou . All rights reserved. | ||
| 245 | |||
| 246 | This program is free software; you can redistribute it and/or | ||
| 247 | modify it under the same terms as Perl itself. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | 1; |