Permalink
Browse files

Add support for adsense ads

  • Loading branch information...
1 parent 6660895 commit e206a750eaba9a237485d7d55d62e8bec5e5ea9a Sebastian von Conrad committed Jan 28, 2012
Showing with 78 additions and 0 deletions.
  1. +1 −0 lib/in_bed_with.rb
  2. +40 −0 lib/in_bed_with/adsense.rb
  3. +37 −0 spec/in_bed_with/adsense_spec.rb
View
@@ -5,6 +5,7 @@
require 'active_support/inflector'
require 'in_bed_with/base'
+require 'in_bed_with/adsense'
require 'in_bed_with/analytics'
require 'in_bed_with/gauges'
View
@@ -0,0 +1,40 @@
+module InBedWith
+ class Adsense < Base
+ register :client
+ register :slot
+ register :type, optional: true
+ register :width, optional: true
+ register :height, optional: true
+
+ AD_TYPES = {
+ button: { width: 125, height: 125 },
+ half_banner: { width: 234, height: 60 },
+ banner: { width: 468, height: 60 },
+ vertical_banner: { width: 120, height: 240 },
+ leaderboard: { width: 728, height: 90 },
+ skyscraper: { width: 468, height: 600 },
+ wide_skyscraper: { width: 160, height: 600 },
+ small_square: { width: 200, height: 200 },
+ square: { width: 250, height: 250 },
+ small_rectangle: { width: 180, height: 150 },
+ medium_rectangle: { width: 300, height: 250 },
+ large_rectangle: { width: 336, height: 280 }
+ }
+
+ def code
+ "<script type=\"text/javascript\">google_ad_client = '#{client}'; google_ad_slot = '#{slot}'; google_ad_width = #{properties[:width]}; google_ad_height = #{properties[:height]};</script><script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"></script>"
+ end
+
+ private
+
+ def properties
+ if width && height
+ { width: width, height: height }
+ elsif type
+ AD_TYPES[type] or raise ArgumentError, "Ad type '#{type}' is not known: Please use width and height parameters instead."
+ else
+ raise ArgumentError, 'Please set either type or height/width of the ad.'
+ end
+ end
+ end
+end
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+describe InBedWith::Adsense do
+ it 'prints a javascript tag based on argument' do
+ js = InBedWith.adsense(client: 'ca-pub-1234567890123456', slot: '1234567890', width: 728, height: 90)
+ js.should == "<script type=\"text/javascript\">google_ad_client = 'ca-pub-1234567890123456'; google_ad_slot = '1234567890'; google_ad_width = 728; google_ad_height = 90;</script><script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"></script>"
+ end
+
+ it 'prints a javascript tag based on configuration option' do
+ InBedWith.config do |config|
+ config.adsense_client = 'ca-pub-1234567890123456'
+ config.adsense_slot = '1234567890'
+ config.adsense_width = 728
+ config.adsense_height = 90
+ end
+
+ js = InBedWith.adsense
+ js.should == "<script type=\"text/javascript\">google_ad_client = 'ca-pub-1234567890123456'; google_ad_slot = '1234567890'; google_ad_width = 728; google_ad_height = 90;</script><script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"></script>"
+ end
+
+ context 'predefined ad types' do
+ before do
+ InBedWith.config do |config|
+ %w(adsense_client adsense_slot adsense_width adsense_height).each { |opt| config.send("#{opt}=", nil) }
+ end
+ end
+
+ it 'prints a javascript tag based on a predefined ad type' do
+ js = InBedWith.adsense(client: 'ca-pub-1234567890123456', slot: '1234567890', type: :leaderboard)
+ js.should == "<script type=\"text/javascript\">google_ad_client = 'ca-pub-1234567890123456'; google_ad_slot = '1234567890'; google_ad_width = 728; google_ad_height = 90;</script><script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"></script>"
+ end
+
+ it 'raises an exception if the ad type is not known' do
+ lambda { InBedWith.adsense(client: 'ca-pub-1234567890123456', slot: '1234567890', type: :foobarbaz) }.should raise_exception ArgumentError, /'foobarbaz' is not known/
+ end
+ end
+end

0 comments on commit e206a75

Please sign in to comment.