Sha256: 450c75caf1d9cdd57a2e3a8eebc5bef453429997c395e5b8dab99c26054b0e1d

Contents?: true

Size: 1.6 KB

Versions: 4

Compression:

Stored size: 1.6 KB

Contents

# = Google Adsense Helper
module AdsenseHelper
  # Parameters:
  # 
  # client :: 16 digits
  # slot :: 10 digits
  # dimension :: "#{width}x#{height}", default is 336x280
  #
  # Example:
  # 
  #   adsense_tag(client: 0000000000000000, slot: 1111111111, dimension: '336x280')
  # 
  # Generated code:
  #
  #   <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  #   <ins class="adsbygoogle"
  #        style="display:inline-block;width:336px;height:280px"
  #        data-ad-client="ca-pub-0000000000000000"
  #        data-ad-slot="1111111111"></ins>
  #   <script>
  #   (adsbygoogle = window.adsbygoogle || []).push({});
  #   </script>
  #
  def adsense_tag(options = {})
    defaults = { dimension: '336x280' }
    options = options.to_h.reverse_merge(defaults).with_indifferent_access
    width, height = options[:dimension].split('x').to_a
    
    raise 'Invalid ad client, should be 16 digits' unless options[:client].to_s[/^\d{16}$/]
    raise 'Invalid ad slot, should be 10 digits' unless options[:slot].to_s[/^\d{10}$/]
    raise 'Invalid ad dimension, should be "#{width}x#{height}"' unless width.to_s[/^\d+$/] && height.to_s[/^\d+$/]
    
    tags = [
      content_tag('script', '', async: '', src: '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'),
      content_tag('ins', '', class: 'adsbygoogle', style: "display:inline-block;width:#{width}px;height:#{height}px", 'data-ad-client' => "ca-pub-#{options[:client]}", 'data-ad-slot' => options[:slot]),
      content_tag('script', '(adsbygoogle = window.adsbygoogle || []).push({});'),
    ]
    safe_join(tags)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
adsense_helper-0.1.0 lib/adsense_helper/adsense_helper.rb
adsense_helper-0.0.4 lib/adsense_helper/adsense_helper.rb
adsense_helper-0.0.3 lib/adsense_helper/adsense_helper.rb
adsense_helper-0.0.2 lib/adsense_helper/adsense_helper.rb