admob 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/Manifest.txt +5 -0
- data/README.txt +71 -0
- data/Rakefile +13 -0
- data/lib/admob.rb +173 -0
- metadata +68 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
= AdMob Ruby on Rails integration code
|
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
|
+
end
|
24
|
+
* In a view, where you want an ad, place an ad request.
|
25
|
+
* Note: You must provide your publisher_id and/or analytics_id in the default setting, or when you make the ad request (see below).
|
26
|
+
|
27
|
+
=== Sample requests
|
28
|
+
|
29
|
+
==== If you've configured a default publisher and/or analytics id
|
30
|
+
|
31
|
+
<%= AdMob::request(request, session.session_id) %>
|
32
|
+
|
33
|
+
==== If you didn't configure a default publisher_id or want to override the configured default publisher_id
|
34
|
+
|
35
|
+
<%= AdMob::request(request, session.session_id, :publisher_id => "YOUR_PUBLISHER_ID") %>
|
36
|
+
|
37
|
+
==== Request a test ad (happens by default when in test or development mode)
|
38
|
+
|
39
|
+
<%= AdMob::request(request, session.session_id, :test => true) %>
|
40
|
+
|
41
|
+
==== Request an ad, providing demographic info and keywords
|
42
|
+
|
43
|
+
<%= AdMob::request(request, session.session_id, :publisher_id => "YOUR_PUBLISHER_ID", :dob => "19800229", :gender => "male", :postal_code => "90210", :keywords => 'ruby gem admob') %>
|
44
|
+
|
45
|
+
==== Make an analytics request only
|
46
|
+
|
47
|
+
<%= AdMob::request(request, session.session_id, :ad_request => false) %>
|
48
|
+
|
49
|
+
See the documentation for #AdMob::request for a list of all params that can be set for a request.
|
50
|
+
|
51
|
+
== REQUIREMENTS:
|
52
|
+
|
53
|
+
* An AdMob publisher_id and/or analytics_id (get them at http://www.admob.com)
|
54
|
+
|
55
|
+
== INSTALL:
|
56
|
+
|
57
|
+
<tt>sudo gem install admob</tt>
|
58
|
+
|
59
|
+
== LICENSE:
|
60
|
+
|
61
|
+
(The BSD License)
|
62
|
+
|
63
|
+
Copyright (c) 2008, AdMob, Inc.
|
64
|
+
All rights reserved.
|
65
|
+
|
66
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
67
|
+
|
68
|
+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
69
|
+
* 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.
|
70
|
+
|
71
|
+
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,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hoe'
|
3
|
+
require './lib/admob.rb'
|
4
|
+
|
5
|
+
Hoe.new('admob', AdMob::GEM_VERSION) do |p|
|
6
|
+
p.rubyforge_name = 'admob'
|
7
|
+
p.summary = 'Enables easy integration of AdMob ads into a RoR mobile site.'
|
8
|
+
p.author = 'AdMob'
|
9
|
+
p.email = 'support@admob.com'
|
10
|
+
p.url = 'http://www.admob.com/'
|
11
|
+
p.description = 'Enables easy integration of AdMob ads into a RoR mobile site.'
|
12
|
+
p.developer('AdMob', 'support@admob.com')
|
13
|
+
end
|
data/lib/admob.rb
ADDED
@@ -0,0 +1,173 @@
|
|
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.0.2'
|
12
|
+
|
13
|
+
ENDPOINT = URI.parse('http://r.admob.com/ad_source.php')
|
14
|
+
PUBCODE_VERSION = '20080630-RUBY-924f7dd99e8828c2'
|
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
|
+
# Guarantee that only one analytics call gets made per request
|
43
|
+
if request.env['admob_analytics_request_issued'] == true
|
44
|
+
params[:analytics_request] = false
|
45
|
+
end
|
46
|
+
|
47
|
+
raise_exceptions = params[:raise_exceptions].nil? ? AdMob::Defaults.raise_exceptions : params[:raise_exceptions]
|
48
|
+
|
49
|
+
# Build the post request
|
50
|
+
post_data = self.build_post_data(request, session_id, params)
|
51
|
+
if post_data.nil?
|
52
|
+
raise AdMob::Error.new("AdMob::request called as neither an ad nor an analytics request") if raise_exceptions
|
53
|
+
return ''
|
54
|
+
end
|
55
|
+
|
56
|
+
# Send request
|
57
|
+
req = Net::HTTP::Post.new(ENDPOINT.path)
|
58
|
+
req.set_form_data(post_data)
|
59
|
+
conn = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port)
|
60
|
+
timeout = params[:timeout] || AdMob::Defaults.timeout || DEFAULT_TIMEOUT
|
61
|
+
conn.read_timeout = timeout
|
62
|
+
conn.open_timeout = timeout
|
63
|
+
begin
|
64
|
+
response = conn.start {|http| http.request(req)}
|
65
|
+
contents = response.body
|
66
|
+
rescue Timeout::Error => te
|
67
|
+
contents = %Q|<img src="http://t.admob.com/li.php/c.gif/#{post_data['s']}/1/#{timeout}/#{MD5.hexdigest(post_data['p'])}" alt="" width="1" height="1" />|
|
68
|
+
raise AdMob::Error.new("AdMob::request timed out; timeout was #{timeout}, elapsed time was #{Time.now.to_f - post_data['z']}") if raise_exceptions
|
69
|
+
rescue
|
70
|
+
raise AdMob::Error.new("AdMob::request encountered unexpected exception #{$!}") if raise_exceptions
|
71
|
+
ensure
|
72
|
+
contents ||= ''
|
73
|
+
end
|
74
|
+
|
75
|
+
# If appropriate, add the analytics pixel
|
76
|
+
if post_data['rt'] > 0
|
77
|
+
contents << %Q|<img src="http://a.admob.com/e0?a=#{post_data['a']}&s=#{post_data['s']}&z=#{post_data['z']}&lt=#{Time.now.to_f - post_data['z']}&rt=#{post_data['rt']}" alt="" width="1" height="1"/>|
|
78
|
+
end
|
79
|
+
|
80
|
+
contents
|
81
|
+
end
|
82
|
+
|
83
|
+
# Provides access to AdMob config, used for setting default request info.
|
84
|
+
# Currently, can be used to set defaults for: publisher_id, analytics_id, ad encoding, request timeout,
|
85
|
+
# and whether exceptions are raised when something goes wrong.
|
86
|
+
# For example, in environment.rb:
|
87
|
+
# require 'admob'
|
88
|
+
# AdMob::config do |c|
|
89
|
+
# c.publisher_id = 'YOUR_DEFAULT_PUBLISHER_ID'
|
90
|
+
# c.analytics_id = 'YOUR_DEFAULT_ANALYTICS_ID'
|
91
|
+
# c.encoding = 'SJIS'
|
92
|
+
# c.timeout = 3
|
93
|
+
# c.raise_exceptions = true
|
94
|
+
# end
|
95
|
+
def self.config
|
96
|
+
yield AdMob::Defaults
|
97
|
+
end
|
98
|
+
|
99
|
+
# Simple exception class used for all AdMob exceptions. By default, exceptions are never raised.
|
100
|
+
# To enable raising of exceptions, set parameter :raise_exceptions => true for a request, or
|
101
|
+
# set a default using AdMob::config (see AdMob::config documentation).
|
102
|
+
class Error < StandardError
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
# Stores default values for AdMob requests. Set these defaults via AdMob::config.
|
108
|
+
class Defaults
|
109
|
+
class << self
|
110
|
+
attr_accessor :publisher_id, :analytics_id, :encoding, :timeout, :raise_exceptions
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.build_post_data(request, session_id, params)
|
115
|
+
# Gather basic data
|
116
|
+
publisher_id = params[:publisher_id] || AdMob::Defaults.publisher_id
|
117
|
+
analytics_id = params[:analytics_id] || AdMob::Defaults.analytics_id
|
118
|
+
test = params[:test].nil? ? (RAILS_ENV == 'test') : params[:test]
|
119
|
+
encoding = params[:encoding] || AdMob::Defaults.encoding
|
120
|
+
|
121
|
+
# Determine the type of request
|
122
|
+
analytics_request = (params[:analytics_request] != false) && (!analytics_id.nil?) && (!analytics_id.strip.empty?)
|
123
|
+
ad_request = (params[:ad_request] != false) && (!publisher_id.nil?) && (!publisher_id.strip.empty?)
|
124
|
+
|
125
|
+
case [ad_request, analytics_request]
|
126
|
+
when [false, false] then return nil
|
127
|
+
when [true, false] then request_type = 0
|
128
|
+
when [false, true] then request_type = 1
|
129
|
+
when [true, true] then request_type = 2
|
130
|
+
end
|
131
|
+
|
132
|
+
# mark for this request so we don't make analytics calls again
|
133
|
+
request.env['admob_analytics_request_issued'] = true if analytics_request
|
134
|
+
|
135
|
+
# Build the basic request
|
136
|
+
post_data = {
|
137
|
+
's' => publisher_id,
|
138
|
+
'a' => analytics_id,
|
139
|
+
'u' => request.user_agent,
|
140
|
+
'i' => request.remote_ip,
|
141
|
+
'p' => request.request_uri,
|
142
|
+
't' => MD5.hexdigest(session_id),
|
143
|
+
'ma' => params[:markup],
|
144
|
+
'rt' => request_type,
|
145
|
+
'v' => PUBCODE_VERSION,
|
146
|
+
'd[pc]' => params[:postal_code],
|
147
|
+
'd[ac]' => params[:area_code],
|
148
|
+
'd[coord]' => params[:coordinates],
|
149
|
+
'd[dob]' => params[:dob],
|
150
|
+
'd[gender]' => params[:gender],
|
151
|
+
'z' => Time.now.to_f,
|
152
|
+
'k' => params[:keywords],
|
153
|
+
'search' => params[:search],
|
154
|
+
'f' => 'html',
|
155
|
+
'title' => params[:title],
|
156
|
+
'event' => params[:event]
|
157
|
+
}
|
158
|
+
|
159
|
+
# Add in headers
|
160
|
+
ignore_headers = Set['HTTP_PRAGMA', 'HTTP_CACHE_CONTROL', 'HTTP_CONNECTION',
|
161
|
+
'HTTP_USER_AGENT', 'HTTP_COOKIE', 'ADMOB_ANALYTICS_REQUEST_ISSUED']
|
162
|
+
request.env.each {|k,v| post_data["h[#{k}]"] = v unless ignore_headers.include?(k.upcase.gsub(/-/,'_'))}
|
163
|
+
|
164
|
+
# Add in optional data
|
165
|
+
post_data['e'] = encoding if encoding
|
166
|
+
post_data['y'] = 'text' if params[:text_only]
|
167
|
+
post_data['m'] = 'test' if test
|
168
|
+
|
169
|
+
# Don't send anything that's nil (but send if empty string)
|
170
|
+
post_data.delete_if {|k,v| v.nil?}
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: admob
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- AdMobAdMob
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-29 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.6.0
|
23
|
+
version:
|
24
|
+
description: Enables easy integration of AdMob ads into a RoR mobile site.
|
25
|
+
email: support@admob.comsupport@admob.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- History.txt
|
32
|
+
- Manifest.txt
|
33
|
+
- README.txt
|
34
|
+
files:
|
35
|
+
- History.txt
|
36
|
+
- Manifest.txt
|
37
|
+
- README.txt
|
38
|
+
- Rakefile
|
39
|
+
- lib/admob.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://www.admob.com/
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --main
|
45
|
+
- README.txt
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project: admob
|
63
|
+
rubygems_version: 1.0.1
|
64
|
+
signing_key:
|
65
|
+
specification_version: 2
|
66
|
+
summary: Enables easy integration of AdMob ads into a RoR mobile site.
|
67
|
+
test_files: []
|
68
|
+
|