tatyree-admob 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +9 -0
- data/Manifest +6 -0
- data/Rakefile +12 -0
- data/admob.gemspec +31 -0
- data/lib/admob.rb +218 -0
- metadata +65 -0
data/History.txt
ADDED
data/Manifest
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('admob', '1.1.0') do |p|
|
6
|
+
p.description = "Enables easy integration of AdMob ads into a RoR mobile site. Bypasses the original gem's cookie requirement."
|
7
|
+
p.url = "http://github.com/tatyree/admob"
|
8
|
+
p.author = "AdMob, Todd Tyree"
|
9
|
+
p.email = "todd@snappl.co.uk"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
data/admob.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{admob}
|
5
|
+
s.version = "1.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["AdMob, Todd Tyree"]
|
9
|
+
s.date = %q{2009-03-08}
|
10
|
+
s.description = %q{Enables easy integration of AdMob ads into a RoR mobile site. Bypasses the original gem's cookie requirement.}
|
11
|
+
s.email = %q{todd@snappl.co.uk}
|
12
|
+
s.extra_rdoc_files = ["lib/admob.rb", "README.txt"]
|
13
|
+
s.files = ["History.txt", "lib/admob.rb", "Manifest.txt", "Rakefile", "README.txt", "Manifest", "admob.gemspec"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/tatyree/admob}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Admob", "--main", "README.txt"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{admob}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Enables easy integration of AdMob ads into a RoR mobile site. Bypasses the original gem's cookie requirement.}
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 2
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
else
|
28
|
+
end
|
29
|
+
else
|
30
|
+
end
|
31
|
+
end
|
data/lib/admob.rb
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require '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.0'
|
12
|
+
|
13
|
+
ENDPOINT = URI.parse('http://r.admob.com/ad_source.php')
|
14
|
+
PUBCODE_VERSION = '20081105-RUBY-70e4a867449121c5'
|
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, ...) %></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, params = {})
|
42
|
+
raise_exceptions = params[:raise_exceptions].nil? ? AdMob::Defaults.raise_exceptions : params[:raise_exceptions]
|
43
|
+
|
44
|
+
# Build the post request
|
45
|
+
post_data = self.build_post_data(request, session, params)
|
46
|
+
if post_data.nil?
|
47
|
+
raise AdMob::Error.new("AdMob::request called as neither an ad nor an analytics request") if raise_exceptions
|
48
|
+
return ''
|
49
|
+
end
|
50
|
+
|
51
|
+
# Send request
|
52
|
+
req = Net::HTTP::Post.new(ENDPOINT.path)
|
53
|
+
req.set_form_data(post_data)
|
54
|
+
conn = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port)
|
55
|
+
timeout = params[:timeout] || AdMob::Defaults.timeout || DEFAULT_TIMEOUT
|
56
|
+
conn.read_timeout = timeout
|
57
|
+
conn.open_timeout = timeout
|
58
|
+
begin
|
59
|
+
start = Time.now.getutc.to_f
|
60
|
+
response = conn.start {|http| http.request(req)}
|
61
|
+
contents = response.body
|
62
|
+
rescue Timeout::Error => te
|
63
|
+
raise AdMob::Error.new("AdMob::request timed out; timeout was #{timeout}, elapsed time was #{Time.now.to_f - post_data['z']}") if raise_exceptions
|
64
|
+
rescue
|
65
|
+
raise AdMob::Error.new("AdMob::request encountered unexpected exception #{$!}") if raise_exceptions
|
66
|
+
ensure
|
67
|
+
contents ||= ''
|
68
|
+
lt = Time.now.getutc.to_f - start
|
69
|
+
end
|
70
|
+
|
71
|
+
# If appropriate, add the analytics pixel
|
72
|
+
if !request.env['admob_pixel_sent']
|
73
|
+
request.env['admob_pixel_sent'] = true
|
74
|
+
contents << '<img src="http://p.admob.com/e0?'
|
75
|
+
contents << "rt=#{post_data['rt']}&"
|
76
|
+
contents << "z=#{post_data['z']}&"
|
77
|
+
contents << "a=#{post_data['a']}&"
|
78
|
+
contents << "s=#{post_data['s']}&"
|
79
|
+
contents << "o=#{post_data['o']}&"
|
80
|
+
contents << "lt=%0.4f&" % lt
|
81
|
+
contents << "to=#{timeout}"
|
82
|
+
contents << '" alt="" width="1" height="1"/>'
|
83
|
+
end
|
84
|
+
|
85
|
+
contents
|
86
|
+
end
|
87
|
+
|
88
|
+
# This function should be called from an ActionController to set a cookie on behalf of AdMob.
|
89
|
+
# AdMob recommends using a before_filter in your ActionController::Base class (usually in app/controllers/application.rb) to call set_cookie on each request.
|
90
|
+
# Here is a sample application.rb.
|
91
|
+
# require 'admob'
|
92
|
+
# # Filters added to this controller apply to all controllers in the application.
|
93
|
+
# # Likewise, all the methods added will be available for all controllers.
|
94
|
+
#
|
95
|
+
# class ApplicationController < ActionController::Base
|
96
|
+
# before_filter :admob_set_cookie
|
97
|
+
#
|
98
|
+
# def admob_set_cookie
|
99
|
+
# AdMob::set_cookie(request, cookies)
|
100
|
+
# end
|
101
|
+
# end
|
102
|
+
def self.set_cookie(request, cookies, session)
|
103
|
+
# First, see if the env or the cookies is set and return.
|
104
|
+
return cookies[:admobuu][0] if cookies[:admobuu]
|
105
|
+
|
106
|
+
# Try the session for recovering the value next. For unreliable handlers of cookies, this *SHOULD* allow us
|
107
|
+
# to maintain the admobuu across a flip-flopping session.
|
108
|
+
return session[:admobuu] if !session[:admobuu].blank?
|
109
|
+
|
110
|
+
# make a new cookie
|
111
|
+
value = MD5.hexdigest(rand().to_s + request.user_agent + request.remote_ip + Time.now.to_f.to_s)
|
112
|
+
new_cookie = { :value => value,
|
113
|
+
:expires => Time.at(0x7fffffff), # end of 32 bit time
|
114
|
+
:path => "/" }
|
115
|
+
|
116
|
+
if AdMob::Defaults.cookie_domain
|
117
|
+
domain = AdMob::Defaults.cookie_domain
|
118
|
+
domain = '.' + domain if domain[0].chr != '.'
|
119
|
+
new_cookie[:domain] = domain
|
120
|
+
end
|
121
|
+
cookies[:admobuu] = new_cookie
|
122
|
+
|
123
|
+
session[:admobuu] = value
|
124
|
+
|
125
|
+
# Return the value so we can manually set the environment if cookies aren't on
|
126
|
+
value
|
127
|
+
end
|
128
|
+
|
129
|
+
# Provides access to AdMob config, used for setting default request info.
|
130
|
+
# Currently, can be used to set defaults for: publisher_id, analytics_id, ad encoding, request timeout, cookie_domain,
|
131
|
+
# and whether exceptions are raised when something goes wrong.
|
132
|
+
# For example, in environment.rb:
|
133
|
+
# require 'admob'
|
134
|
+
# AdMob::config do |c|
|
135
|
+
# c.publisher_id = 'YOUR_DEFAULT_PUBLISHER_ID'
|
136
|
+
# c.analytics_id = 'YOUR_DEFAULT_ANALYTICS_ID'
|
137
|
+
# c.encoding = 'SJIS'
|
138
|
+
# c.timeout = 3
|
139
|
+
# c.raise_exceptions = true
|
140
|
+
# c.cookie_domain = 'example.com'
|
141
|
+
# end
|
142
|
+
def self.config
|
143
|
+
yield AdMob::Defaults
|
144
|
+
end
|
145
|
+
|
146
|
+
# Simple exception class used for all AdMob exceptions. By default, exceptions are never raised.
|
147
|
+
# To enable raising of exceptions, set parameter :raise_exceptions => true for a request, or
|
148
|
+
# set a default using AdMob::config (see AdMob::config documentation).
|
149
|
+
class Error < StandardError
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
# Stores default values for AdMob requests. Set these defaults via AdMob::config.
|
155
|
+
class Defaults
|
156
|
+
class << self
|
157
|
+
attr_accessor :publisher_id, :analytics_id, :encoding, :timeout, :raise_exceptions, :cookie_domain
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def self.build_post_data(request, session, params)
|
162
|
+
# Gather basic data
|
163
|
+
publisher_id = params[:publisher_id] || AdMob::Defaults.publisher_id
|
164
|
+
analytics_id = params[:analytics_id] || AdMob::Defaults.analytics_id
|
165
|
+
test = params[:test].nil? ? (RAILS_ENV == 'test') : params[:test]
|
166
|
+
encoding = params[:encoding] || AdMob::Defaults.encoding
|
167
|
+
|
168
|
+
# Determine the type of request
|
169
|
+
analytics_request = (params[:analytics_request] != false) && (!analytics_id.nil?) && (!analytics_id.strip.empty?) && (!request.env['admob_pixel_sent'])
|
170
|
+
ad_request = (params[:ad_request] != false) && (!publisher_id.nil?) && (!publisher_id.strip.empty?)
|
171
|
+
|
172
|
+
case [ad_request, analytics_request]
|
173
|
+
when [false, false] then return nil
|
174
|
+
when [true, false] then request_type = 0
|
175
|
+
when [false, true] then request_type = 1
|
176
|
+
when [true, true] then request_type = 2
|
177
|
+
end
|
178
|
+
|
179
|
+
# Build the basic request
|
180
|
+
post_data = {
|
181
|
+
'rt' => request_type,
|
182
|
+
'z' => Time.now.getutc.to_f,
|
183
|
+
'u' => request.user_agent,
|
184
|
+
'i' => request.remote_ip,
|
185
|
+
'p' => request.request_uri,
|
186
|
+
't' => MD5.hexdigest(session.session_id),
|
187
|
+
'v' => PUBCODE_VERSION,
|
188
|
+
'o' => request.cookies['admobuu'][0] || session[:admobuu],
|
189
|
+
's' => publisher_id,
|
190
|
+
'a' => analytics_id,
|
191
|
+
'ma' => params[:markup],
|
192
|
+
'd[pc]' => params[:postal_code],
|
193
|
+
'd[ac]' => params[:area_code],
|
194
|
+
'd[coord]' => params[:coordinates],
|
195
|
+
'd[dob]' => params[:dob],
|
196
|
+
'd[gender]' => params[:gender],
|
197
|
+
'k' => params[:keywords],
|
198
|
+
'search' => params[:search],
|
199
|
+
'f' => 'html',
|
200
|
+
'title' => params[:title],
|
201
|
+
'event' => params[:event]
|
202
|
+
}
|
203
|
+
|
204
|
+
# Add in headers
|
205
|
+
ignore_headers = Set['HTTP_PRAGMA', 'HTTP_CACHE_CONTROL', 'HTTP_CONNECTION',
|
206
|
+
'HTTP_USER_AGENT', 'HTTP_COOKIE', 'ADMOB_PIXEL_SENT', 'ADMOBUU']
|
207
|
+
request.env.each {|k,v| post_data["h[#{k}]"] = v unless ignore_headers.include?(k.upcase.gsub(/-/,'_'))}
|
208
|
+
|
209
|
+
# Add in optional data
|
210
|
+
post_data['e'] = encoding if encoding
|
211
|
+
post_data['y'] = 'text' if params[:text_only]
|
212
|
+
post_data['m'] = 'test' if test
|
213
|
+
|
214
|
+
# Don't send anything that's nil (but send if empty string)
|
215
|
+
post_data.delete_if {|k,v| v.nil?}
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tatyree-admob
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- AdMob, Todd Tyree
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-08 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Enables easy integration of AdMob ads into a RoR mobile site. Bypasses the original gem's cookie requirement.
|
17
|
+
email: todd@snappl.co.uk
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- lib/admob.rb
|
24
|
+
- README.txt
|
25
|
+
files:
|
26
|
+
- History.txt
|
27
|
+
- lib/admob.rb
|
28
|
+
- Manifest.txt
|
29
|
+
- Rakefile
|
30
|
+
- README.txt
|
31
|
+
- Manifest
|
32
|
+
- admob.gemspec
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/tatyree/admob
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --line-numbers
|
38
|
+
- --inline-source
|
39
|
+
- --title
|
40
|
+
- Admob
|
41
|
+
- --main
|
42
|
+
- README.txt
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "1.2"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project: admob
|
60
|
+
rubygems_version: 1.2.0
|
61
|
+
signing_key:
|
62
|
+
specification_version: 2
|
63
|
+
summary: Enables easy integration of AdMob ads into a RoR mobile site. Bypasses the original gem's cookie requirement.
|
64
|
+
test_files: []
|
65
|
+
|