admob19 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/History.txt +13 -0
  2. data/Manifest.txt +5 -0
  3. data/README.txt +108 -0
  4. data/Rakefile +14 -0
  5. data/lib/admob.rb +232 -0
  6. metadata +85 -0
data/History.txt ADDED
@@ -0,0 +1,13 @@
1
+ === 1.1.5 / 2010-12-28
2
+ * Added support for Rails 3 - access session_id using hash & Ruby 1.9 - Digest::MD5 rather than just MD5
3
+
4
+ === 1.1.1 / 2009-01-06
5
+ * Added ability to override default cookie domain in AdMob::set_cookie()
6
+ * Added ability to specify cookie path in AdMob::set_cookie()
7
+
8
+ === 1.1.0 / 2008-10-05
9
+ * Added support for AdMob cookies (see README.TXT)
10
+
11
+ === 1.0.0 / 2008-05-08
12
+
13
+ * AdMob gem created
data/Manifest.txt ADDED
@@ -0,0 +1,5 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/admob.rb
data/README.txt ADDED
@@ -0,0 +1,108 @@
1
+ = AdMob Ruby on Rails integration code (for Rails 3 & Ruby 1.9)
2
+
3
+ * http://www.admob.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ Enables easy integration of AdMob services (ads and/or analytics) into a RoR mobile site.
8
+
9
+ == SYNOPSIS:
10
+
11
+ === Setting up
12
+
13
+ * Get a publisher_id and/or analytics_id from http://www.admob.com and/or http://analytics.admob.com
14
+ * Add <tt>require "admob"</tt> to app/controllers/application.rb, or to any specific controller as needed.
15
+ * Provide some or all default settings in your environment.rb . e.g. :
16
+ require 'admob'
17
+ AdMob::config do |c|
18
+ c.publisher_id = 'YOUR_PUBLISHER_ID'
19
+ c.analytics_id = 'YOUR_ANALYTICS_ID'
20
+ c.timeout = 1
21
+ c.encoding = 'UTF-8'
22
+ c.raise_exceptions = false
23
+ c.cookie_domain = 'example.com' # you can override this setting in AdMob::set_cookie()
24
+ c.cookie_path = '/' # you can override this setting in AdMob::set_cookie()
25
+ end
26
+ * In a view, where you want an ad, place an ad request.
27
+ * Note: You must provide your publisher_id and/or analytics_id in the default setting, or when you make the ad request (see below).
28
+
29
+ === Using Cookies
30
+
31
+ * AdMob now uses cookies to enhance ad targeting and analytics data.
32
+ * The easiest way to enable AdMob cookies on your website is to add the following code snippet to your ActionController::Base class (usually in app/controllers/application.rb)
33
+ * This will cause AdMob::set_cookie() to be executed on every request (the function will return immediately if a cookie does not need to be set)
34
+
35
+ require 'admob'
36
+ class ApplicationController < ActionController::Base
37
+
38
+ ## begin AdMob cookie snippet ##
39
+ before_filter :admob_set_cookie
40
+
41
+ def admob_set_cookie
42
+ AdMob::set_cookie(request, cookies)
43
+ end
44
+ ## end AdMob cookie snippet ##
45
+
46
+ end
47
+
48
+ === Configuring Cookies (IMPORTANT if your site uses multiple subdomains)
49
+
50
+ * By default, the domain of the cookie is for the host that set it. This means that cookies set from a.example.com are not visible from b.example.com
51
+ * To allow AdMob cookies to be visible everywhere on your site, you need to set a default cookie domain in environment.rb
52
+ * The default cookie domain should be the root domain of your mobile site (e.g. "example.com", "example.co.uk")
53
+
54
+ require 'admob'
55
+ AdMob::config do |c|
56
+ c.cookie_domain = 'example.com' # if your main domain is www.example.com
57
+ c.cookie_domain = 'example.co.uk' # if your main domain is www.example.co.uk
58
+ end
59
+
60
+ === Sample requests
61
+
62
+ ==== If you've configured a default publisher and/or analytics id
63
+
64
+ <%= AdMob::request(request, session[:session_id]) %>
65
+
66
+ ==== If you didn't configure a default publisher_id or want to override the configured default publisher_id
67
+
68
+ <%= AdMob::request(request, session[:session_id], :publisher_id => "YOUR_PUBLISHER_ID") %>
69
+
70
+ ==== Request a test ad (happens by default when in test or development mode)
71
+
72
+ <%= AdMob::request(request, session[:session_id], :test => true) %>
73
+
74
+ ==== Request an ad, providing demographic info and keywords
75
+
76
+ <%= AdMob::request(request, session[:session_id], :publisher_id => "YOUR_PUBLISHER_ID", :dob => "19800229", :gender => "male", :postal_code => "90210", :keywords => 'ruby gem admob') %>
77
+
78
+ ==== Make an analytics request only
79
+
80
+ <%= AdMob::request(request, session[:session_id], :ad_request => false) %>
81
+
82
+ See the documentation for #AdMob::request for a list of all params that can be set for a request.
83
+
84
+ == REQUIREMENTS:
85
+
86
+ * An AdMob publisher_id and/or analytics_id (get them at http://www.admob.com and http://analytics.admob.com)
87
+
88
+ == INSTALL:
89
+
90
+ <tt>sudo gem install admob19</tt>
91
+
92
+ == HELP:
93
+
94
+ Additional information can be found at http://developer.admob.com
95
+
96
+ == LICENSE:
97
+
98
+ (The BSD License)
99
+
100
+ Copyright (c) 2008, AdMob, Inc.
101
+ All rights reserved.
102
+
103
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
104
+
105
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
106
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
107
+
108
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '= 1.12.2' # need old version as 2.x has different format
3
+ require 'hoe'
4
+ require './lib/admob.rb'
5
+
6
+ Hoe.new('admob19', AdMob::GEM_VERSION) do |p|
7
+ p.rubyforge_name = 'admob19'
8
+ p.summary = 'Enables easy integration of AdMob ads into a RoR mobile site.'
9
+ p.author = 'AdMob'
10
+ p.email = 'support@admob.com'
11
+ p.url = 'http://www.admob.com/'
12
+ p.description = 'Enables easy integration of AdMob ads into a RoR mobile site.'
13
+ p.developer('AdMob', 'support@admob.com')
14
+ end
data/lib/admob.rb ADDED
@@ -0,0 +1,232 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'digest/md5'
4
+
5
+ # Set up include path correctly
6
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
7
+
8
+ # This module encapsulates functionality (ad requests, analytics requests) provided by AdMob. See README.txt for usage.
9
+ module AdMob
10
+
11
+ GEM_VERSION = '1.1.5'
12
+
13
+ ENDPOINT = URI.parse('http://r.admob.com/ad_source.php')
14
+ PUBCODE_VERSION = '20090714-RUBY-8708a7ab5f2b70b6'
15
+ DEFAULT_TIMEOUT = 1.0
16
+
17
+ # Make an AdMob ad/analytics request. The first param is the request variable from Rails; the second is a unique session
18
+ # identifier. In general, requests should always be of the form <tt><%= AdMob::request(request, session.session_id, ...) %></tt>.
19
+ # Regardless of how many times AdMob::request is called, only one analytics call will be made per page load.
20
+ # The remaining params set optional features of the request. Params that can be set are:
21
+ #
22
+ # [<tt>:publisher_id</tt>] your admob publisher_id, a default can be set using <tt>AdMob::config {|c| c.publisher_id = "YOUR_PUBLISHER_ID"}</tt>
23
+ # [<tt>:analytics_id</tt>] your admob analytics_id, a default can be set using <tt>AdMob::config {|c| c.analytics_id = "YOUR_ANALYTICS_ID"}</tt>
24
+ # [<tt>:ad_request</tt>] whether to make an ad request, defaults to true
25
+ # [<tt>:analytics_request</tt>] whether to make an analytics request, defaults to true
26
+ # [<tt>:encoding</tt>] char encoding of the response, either "UTF-8" or "SJIS", defaults to UTF-8
27
+ # [<tt>:markup</tt>] your site's markup, e.g. "xhtml", "wml", "chtml"
28
+ # [<tt>:postal_code</tt>] postal code of the current user, e.g. "94401"
29
+ # [<tt>:area_code</tt>] area code of the current user, e.g. "415"
30
+ # [<tt>:coordinates</tt>] lat/long of the current user, comma separated, e.g. "37.563657,-122.324807"
31
+ # [<tt>:dob</tt>] date of birth of the current user, e.g. "19800229"
32
+ # [<tt>:gender</tt>] gender of the current user, e.g. "m" or "f"
33
+ # [<tt>:keywords</tt>] keywords, e.g. "ruby gem admob"
34
+ # [<tt>:search</tt>] searchwords (much more restrictive than keywords), e.g. "ruby gem admob"
35
+ # [<tt>:title</tt>] title of the page, e.g. "Home Page"
36
+ # [<tt>:event</tt>] the event you want to report to analytics, e.g. "reg_success"
37
+ # [<tt>:text_only</tt>] if set to true, don't return a banner ad for this request
38
+ # [<tt>:test</tt>] whether this should issue a test ad request, not a real one
39
+ # [<tt>:timeout</tt>] override the default timeout value for this ad request in seconds, e.g. 2
40
+ # [<tt>:raise_exceptions</tt>] whether to raise exceptions when something goes wrong (defaults to false); exceptions will all be instances of AdMob::Error; a default can be set using <tt>AdMob::config {|c| c.raise_exceptions = true}</tt>
41
+ def self.request(request, session_id, params = {})
42
+ raise_exceptions = params[:raise_exceptions].nil? ? AdMob::Defaults.raise_exceptions : params[:raise_exceptions]
43
+
44
+ if raise_exceptions and !params[:cookie_domain].nil?
45
+ raise AdMob::Error.new("Cannot set cookie_domain in AdMob::request(), set the cookie_domain in the call to AdMob::set_cookie()")
46
+ end
47
+
48
+ if raise_exceptions and !params[:cookie_path].nil?
49
+ raise AdMob::Error.new("Cannot set cookie_path in AdMob::request(), set the cookie_path in the call to AdMob::set_cookie()")
50
+ end
51
+
52
+ # Build the post request
53
+ post_data = self.build_post_data(request, session_id, params)
54
+ if post_data.nil?
55
+ raise AdMob::Error.new("AdMob::request called as neither an ad nor an analytics request") if raise_exceptions
56
+ return ''
57
+ end
58
+
59
+ # Send request
60
+ req = Net::HTTP::Post.new(ENDPOINT.path)
61
+ req.set_form_data(post_data)
62
+ conn = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port)
63
+ timeout = params[:timeout] || AdMob::Defaults.timeout || DEFAULT_TIMEOUT
64
+ conn.read_timeout = timeout
65
+ conn.open_timeout = timeout
66
+ begin
67
+ start = Time.now.getutc.to_f
68
+ response = conn.start {|http| http.request(req)}
69
+ contents = response.body
70
+ rescue Timeout::Error => te
71
+ raise AdMob::Error.new("AdMob::request timed out; timeout was #{timeout}, elapsed time was #{Time.now.to_f - post_data['z']}") if raise_exceptions
72
+ rescue
73
+ raise AdMob::Error.new("AdMob::request encountered unexpected exception #{$!}") if raise_exceptions
74
+ ensure
75
+ contents ||= ''
76
+ lt = Time.now.getutc.to_f - start
77
+ end
78
+
79
+ # If appropriate, add the analytics pixel
80
+ if !request.env['admob_pixel_sent']
81
+ request.env['admob_pixel_sent'] = true
82
+ contents << '<img src="http://p.admob.com/e0?'
83
+ contents << "rt=#{post_data['rt']}&amp;"
84
+ contents << "z=#{post_data['z']}&amp;"
85
+ contents << "a=#{post_data['a']}&amp;"
86
+ contents << "s=#{post_data['s']}&amp;"
87
+ contents << "o=#{post_data['o']}&amp;"
88
+ contents << "lt=%0.4f&amp;" % lt
89
+ contents << "to=#{timeout}"
90
+ contents << '" alt="" width="1" height="1"/>'
91
+ end
92
+
93
+ contents
94
+ end
95
+
96
+ # This function should be called from an ActionController to set a cookie on behalf of AdMob.
97
+ # If you need to override the default cookie domain or cookie path, pass these as optional parameters to AdMob::set_cookie().
98
+ # AdMob::set_cookie(request, cookies, :cookie_domain => 'example.com', :cookie_path => '/videos')
99
+ # You can NOT pass cookie_domain or cookie_path as optional parameters to AdMob::request()
100
+ # AdMob recommends using a before_filter in your ActionController::Base class (usually in app/controllers/application.rb) to call set_cookie on each request.
101
+ # Here is a sample application.rb.
102
+ # require 'admob'
103
+ # # Filters added to this controller apply to all controllers in the application.
104
+ # # Likewise, all the methods added will be available for all controllers.
105
+ #
106
+ # class ApplicationController < ActionController::Base
107
+ # before_filter :admob_set_cookie
108
+ #
109
+ # def admob_set_cookie
110
+ # AdMob::set_cookie(request, cookies)
111
+ # end
112
+ # end
113
+ def self.set_cookie(request, cookies, params = {})
114
+ # don't make a new cookie if one already exists
115
+ return if request.env['admobuu'] or cookies[:admobuu]
116
+
117
+ # make a new cookie
118
+ ua = request.user_agent || ''
119
+ value = Digest::MD5.hexdigest(rand().to_s + ua + request.remote_ip + Time.now.to_f.to_s)
120
+ new_cookie = { :value => value,
121
+ :expires => Time.at(0x7fffffff), # end of 32 bit time
122
+ :path => params[:cookie_path] || AdMob::Defaults.cookie_path || "/" }
123
+
124
+ domain = params[:cookie_domain] || AdMob::Defaults.cookie_domain
125
+ if domain
126
+ domain = '.' + domain if domain[0].chr != '.'
127
+ new_cookie[:domain] = domain
128
+ end
129
+ cookies[:admobuu] = new_cookie
130
+
131
+ # make this cookie visible to the current page
132
+ request.env['admobuu'] = value
133
+ end
134
+
135
+ # Provides access to AdMob config, used for setting default request info.
136
+ # Currently, can be used to set defaults for: publisher_id, analytics_id, ad encoding, request timeout, cookie_domain, cookie_path
137
+ # and whether exceptions are raised when something goes wrong.
138
+ # For example, in environment.rb:
139
+ # require 'admob'
140
+ # AdMob::config do |c|
141
+ # c.publisher_id = 'YOUR_DEFAULT_PUBLISHER_ID'
142
+ # c.analytics_id = 'YOUR_DEFAULT_ANALYTICS_ID'
143
+ # c.encoding = 'SJIS'
144
+ # c.timeout = 3
145
+ # c.raise_exceptions = true
146
+ # c.cookie_domain = 'example.com' # this can also be passed to AdMob::set_cookie() but not AdMob::request()
147
+ # c.cookie_path = '/' # this can also be passed to AdMob::set_cookie() but not AdMob:request()
148
+ # end
149
+ def self.config
150
+ yield AdMob::Defaults
151
+ end
152
+
153
+ # Simple exception class used for all AdMob exceptions. By default, exceptions are never raised.
154
+ # To enable raising of exceptions, set parameter :raise_exceptions => true for a request, or
155
+ # set a default using AdMob::config (see AdMob::config documentation).
156
+ class Error < StandardError
157
+ end
158
+
159
+ private
160
+
161
+ # Stores default values for AdMob requests. Set these defaults via AdMob::config.
162
+ class Defaults
163
+ class << self
164
+ attr_accessor :publisher_id, :analytics_id, :encoding, :timeout, :raise_exceptions,
165
+ :cookie_domain, :cookie_path
166
+ end
167
+ end
168
+
169
+ def self.build_post_data(request, session_id, params)
170
+ # Gather basic data
171
+ publisher_id = params[:publisher_id] || AdMob::Defaults.publisher_id
172
+ analytics_id = params[:analytics_id] || AdMob::Defaults.analytics_id
173
+ test = params[:test].nil? ? (RAILS_ENV == 'test') : params[:test]
174
+ encoding = params[:encoding] || AdMob::Defaults.encoding
175
+
176
+ # Determine the type of request
177
+ analytics_request = (params[:analytics_request] != false) && (!analytics_id.nil?) && (!analytics_id.strip.empty?) && (!request.env['admob_pixel_sent'])
178
+ ad_request = (params[:ad_request] != false) && (!publisher_id.nil?) && (!publisher_id.strip.empty?)
179
+
180
+ case [ad_request, analytics_request]
181
+ when [false, false] then return nil
182
+ when [true, false] then request_type = 0
183
+ when [false, true] then request_type = 1
184
+ when [true, true] then request_type = 2
185
+ end
186
+
187
+ admob_cookie= request.cookies['admobuu']
188
+ admob_cookie_char= admob_cookie ? admob_cookie[0] : nil
189
+
190
+ # Build the basic request
191
+ post_data = {
192
+ 'rt' => request_type,
193
+ 'z' => Time.now.getutc.to_f,
194
+ 'u' => request.user_agent,
195
+ 'i' => request.remote_ip,
196
+ 'p' => request.fullpath,
197
+ 't' => Digest::MD5.hexdigest(session_id),
198
+ 'v' => PUBCODE_VERSION,
199
+ 'o' => admob_cookie_char || request.env['admobuu'],
200
+ 's' => publisher_id,
201
+ 'a' => analytics_id,
202
+ 'ma' => params[:markup],
203
+ 'd[pc]' => params[:postal_code],
204
+ 'd[ac]' => params[:area_code],
205
+ 'd[coord]' => params[:coordinates],
206
+ 'd[dob]' => params[:dob],
207
+ 'd[gender]' => params[:gender],
208
+ 'k' => params[:keywords],
209
+ 'search' => params[:search],
210
+ 'f' => 'html',
211
+ 'title' => params[:title],
212
+ 'event' => params[:event]
213
+ }
214
+
215
+ # Add in headers
216
+ ignore_headers = Set['HTTP_PRAGMA', 'HTTP_CACHE_CONTROL', 'HTTP_CONNECTION',
217
+ 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'ADMOB_PIXEL_SENT', 'ADMOBUU']
218
+ request.env.each { |k,v|
219
+ #also make sure don't includes rails3 headers 'rack.', 'action_controller.' etc (all lowercase)
220
+ post_data["h[#{k}]"] = v unless (ignore_headers.include?(k.upcase.gsub(/-/,'_'))) or (k[0] > 'Z')
221
+ }
222
+
223
+ # Add in optional data
224
+ post_data['e'] = encoding if encoding
225
+ post_data['y'] = 'text' if params[:text_only]
226
+ post_data['m'] = 'test' if test
227
+
228
+ # Don't send anything that's nil (but send if empty string)
229
+ post_data.delete_if {|k,v| v.nil?}
230
+ end
231
+
232
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: admob19
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 5
9
+ version: 1.1.5
10
+ platform: ruby
11
+ authors:
12
+ - AdMobAdMob
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-30 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hoe
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 12
31
+ - 2
32
+ version: 1.12.2
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Enables easy integration of AdMob ads into a RoR mobile site.
36
+ email: support@admob.comsupport@admob.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - History.txt
43
+ - Manifest.txt
44
+ - README.txt
45
+ files:
46
+ - History.txt
47
+ - Manifest.txt
48
+ - README.txt
49
+ - Rakefile
50
+ - lib/admob.rb
51
+ has_rdoc: true
52
+ homepage: http://www.admob.com/
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --main
58
+ - README.txt
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project: admob19
80
+ rubygems_version: 1.3.7
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Enables easy integration of AdMob ads into a RoR mobile site.
84
+ test_files: []
85
+