Module: Rmobio::Ads::AdMobs

Defined in:
lib/rmobio/ads/ad_mobs.rb

Instance Method Summary collapse

Instance Method Details

#get_ad(keywords, ad_client, request, kw_type = "broad") ⇒ Object



29
30
31
# File 'lib/rmobio/ads/ad_mobs.rb', line 29

def get_ad(keywords,ad_client,request,kw_type="broad")
  getAd(keywords,ad_client,ip=request.remote_ip,request.request_uri,request.user_agent,userid=request.env["HTTP_USERID"],kw_type)
end

#getAd(keywords, adClient, ip = request.remote_ip, url = request.request_uri, useragent = request.user_agent, userid = request.env["HTTP_USERID"], kw_type = "broad") ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rmobio/ads/ad_mobs.rb', line 33

def getAd(keywords,adClient,ip=request.remote_ip,url=request.request_uri,useragent=request.user_agent,userid=request.env["HTTP_USERID"],kw_type="broad")
  logger.info("Retrieving AdMobs ad")
  admob_params = {:admob_site_id => adClient, # REQUIRED - get from admob.com, value is generally set in rmobio.yml
    :admob_pc => "",       # OPTIONAL - Postal Code, e.g. "90210"
    :admob_dob => "",      # OPTIONAL - Date of Birth formatted like YYYYMMDD, e.g. "19800229"
    :admob_gender => "",   # OPTIONAL - Gender, m[ale] or f[emale]
    :admob_keywords => keywords} 
  # change to "live" when ready to deploy
  admob_mode = "test"
  
  admob_endpoint = "http://r.admob.com/ad_source.php"
  admob_version  = "20071126-RUBY-19db4ba7ab087721"
  admob_timeout  = 1.0
  admob_ignore   = Set["HTTP_PRAGMA", "HTTP_CACHE_CONTROL", "HTTP_CONNECTION", "HTTP_USER_AGENT", "HTTP_COOKIE"]
  # build url
  admob_post = {} 
  admob_post["s"] = admob_params[:admob_site_id]
  admob_post["u"] = useragent if useragent
  admob_post["i"] = ip if ip
  admob_post["p"] = url if url
  admob_post["t"] = MD5.hexdigest(userid)  # TODO: is this how to set userid?!
  admob_post["v"] = admob_version
  admob_post["d[pc]"] = admob_params[:admob_pc]
  admob_post["d[dob]"] = admob_params[:admob_dob]
  admob_post["d[gender]"] = admob_params[:admob_gender]
  admob_post["k"] = admob_params[:admob_keywords]      
  request.env.each {|k,v| admob_post["h[#{k}]"] = v unless admob_ignore.include?(k.upcase)}
  admob_post["m"] = "test" if admob_mode == "test"     
  begin # request ad
    admob_uri = URI.parse(admob_endpoint)
    admob_request = Net::HTTP::Post.new(admob_uri.path)
    admob_request.set_form_data(admob_post)
    admob_conn = Net::HTTP.new(admob_uri.host, admob_uri.port)
    admob_conn.read_timeout = admob_timeout
    admob_conn.open_timeout = admob_timeout
    admob_response = admob_conn.start {|admob_http| admob_http.request(admob_request) }
    admob_contents = admob_response.body     
  rescue Timeout::Error => te
    admob_contents = "<img src=\"http://t.admob.com/li.php/c.gif/#{admob_params[:admob_site_id]}/1/#{admob_timeout}/#{MD5.hexdigest(request.request_uri)}\" alt=\"\" width=\"1\" height=\"1\" />"
  rescue
  end
  @ad=admob_contents
end

#getAdByPage(adClient, ip = request.remote_ip, url = request.request_uri, useragent = request.user_agent, userid = , kw_type = "broad") ⇒ Object

the keyword search version also evaluates the supplied page so for admobs they are the same



79
80
81
# File 'lib/rmobio/ads/ad_mobs.rb', line 79

def getAdByPage(adClient,ip=request.remote_ip,url=request.request_uri,useragent=request.user_agent,userid=request.env["HTTP_USERID"],kw_type="broad")
  getAd("",adClient)
end