Class: RailsAdserver::Advertisement

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/rails_adserver/advertisement.rb

Class Method Summary collapse

Class Method Details

.ad(adspace_id, ip) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/rails_adserver/advertisement.rb', line 52

def self.ad(adspace_id,ip)
  space = RailsAdserver::Adspace.find(adspace_id)
  geo_ip = Geokit::Geocoders::MultiGeocoder.geocode(ip)
  geo =  Geokit::Geocoders::MultiGeocoder.geocode("#{geo_ip.city},#{geo_ip.state},#{geo_ip.country}")
  id =  space.advertisements.geo_city(geo.city)
  if id == nil
    id = space.advertisements.geo_state(geo.state)
    if id == nil
      id = space.advertisements.geo_country(geo.country)
      if id == nil
        id = space.advertisements.random_ad(nil)
        if id == nil
          id = space.advertisements.backup_ad
        end
      end
    end
  end
  unless id == nil
    advertisement = RailsAdserver::Advertisement.find(id)
    if advertisement.impressions_count == nil
      advertisement.update_attribute(:impressions_count,0)
    end
    count = advertisement.impressions_count+1
    advertisement.update_attribute(:impressions_count,count)
    unless advertisement.max_impressions == 0 || advertisement.max_impressions == nil
      if advertisement.max_impressions <= advertisement.impressions_count
        advertisement.update_attribute(:is_active,false)
      end
    end
  end
  return id
end

.ad_with_parameter(adspace_id, param, ip) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/rails_adserver/advertisement.rb', line 16

def self.ad_with_parameter(adspace_id,param,ip)
  space = RailsAdserver::Adspace.find(adspace_id)
  id = space.advertisements.random_ad(param)
  if id == nil
    geo_ip = Geokit::Geocoders::MultiGeocoder.geocode(ip)
    geo =  Geokit::Geocoders::MultiGeocoder.geocode("#{geo_ip.city},#{geo_ip.state},#{geo_ip.country}")
    id =  space.advertisements.geo_city(geo.city)
    if id == nil
      id = space.advertisements.geo_state(geo.state)
      if id == nil
        id = space.advertisements.geo_country(geo.country)
        if id == nil
          id = space.advertisements.random_ad(nil)
          if id == nil
            id = space.advertisements.backup_ad
          end
        end
      end
    end
  end
  unless id == nil
    advertisement = RailsAdserver::Advertisement.find(id)
    if advertisement.impressions_count == nil
      advertisement.update_attribute(:impressions_count,0)
    end
    count = advertisement.impressions_count+1
    advertisement.update_attribute(:impressions_count,count)
    unless advertisement.max_impressions == 0 || advertisement.max_impressions == nil
      if advertisement.max_impressions <= advertisement.impressions_count
        advertisement.update_attribute(:is_active,false)
      end
    end
  end
  return id
end

.backup_adObject



105
106
107
108
# File 'app/models/rails_adserver/advertisement.rb', line 105

def self.backup_ad
  ad_ids = self.where("backup = ?", true).map(&:id)
  id = ad_ids[rand(ad_ids.length)]
end

.geo_city(city) ⇒ Object



93
94
95
96
# File 'app/models/rails_adserver/advertisement.rb', line 93

def self.geo_city(city)
  ad_ids = self.where("city_name = ? AND is_active = ?  AND geolocation_boolean = ?", city, true,true).map(&:id)
  id = ad_ids[rand(ad_ids.length)]
end

.geo_country(country) ⇒ Object



101
102
103
104
# File 'app/models/rails_adserver/advertisement.rb', line 101

def self.geo_country(country)
  ad_ids = self.where("country_name = ? AND state_name = ? AND city_name = ? AND is_active = ? AND geolocation_boolean = ?", country, nil, nil, true, true).map(&:id)
  id = ad_ids[rand(ad_ids.length)]
end

.geo_state(state) ⇒ Object



97
98
99
100
# File 'app/models/rails_adserver/advertisement.rb', line 97

def self.geo_state(state)
  ad_ids = self.where("state_name = ? AND city_name = ? AND is_active = ? AND geolocation_boolean = ?", state, nil, true, true).map(&:id)
  id = ad_ids[rand(ad_ids.length)]
end

.random_ad(param) ⇒ Object



85
86
87
88
89
90
91
92
# File 'app/models/rails_adserver/advertisement.rb', line 85

def self.random_ad(param)
  if param == nil
    ad_ids = self.where("is_active = ? AND parameter_restriction_boolean = ? AND geolocation_boolean = ? AND backup = ?", true, false, false, false).map(&:id)
  else
    ad_ids = self.where("is_active = ? AND param_restriction = ? AND backup = ?",true, param, false).map(&:id)
  end
  id = ad_ids[rand(ad_ids.length)]
end