Permalink
Browse files

Add in_bed_with.gauges support

  • Loading branch information...
1 parent 0e055c2 commit 8b7c22591afcdab310d78b003b2617c79f86dd44 Sebastian von Conrad committed Jan 9, 2012
Showing with 36 additions and 0 deletions.
  1. +2 −0 lib/in_bed_with.rb
  2. +20 −0 lib/in_bed_with/gauges.rb
  3. +14 −0 spec/in_bed_with/gauges_spec.rb
View
@@ -5,11 +5,13 @@
require 'active_support/inflector'
require 'in_bed_with/analytics'
+require 'in_bed_with/gauges'
require 'in_bed_with/railtie' if defined?(Rails)
module InBedWith
mattr_accessor :analytics_property_id
+ mattr_accessor :gauges_site_id
def self.config(&block)
yield self
View
@@ -0,0 +1,20 @@
+module InBedWith
+ class Gauges
+ attr_accessor :site_id
+
+ def initialize(options={})
+ self.site_id = options[:site_id] || InBedWith.gauges_site_id
+ end
+
+ def code
+ raise ArgumentError, site_id_missing unless site_id
+
+ "<script type=\"text/javascript\">var _gauges = _gauges || []; (function() { var t = document.createElement('script'); t.type = 'text/javascript'; t.async = true; t.id = 'gauges-tracker'; t.setAttribute('data-site-id', '#{site_id}'); t.src = '//secure.gaug.es/track.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(t, s); })();</script>"
+ end
+
+ private
+ def site_id_missing
+ 'Please set a Gauges Site ID through config.gauges_site_id or by supplying :site_id when calling in_bed_with.gauges.'
+ end
+ end
+end
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe InBedWith::Gauges do
+ it 'prints a javascript tag based on argument' do
+ js = InBedWith.gauges(:site_id => '1234567890abcdef12345678')
+ js.should == "<script type=\"text/javascript\">var _gauges = _gauges || []; (function() { var t = document.createElement('script'); t.type = 'text/javascript'; t.async = true; t.id = 'gauges-tracker'; t.setAttribute('data-site-id', '1234567890abcdef12345678'); t.src = '//secure.gaug.es/track.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(t, s); })();</script>"
+ end
+
+ it 'prints a javascript tag based on configuration option' do
+ InBedWith.config { |config| config.gauges_site_id = '87654321fedcba0987654321' }
+ js = InBedWith.gauges
+ js.should == "<script type=\"text/javascript\">var _gauges = _gauges || []; (function() { var t = document.createElement('script'); t.type = 'text/javascript'; t.async = true; t.id = 'gauges-tracker'; t.setAttribute('data-site-id', '87654321fedcba0987654321'); t.src = '//secure.gaug.es/track.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(t, s); })();</script>"
+ end
+end

0 comments on commit 8b7c225

Please sign in to comment.