Permalink
Browse files

Add webmock based specs, refactoring, and update to version 0.0.2 tha…

…nks to mreinsch for some suggestions
  • Loading branch information...
deadprogram committed Dec 18, 2010
1 parent 9931696 commit 19328bb1e5d10c72a4bb610e6ca9ed2dd0100bca
Showing with 49 additions and 29 deletions.
  1. +2 −1 Gemfile
  2. +6 −0 Gemfile.lock
  3. +20 −24 lib/gabba/gabba.rb
  4. +1 −1 lib/gabba/version.rb
  5. +17 −3 spec/gabba_spec.rb
  6. +3 −0 spec/spec_helper.rb
View
@@ -3,4 +3,5 @@ source "http://rubygems.org"
# Specify your gem's dependencies in gabba.gemspec
gemspec
-gem 'minitest'
+gem 'minitest'
+gem 'webmock'
View
@@ -6,11 +6,17 @@ PATH
GEM
remote: http://rubygems.org/
specs:
+ addressable (2.2.2)
+ crack (0.1.8)
minitest (2.0.0)
+ webmock (1.6.1)
+ addressable (>= 2.2.2)
+ crack (>= 0.1.7)
PLATFORMS
ruby
DEPENDENCIES
gabba!
minitest
+ webmock
View
@@ -11,9 +11,8 @@ class NoGoogleAnalyticsDomainError < RuntimeError; end
class GoogleAnalyticsNetworkError < RuntimeError; end
class Gabba
- GOOGLE_URL = "http://www.google-analytics.com"
- TRACKING_URL = "/ga.js"
- BEACON_URL = "/__utm.gif"
+ GOOGLE_HOST = "www.google-analytics.com"
+ BEACON_PATH = "/__utm.gif"
USER_AGENT = "Gabba #{VERSION} Agent"
attr_accessor :utmwv, :utmn, :utmhn, :utmcs, :utmul, :utmdt, :utmp, :utmac, :utmt, :utmcc, :user_agent
@@ -36,9 +35,9 @@ def page_view(title, page, utmhid = rand(8999999999) + 1000000000)
hey(page_view_params(title, page, utmhid))
end
- def event(category, action, label = nil, value = nil)
+ def event(category, action, label = nil, value = nil, utmhid = rand(8999999999) + 1000000000)
check_account_params
- hey(event_params(category, action, label, value))
+ hey(event_params(category, action, label, value, utmhid))
end
def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
@@ -52,7 +51,7 @@ def page_view_params(title, page, utmhid = rand(8999999999) + 1000000000)
:utmhid => utmhid,
:utmp => page,
:utmac => @utmac,
- :utmcc => cookie_params
+ :utmcc => @utmcc || cookie_params
}
end
@@ -67,42 +66,39 @@ def event_params(category, action, label = nil, value = nil, utmhid = rand(89999
:utmul => @utmul,
:utmhid => utmhid,
:utmac => @utmac,
- :utmcc => cookie_params
+ :utmcc => @utmcc || cookie_params
}
end
-
+
def event_data(category, action, label = nil, value = nil)
- data = "5(#{category}*action" + (label ? "*#{label})" : ")")
+ data = "5(#{category}*action" + (label ? "*#{label})" : ")")
data += "(#{value})" if value
end
# create magical cookie params used by GA for its own nefarious purposes
def cookie_params(utma1 = rand(89999999) + 10000000, utma2 = rand(1147483647) + 1000000000, today = Time.now)
"__utma=1.#{utma1}00145214523.#{utma2}.#{today.to_i}.#{today.to_i}.15;+__utmz=1.#{today.to_i}.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);"
end
-
+
# sanity check that we have needed params to even call GA
def check_account_params
raise NoGoogleAnalyticsAccountError unless @utmac
raise NoGoogleAnalyticsDomainError unless @utmhn
end
# makes the tracking call to Google Analytics
- def hey(params, referer = "-")
- uri = URI.parse("#{GOOGLE_URL}#{BEACON_URL}?#{hash_to_querystring(params)}")
- req = Net::HTTP::Get.new(uri.path, {"User-Agent" => URI.escape(user_agent)})
- res = Net::HTTP.start(uri.host, uri.port) do |http|
- http.request(req)
+ def hey(params)
+ query = params.map {|k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
+ response = Net::HTTP.start(GOOGLE_HOST) do |http|
+ request = Net::HTTP::Get.new("#{BEACON_PATH}?#{query}")
+ request["User-Agent"] = URI.escape(user_agent)
+ request["Accept"] = "*/*"
+ http.request(request)
end
- raise GoogleAnalyticsNetworkError unless res.code == "200"
+
+ raise GoogleAnalyticsNetworkError unless response.code == "200"
+ response
end
-
- # convert params hash to query string
- def hash_to_querystring(hash = {})
- hash.keys.inject('') do |query_string, key|
- query_string << '&' unless key == hash.keys.first
- query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key].to_s)}"
- end
- end
end
+
end
View
@@ -1,3 +1,3 @@
module Gabba
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
View
@@ -5,6 +5,9 @@
describe "when tracking page views" do
before do
@gabba = Gabba::Gabba.new("abc", "123")
+ @gabba.utmn = "1009731272"
+ @gabba.utmcc = ''
+ stub_analytics @gabba.page_view_params("title", "/page/path", "6783939397")
end
it "must require GA account" do
@@ -19,14 +22,17 @@
@gabba.page_view_params("hiya", "/tracker/page")[:utmdt].must_equal("hiya")
end
- it "must be able to create hash of page_view_params" do
- @gabba.hash_to_querystring(@gabba.page_view_params("hiya", "/tracker/page")).wont_be_nil
+ it "must do page view request to google" do
+ @gabba.page_view("title", "/page/path", "6783939397").code.must_equal("200")
end
end
describe "when tracking custom events" do
before do
@gabba = Gabba::Gabba.new("abc", "123")
+ @gabba.utmn = "1009731272"
+ @gabba.utmcc = ''
+ stub_analytics @gabba.event_params("cat1", "act1", "lab1", "val1", "6783939397")
end
it "must require GA account" do
@@ -41,6 +47,14 @@
@gabba.event_data("cat1", "act1", "lab1", "val1").wont_be_nil
end
+ it "must do event request to google" do
+ @gabba.event("cat1", "act1", "lab1", "val1", "6783939397").code.must_equal("200")
+ end
+
+ end
+
+ def stub_analytics(expected_params)
+ s = stub_request(:get, /www.google-analytics.com\/__utm.gif\?utmac=#{expected_params[:utmac]}&.*/).
+ to_return(:status => 200, :body => "", :headers => {})
end
-
end
View
@@ -3,4 +3,7 @@
require 'minitest/autorun'
require 'minitest/pride'
+require 'webmock'
+include WebMock::API
+
require File.dirname(__FILE__) + '/../lib/gabba/gabba'

0 comments on commit 19328bb

Please sign in to comment.