Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

removed HappyMapper from report response

removed HappyMapper from profile
  • Loading branch information...
commit 2733d9370816e167961c587da64b994561db5cc9 1 parent 4f05798
Tony Pitale tpitale authored
12 lib/garb.rb
View
@@ -5,7 +5,7 @@
require 'cgi'
require 'ostruct'
-require 'happymapper'
+require 'crack'
require 'active_support'
require 'garb/version'
@@ -25,14 +25,18 @@
module Garb
GA = "http://schemas.google.com/analytics/2008"
-
- def self.to_google_analytics(thing)
+
+ extend self
+
+ def to_google_analytics(thing)
return thing.to_google_analytics if thing.respond_to?(:to_google_analytics)
"ga:#{thing.to_s.camelize(:lower)}"
end
+ alias :to_ga :to_google_analytics
- def self.from_google_analytics(thing)
+ def from_google_analytics(thing)
thing.to_s.gsub(/^ga\:/, '').underscore
end
+ alias :from_ga :from_google_analytics
end
44 lib/garb/profile.rb
View
@@ -4,54 +4,34 @@ class Profile
include ProfileReports
attr_reader :session, :table_id, :title, :account_name, :account_id, :web_property_id
-
- class Property
- include HappyMapper
-
- tag 'property'
- namespace 'http://schemas.google.com/analytics/2009'
-
- attribute :name, String
- attribute :value, String
-
- def instance_name
- Garb.from_google_analytics(name)
- end
- end
-
- class Entry
- include HappyMapper
-
- tag 'entry'
-
- element :title, String
- element :tableId, String, :namespace => 'http://schemas.google.com/analytics/2009'
-
- has_many :properties, Property
- end
def initialize(entry, session)
@session = session
- @title = entry.title
- @table_id = entry.tableId
+ @title = entry['title']
+ @table_id = entry['dxp:tableId']
- entry.properties.each do |p|
- instance_variable_set :"@#{p.instance_name}", p.value
+ entry['dxp:property'].each do |p|
+ instance_variable_set :"@#{Garb.from_ga(p['name'])}", p['value']
end
end
def id
- Garb.from_google_analytics(@table_id)
+ Garb.from_ga(@table_id)
end
def self.all(session = Session)
url = "https://www.google.com/analytics/feeds/accounts/default"
- response = DataRequest.new(session, url).send_request
- Entry.parse(response.body).map {|entry| new(entry, session)}
+ response = DataRequest.new(session, url).send_request
+ parse(response.body).map {|entry| new(entry, session)}
end
def self.first(id, session = Session)
all(session).detect {|profile| profile.id == id || profile.web_property_id == id }
end
+
+ def self.parse(response_body)
+ entry_hash = Crack::XML.parse(response_body)
+ entry_hash ? [entry_hash['feed']['entry']].flatten : []
+ end
end
end
61 lib/garb/report_response.rb
View
@@ -1,62 +1,33 @@
module Garb
class ReportResponse
- # include Enumerable
+ KEYS = ['dxp:metric', 'dxp:dimension']
def initialize(response_body)
@xml = response_body
end
-
- def parse
- entries = Entry.parse(@xml)
-
- @results = entries.collect do |entry|
- hash = {}
-
- entry.metrics.each do |m|
- name = m.name.sub(/^ga\:/,'').underscore
- hash.merge!({name => m.value})
- end
-
- entry.dimensions.each do |d|
- name = d.name.sub(/^ga\:/,'').underscore
- hash.merge!({name => d.value})
- end
-
- OpenStruct.new(hash)
- end
- end
def results
- @results || parse
+ @results ||= parse
end
-
- class Metric
- include HappyMapper
- tag 'metric'
- namespace 'http://schemas.google.com/analytics/2009'
+ private
+ def parse
+ entries.map do |entry|
+ hash = values_for(entry).inject({}) do |h, v|
+ h.merge(Garb.from_ga(v['name']) => v['value'])
+ end
- attribute :name, String
- attribute :value, String
+ OpenStruct.new(hash)
+ end
end
-
- class Dimension
- include HappyMapper
-
- tag 'dimension'
- namespace 'http://schemas.google.com/analytics/2009'
- attribute :name, String
- attribute :value, String
+ def entries
+ entry_hash = Crack::XML.parse(@xml)
+ entry_hash ? [entry_hash['feed']['entry']].flatten : []
end
-
- class Entry
- include HappyMapper
-
- tag 'entry'
- has_many :metrics, Metric
- has_many :dimensions, Dimension
+ def values_for(entry)
+ KEYS.map {|k| entry[k]}.flatten.compact
end
end
-end
+end
62 test/fixtures/profile_feed.xml
View
@@ -1,40 +1,38 @@
-<?xml version='1.0' encoding='UTF-8'?>
-<feed xmlns='http://www.w3.org/2005/Atom' xmlns:dxp='http://schemas.google.com/analytics/2009' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'>
- <id>http://www.google.com/analytics/feeds/accounts/tpitale@gmail.com</id>
- <updated>2009-10-02T07:47:35.000-07:00</updated>
- <title type='text'>Profile list for tpitale@gmail.com</title><link rel='self' type='application/atom+xml' href='http://www.google.com/analytics/feeds/accounts/default'/>
- <author>
- <name>Google Analytics</name>
- </author>
- <generator version='1.0'>Google Analytics</generator>
- <openSearch:totalResults>2</openSearch:totalResults>
- <openSearch:startIndex>1</openSearch:startIndex>
- <openSearch:itemsPerPage>2</openSearch:itemsPerPage>
+<?xml version="1.0" encoding="UTF-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dxp="http://schemas.google.com/analytics/2009" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">
+ <id>http://www.google.com/analytics/feeds/accounts/tpitale@gmail.com</id>
+ <updated>2009-10-02T07:47:35.000-07:00</updated>
+ <title type="text">Profile list for tpitale@gmail.com</title>
+ <link rel="self" type="application/atom+xml" href="http://www.google.com/analytics/feeds/accounts/default"/>
+ <author><name>Google Analytics</name></author>
+ <generator version="1.0">Google Analytics</generator>
+ <openSearch:totalResults>2</openSearch:totalResults>
+ <openSearch:startIndex>1</openSearch:startIndex>
+ <openSearch:itemsPerPage>2</openSearch:itemsPerPage>
<entry>
<id>http://www.google.com/analytics/feeds/accounts/ga:12345</id>
<updated>2008-07-21T14:05:57.000-07:00</updated>
- <title type='text'>Historical</title>
- <link rel='alternate' type='text/html' href='http://www.google.com/analytics'/>
- <dxp:property name='ga:accountId' value='1111'/>
- <dxp:property name='ga:accountName' value='Blog Beta'/>
- <dxp:property name='ga:profileId' value='1212'/>
- <dxp:property name='ga:webPropertyId' value='UA-1111-1'/>
- <dxp:property name='ga:currency' value='USD'/>
- <dxp:property name='ga:timezone' value='America/New_York'/>
- <dxp:tableId>ga:12345</dxp:tableId>
+ <title type="text">Historical</title>
+ <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
+ <dxp:property name="ga:accountId" value="1111"/>
+ <dxp:property name="ga:accountName" value="Blog Beta"/>
+ <dxp:property name="ga:profileId" value="1212"/>
+ <dxp:property name="ga:webPropertyId" value="UA-1111-1"/>
+ <dxp:property name="ga:currency" value="USD"/>
+ <dxp:property name="ga:timezone" value="America/New_York"/>
+ <dxp:tableId>ga:12345</dxp:tableId>
</entry>
<entry>
<id>http://www.google.com/analytics/feeds/accounts/ga:12346</id>
<updated>2008-11-24T11:51:07.000-08:00</updated>
- <title type='text'>Presently</title>
- <link rel='alternate' type='text/html' href='http://www.google.com/analytics'/>
- <dxp:property name='ga:accountId' value='1111'/>
- <dxp:property name='ga:accountName' value='Blog Beta'/>
- <dxp:property name='ga:profileId' value='1213'/>
- <dxp:property name='ga:webPropertyId' value='UA-1111-2'/>
- <dxp:property name='ga:currency' value='USD'/>
- <dxp:property name='ga:timezone' value='America/New_York'/>
- <dxp:tableId>ga:12346</dxp:tableId>
+ <title type="text">Presently</title>
+ <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
+ <dxp:property name="ga:accountId" value="1111"/>
+ <dxp:property name="ga:accountName" value="Blog Beta"/>
+ <dxp:property name="ga:profileId" value="1213"/>
+ <dxp:property name="ga:webPropertyId" value="UA-1111-2"/>
+ <dxp:property name="ga:currency" value="USD"/>
+ <dxp:property name="ga:timezone" value="America/New_York"/>
+ <dxp:tableId>ga:12346</dxp:tableId>
</entry>
-</feed>
-
+</feed>
8 test/fixtures/report_feed.xml
View
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
-<feed xmlns='http://www.w3.org/2005/Atom'
- xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
- xmlns:dxp='http://schemas.google.com/analytics/2009'
- xmlns:ga='http://schemas.google.com/analytics/2008'>
+<feed xmlns="http://www.w3.org/2005/Atom"
+ xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"
+ xmlns:dxp="http://schemas.google.com/analytics/2009"
+ xmlns:ga="http://schemas.google.com/analytics/2008">
<id>http://www.google.com/analytics/feeds/data?ids=ga:983247&amp;dimensions=ga:country,ga:city&amp;metrics=ga:pageViews&amp;start-date=2008-01-01&amp;end-date=2008-01-02</id>
<updated>2008-01-02T15:59:59.999-08:00 </updated>
<title type="text">Google Analytics Data for Profile 983247</title>
58 test/unit/garb/profile_test.rb
View
@@ -5,29 +5,19 @@ class ProfileTest < MiniTest::Unit::TestCase
context "The Profile class" do
setup {@session = Session.new}
-
+
should "be able to return a list of all profiles" do
url = 'https://www.google.com/analytics/feeds/accounts/default'
-
+
xml = read_fixture('profile_feed.xml')
-
- data_request = mock
- data_request.expects(:send_request).with().returns(stub(:body => xml))
-
- DataRequest.expects(:new).with(@session, url).returns(data_request)
-
- entries = [stub]
-
- Profile::Entry.expects(:parse).with(xml).returns(entries)
-
- profiles = []
- entries.each do |entry|
- profile = stub
- profiles << profile
- Garb::Profile.expects(:new).with(entry, @session).returns(profile)
- end
-
- assert_equal profiles, Profile.all(@session)
+
+ data_request = stub
+ data_request.stubs(:send_request).returns(stub(:body => xml))
+ DataRequest.stubs(:new).returns(data_request)
+
+ assert_equal ['12345', '12346'], Profile.all(@session).map(&:id)
+ assert_received(DataRequest, :new) {|e| e.with(@session, url)}
+ assert_received(data_request, :send_request)
end
should "return the first profile for a given web property id" do
@@ -35,11 +25,11 @@ class ProfileTest < MiniTest::Unit::TestCase
profile2 = stub(:web_property_id => '67890', :id => 'ghijkl')
entries = [profile1, profile2]
- Garb::Profile.stubs(:all).returns(entries)
+ Profile.stubs(:all).returns(entries)
- assert_equal profile1, Garb::Profile.first('12345', @session)
+ assert_equal profile1, Profile.first('12345', @session)
- assert_received(Garb::Profile, :all) {|e| e.with(@session)}
+ assert_received(Profile, :all) {|e| e.with(@session)}
end
should "return the first profile for a given table id" do
@@ -47,41 +37,39 @@ class ProfileTest < MiniTest::Unit::TestCase
profile2 = stub(:id => '67890', :web_property_id => 'ghijkl')
entries = [profile1, profile2]
- Garb::Profile.stubs(:all).returns(entries)
+ Profile.stubs(:all).returns(entries)
- assert_equal profile2, Garb::Profile.first('67890', @session)
+ assert_equal profile2, Profile.first('67890', @session)
- assert_received(Garb::Profile, :all) {|e| e.with(@session)}
+ assert_received(Profile, :all) {|e| e.with(@session)}
end
end
context "An instance of the Profile class" do
-
setup do
- @entry = (Profile::Entry.parse(read_fixture('profile_feed.xml'))).first
- @profile = Profile.new(@entry, Session)
+ entry = Profile.parse(read_fixture('profile_feed.xml')).first
+ @profile = Profile.new(entry, Session)
end
should "have a value for :title" do
assert_equal "Historical", @profile.title
end
-
+
should "have a value for :table_id" do
assert_equal 'ga:12345', @profile.table_id
end
-
+
should "have a value for :id" do
assert_equal '12345', @profile.id
end
-
+
should "have a value for :account_id" do
assert_equal '1111', @profile.account_id
end
-
+
should "have a value for :account_name" do
assert_equal 'Blog Beta', @profile.account_name
end
end
-
end
-end
+end
25 test/unit/garb/report_response_test.rb
View
@@ -2,27 +2,12 @@
module Garb
class ReportResponseTest < MiniTest::Unit::TestCase
- context "An instance of the ReportResponse class" do
- setup do
- @xml = File.read(File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml"))
- @response = ReportResponse.new(@xml)
- end
-
- should "parse xml response with happymapper" do
- h1 = {"city"=>"(not set)", "pageviews"=>"33", "country"=>"(not set)"}
- h2 = {"city"=>"Kabul", "pageviews"=>"2", "country"=>"Afghanistan"}
- h3 = {"city"=>"Tirana", "pageviews"=>"1", "country"=>"Albania"}
-
- OpenStruct.expects(:new).with(h1).returns('entry1')
- OpenStruct.expects(:new).with(h2).returns('entry2')
- OpenStruct.expects(:new).with(h3).returns('entry3')
+ context "A ReportResponse" do
+ should "parse results from atom xml" do
+ filename = File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml")
+ response = ReportResponse.new(File.read(filename))
- assert_equal(['entry1', 'entry2', 'entry3'], @response.parse)
- end
-
- should "have results or parse them" do
- @response.expects(:parse)
- @response.results
+ assert_equal ['33', '2', '1'], response.results.map(&:pageviews)
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.