sinatra-mobile-adsense 0.1.1

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.
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by milk1000cc, munshkr
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # Mobile Adsense for Sinatra #
2
+
3
+ A Sinatra extension that provides a helper to show Google Adsense for Mobile.
4
+
5
+ ## Usage ##
6
+
7
+ To use it, just use the `mobile_adsense` helper in your application template. A `:client` option must be provided:
8
+
9
+ <%= mobile_adsense :client => 'pub-1234567890' %>
10
+
11
+ And that's it! You can pass any options.
12
+
13
+ <%= mobile_adsense :client => 'pub-1234567890', :format => 'mobile_double', :color_border => 'FFFFFF', :color_bg => 'FFFFFF' %>
14
+
15
+ The `mobile_adsense` helper accesses Google server to get advertisements. If an error occurs doing the request, it returns an empty string. Exceptions are **not** raised.
16
+
17
+ ## Install ##
18
+
19
+ You can install manually the gem:
20
+
21
+ $ gem install munshkr-sinatra-mobile-adsense -s http://gems.github.com/
22
+
23
+ Or, if you use Bundler, add this line on your Gemfile:
24
+
25
+ gem "sinatra-mobile-adsense", :git => "git://github.com/munshkr/sinatra-mobile-adsense.git"
26
+
27
+ and run `bundle install`.
28
+
29
+ Finally, register the extension in your Sinatra application:
30
+
31
+ class MyApp < Sinatra::Base
32
+ register Sinatra::MobileAdsense
33
+
34
+ # Your app code goes here
35
+ end
36
+
37
+ ## Legal ##
38
+
39
+ This extension is mostly based on the Rails [plugin](http://github.com/milk1000cc/mobile_adsense) by milk1000cc.
40
+
41
+ Copyright (c) 2011 milk1000cc, munshkr
42
+ Released under the MIT license
@@ -0,0 +1,13 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ desc 'Test sinatra-mobile-adsense extension.'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'lib'
10
+ t.libs << 'spec'
11
+ t.pattern = 'spec/**/spec_*.rb'
12
+ t.verbose = true
13
+ end
@@ -0,0 +1,88 @@
1
+ require "sinatra/base"
2
+ require "open-uri"
3
+
4
+ module Sinatra
5
+ module MobileAdsense
6
+
7
+ module Helpers
8
+ def mobile_adsense(options)
9
+ raise ArgumentError, ":client option must be defined" unless options[:client]
10
+
11
+ # TODO Should be timezone aware?
12
+ dt = "%.0f" % (1000 * Time.now.to_f)
13
+ user_agent = request.user_agent
14
+
15
+ options = options.dup
16
+ unless options[:client] =~ /^ca\-mb\-/
17
+ options[:client] = "ca-mb-#{ options[:client] }"
18
+ end
19
+ options = {
20
+ :ad_type => "text_image",
21
+ :channel => "",
22
+ :dt => dt,
23
+ :format => "mobile_single",
24
+ :ip => request.ip,
25
+ :markup => "xhtml",
26
+ :oe => "utf8",
27
+ :output => "xhtml",
28
+ :ref => (request.referer || ""),
29
+ :url => request.url,
30
+ :useragent => user_agent
31
+ }.merge(google_screen_res(request)).merge(google_muid(request)).merge(google_via_and_accept(request, user_agent)).merge(options)
32
+
33
+ ad_url = "http://pagead2.googlesyndication.com/pagead/ads?"
34
+ ad_url += options.map { |k, v|
35
+ v = google_color(v, dt) if k =~ /color_/
36
+ "#{ k }=#{ ERB::Util.u(v) }"
37
+ }.join("&")
38
+
39
+ begin
40
+ result = URI(ad_url).read
41
+ rescue StandardError, Timeout::Error
42
+ ""
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.registered(app)
48
+ app.helpers MobileAdsense::Helpers
49
+ end
50
+
51
+
52
+ def google_color(color, time)
53
+ color_array = color.split(",")
54
+ color_array[time.to_i % color_array.size]
55
+ end
56
+
57
+ def google_screen_res(request)
58
+ screen_res =
59
+ request.env["HTTP_UA_PIXELS"] ||
60
+ request.env["HTTP_X_UP_DEVCAP_SCREENPIXELS"] ||
61
+ request.env["HTTP_X_JPHONE_DISPLAY"] ||
62
+ ""
63
+ res_array = screen_res.split(/[x,*]/)
64
+ (res_array.size == 2) ? { :u_w => res_array[0], :u_h => res_array[1] } : {}
65
+ end
66
+
67
+ def google_muid(request)
68
+ muid =
69
+ request.env["HTTP_X_DCMGUID"] ||
70
+ request.env["HTTP_X_UP_SUBNO"] ||
71
+ request.env["HTTP_X_JPHONE_UID"] ||
72
+ request.env["HTTP_X_EM_UID"]
73
+ muid ? { :muid => muid } : {}
74
+ end
75
+
76
+ def google_via_and_accept(request, ua)
77
+ return {} if ua
78
+ via_and_accept = {}
79
+ via = request.env["HTTP_VIA"]
80
+ via_and_accept[:via] = request.env["HTTP_VIA"] if via
81
+ accept = request.env["HTTP_ACCEPT"]
82
+ via_and_accept[:accept] = request.env["HTTP_ACCEPT"] if accept
83
+ via_and_accept
84
+ end
85
+ end
86
+
87
+ register MobileAdsense
88
+ end
@@ -0,0 +1,172 @@
1
+ require "rubygems"
2
+ gem "rspec", ">= 2.0"
3
+
4
+ require "rspec"
5
+ require "sinatra"
6
+ require "rack/test"
7
+ require "webmock"
8
+
9
+ require "sinatra/mobile_adsense"
10
+
11
+ RSpec.configure do |config|
12
+ config.include WebMock::API
13
+ WebMock.disable_net_connect!
14
+ end
15
+
16
+ set :environment, :test
17
+
18
+ class Rack::Request
19
+ def secure?
20
+ (@env['HTTP_X_FORWARDED_PROTO'] || @env['rack.url_scheme']) == 'https'
21
+ end
22
+ end
23
+
24
+
25
+ describe "MobileAdsense" do
26
+ include Rack::Test::Methods
27
+ include Sinatra::MobileAdsense
28
+ include Sinatra::MobileAdsense::Helpers
29
+
30
+ attr_accessor :request
31
+
32
+ def app
33
+ Sinatra::Application
34
+ end
35
+
36
+ def reset_request
37
+ get "/"
38
+ @request = last_request
39
+ end
40
+
41
+ before { reset_request }
42
+
43
+ describe "::mobile_adsense" do
44
+ def set_stub_request(query = {})
45
+ stub_request(:get, "pagead2.googlesyndication.com/pagead/ads").
46
+ with(:query => {
47
+ :client => "ca-mb-pub-1234567890",
48
+ :ad_type => "text_image",
49
+ :channel => "",
50
+ :dt => "1287840160591",
51
+ :format => "mobile_single",
52
+ :ip => @request.ip,
53
+ :markup => "xhtml",
54
+ :oe => "utf8",
55
+ :output => "xhtml",
56
+ :ref => "/",
57
+ :url => @request.url,
58
+ :useragent => @request.user_agent
59
+ }.merge(query)).
60
+ to_return :body => @ad_output
61
+ end
62
+
63
+ before { @ad_output = "<adsense>" }
64
+
65
+ before do
66
+ Time.stub_chain :now, :to_f => 1287840160.59053
67
+ end
68
+
69
+ it "should need 'client' option" do
70
+ set_stub_request
71
+ lambda { mobile_adsense(:client => "pub-1234567890") }.should_not raise_error(ArgumentError)
72
+ lambda { mobile_adsense(:foo => "var", :client => "pub-1234567890") }.should_not raise_error(ArgumentError)
73
+ lambda { mobile_adsense }.should raise_error(ArgumentError)
74
+ lambda { mobile_adsense(:foo => "var") }.should raise_error(ArgumentError)
75
+ end
76
+
77
+ it "should return the adsense's output" do
78
+ set_stub_request
79
+ mobile_adsense(:client => "pub-1234567890").should == @ad_output
80
+ mobile_adsense(:client => "ca-mb-pub-1234567890").should == @ad_output
81
+ end
82
+
83
+ it "should consider passed paramerters" do
84
+ set_stub_request :color_border => "ffffff"
85
+ mobile_adsense(:client => "pub-1234567890", :color_border => "ffffff").should == @ad_output
86
+ end
87
+
88
+ context "when an error happens while requesting for google" do
89
+ it "should return empty string" do
90
+ stub_request(:get, %r!pagead2.googlesyndication.com/pagead/ads!).to_timeout
91
+ lambda { mobile_adsense(:client => "pub-1234567890") }.should_not raise_error
92
+ mobile_adsense(:client => "pub-1234567890").should == ""
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "::google_color" do
98
+ it "should return color code" do
99
+ google_color("ffffff,cccccc", 0).should == "ffffff"
100
+ google_color("ffffff,cccccc", 1).should == "cccccc"
101
+ google_color("ffffff,cccccc", 2).should == "ffffff"
102
+ end
103
+ end
104
+
105
+ describe "::google_screen_res" do
106
+ it "should return 'u_w' and 'u_h' parameters" do
107
+ @request.env["HTTP_UA_PIXELS"] = "240x320"
108
+ google_screen_res(@request).should == { :u_w => "240", :u_h => "320" }
109
+
110
+ reset_request
111
+ @request.env["HTTP_X_UP_DEVCAP_SCREENPIXELS"] = "200,300"
112
+ google_screen_res(@request).should == { :u_w => "200", :u_h => "300" }
113
+
114
+ reset_request
115
+ @request.env["HTTP_X_JPHONE_DISPLAY"] = "250*330"
116
+ google_screen_res(@request).should == { :u_w => "250", :u_h => "330" }
117
+
118
+ reset_request
119
+ @request.env["HTTP_UA_PIXELS"] = "240"
120
+ google_screen_res(@request).should == {}
121
+
122
+ reset_request
123
+ google_screen_res(@request).should == {}
124
+ end
125
+ end
126
+
127
+ describe "::google_muid" do
128
+ it "should return 'muid' parameter" do
129
+ @request.env["HTTP_X_DCMGUID"] = "XXXXX000000"
130
+ google_muid(@request).should == { :muid => "XXXXX000000" }
131
+
132
+ reset_request
133
+ @request.env["HTTP_X_UP_SUBNO"] = "00000000000000_mj.ezweb.ne.jp"
134
+ google_muid(@request).should == { :muid => "00000000000000_mj.ezweb.ne.jp" }
135
+
136
+ reset_request
137
+ @request.env["HTTP_X_JPHONE_UID"] = "aaaaaaaaaaaaaaaa"
138
+ google_muid(@request).should == { :muid => "aaaaaaaaaaaaaaaa" }
139
+
140
+ reset_request
141
+ @request.env["HTTP_X_EM_UID"] = "bbbbbbbbbbbbbbbb"
142
+ google_muid(@request).should == { :muid => "bbbbbbbbbbbbbbbb" }
143
+
144
+ reset_request
145
+ google_muid(@request).should == {}
146
+ end
147
+ end
148
+
149
+ describe "::google_via_and_accept" do
150
+ it "should return 'via' and 'accept' parameters when user agent is undefined" do
151
+ @request.env["HTTP_VIA"] = "via"
152
+ google_via_and_accept(@request, nil).should == { :via => "via" }
153
+
154
+ reset_request
155
+ @request.env["HTTP_ACCEPT"] = "accept"
156
+ google_via_and_accept(@request, nil).should == { :accept => "accept" }
157
+
158
+ reset_request
159
+ @request.env["HTTP_VIA"] = "via!"
160
+ @request.env["HTTP_ACCEPT"] = "accept!"
161
+ google_via_and_accept(@request, nil).should == { :via => "via!", :accept => "accept!" }
162
+
163
+ reset_request
164
+ google_via_and_accept(@request, "ua").should == {}
165
+
166
+ reset_request
167
+ @request.env["HTTP_VIA"] = "via!!"
168
+ @request.env["HTTP_ACCEPT"] = "accept!!"
169
+ google_via_and_accept(@request, "ua").should == {}
170
+ end
171
+ end
172
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-mobile-adsense
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - milk1000cc
13
+ - munshkr
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-15 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: munshkr@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - MIT-LICENSE
30
+ - README.md
31
+ files:
32
+ - MIT-LICENSE
33
+ - README.md
34
+ - Rakefile
35
+ - lib/sinatra/mobile_adsense.rb
36
+ - spec/spec_mobile_adsense.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/munshkr/sinatra-mobile-adsense
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Sinatra extension that provides a helper for showing Google Adsense for Mobile.
69
+ test_files:
70
+ - spec/spec_mobile_adsense.rb