Browse files

update ReportResponse to handle new v3 JSON response

  • Loading branch information...
1 parent c02745f commit ef3fc65015b5b71d628ac7f37a6889fba33e268a @cgunther cgunther committed Jan 24, 2012
Showing with 95 additions and 22 deletions.
  1. +14 −15 lib/garb/report_response.rb
  2. +75 −1 test/fixtures/report_feed.json
  3. +6 −6 test/unit/garb/report_response_test.rb
View
29 lib/garb/report_response.rb
@@ -1,6 +1,5 @@
module Garb
class ReportResponse
- KEYS = ['dxp$metric', 'dxp$dimension']
def initialize(response_body, instance_klass = OpenStruct)
@data = response_body
@@ -22,35 +21,35 @@ def sampled?
private
def parse
- entries.map do |entry|
+ rows.map do |row|
@instance_klass.new(Hash[
- values_for(entry).map {|v| [Garb.from_ga(v['name']), v['value']]}
+ *keys.zip(row).flatten
])
end
end
- def entries
- feed? ? [parsed_data['feed']['entry']].flatten.compact : []
+ def column_headers
+ parsed_data['columnHeaders']
end
- def parse_total_results
- feed? ? parsed_data['feed']['openSearch:totalResults'].to_i : 0
+ def keys
+ column_headers.map { |header| Garb.from_ga(header['name']) }
end
- def parse_sampled_flag
- feed? ? (parsed_data['feed']['dxp$containsSampledData'] == 'true') : false
+ def rows
+ parsed_data['rows']
end
- def parsed_data
- @parsed_data ||= JSON.parse(@data)
+ def parse_total_results
+ parsed_data['totalResults'].to_i
end
- def feed?
- !parsed_data['feed'].nil?
+ def parse_sampled_flag
+ parsed_data['containsSampledData']
end
- def values_for(entry)
- KEYS.map {|k| entry[k]}.flatten.compact
+ def parsed_data
+ @parsed_data ||= JSON.parse(@data)
end
end
end
View
76 test/fixtures/report_feed.json
@@ -1 +1,75 @@
-{"feed":{"id":"http://www.google.com/analytics/feeds/data?ids=ga:123456&dimensions=ga:country,ga:city&metrics=ga:pageViews&start-date=2008-01-01&end-date=2008-01-02","updated":"2008-01-02T15:59:59.999-08:00 ","title":"Google Analytics Data for Profile 123456","link":[{"href":"http://www.google.com/analytics/feeds/data","rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml"},{"href":"http://www.google.com/analytics/feeds/data?end-date=2008-01-02&start-date=2008-01-01&metrics=ga%3ApageViews&ids=ga%3A983247&dimensions=ga%3Acountry%2Cga%3Acity","rel":"self","type":"application/atom+xml"},{"href":"http://www.google.com/analytics/feeds/data?start-index=1001&max-results=1000&end-date=2008-01-02&start-date=2008-01-01&metrics=ga%3ApageViews&ids=ga%3A983247&dimensions=ga%3Acountry%2Cga%3Acity","rel":"next","type":"application/atom+xml"}],"author":{"name":"Google Analytics"},"openSearch:startIndex":"3","openSearch:itemsPerPage":"4","openSearch:totalResults":"18","dxp$containsSampledData":"true","ga:webPropertyID":"UA-123456-78","ga:start_date":"2008-01-01","ga:end_date":"2008-01-02","entry":[{"id":" http://www.google.com/analytics/feeds/data?ids=ga:1174&ga:country=%28not%20set%29&ga:city=%28not%20set%29&start-date=2008-01-01&end-date=2008-01-02 ","updated":" 2008-01-01T16:00:00.001-08:00 ","title":" ga:country=(not set) | ga:city=(not set) ","link":{"href":"http://www.google.com/analytics/feeds/data","rel":"self","type":"application/atom+xml"},"dxp$dimension":[{"name":"ga:country","value":"(not set)"},{"name":"ga:city","value":"(not set)"}],"dxp$metric":{"name":"ga:pageviews","value":"33"}},{"id":" http://www.google.com/analytics/feeds/data?ids=ga:1174&ga:country=Afghanistan&ga:city=Kabul&start-date=2008-01-01&end-date=2008-01-02 ","updated":" 2008-01-01T16:00:00.001-08:00 ","title":" ga:country=Afghanistan | ga:city=Kabul ","dxp$dimension":[{"name":"ga:country","value":"Afghanistan"},{"name":"ga:city","value":"Kabul"}],"dxp$metric":{"name":"ga:pageviews","value":"2"}},{"id":" http://www.google.com/analytics/feeds/data?ids=ga:1174&ga:country=Albania&ga:city=Tirana&start-date=2008-01-01&end-date=2008-01-02 ","updated":" 2008-01-01T16:00:00.001-08:00 ","title":" ga:country=Albania | ga:city=Tirana ","dxp$dimension":[{"name":"ga:country","value":"Albania"},{"name":"ga:city","value":"Tirana"}],"dxp$metric":{"name":"ga:pageviews","value":"1"}}],"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"}}
+{
+ "kind":"analytics#gaData",
+ "id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:123456&dimensions=ga:country,ga:city&metrics=ga:pageViews&start-date=2011-12-01&end-date=2011-12-31&start-index=1&max-results=5",
+ "query":{
+ "start-date":"2011-12-01",
+ "end-date":"2011-12-31",
+ "ids":"ga:123456",
+ "dimensions":"ga:country,ga:city",
+ "metrics":[
+ "ga:pageViews"
+ ],
+ "start-index":1,
+ "max-results":5
+ },
+ "itemsPerPage":5,
+ "totalResults":1261,
+ "selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:123456&dimensions=ga:country,ga:city&metrics=ga:pageViews&start-date=2011-12-01&end-date=2011-12-31&start-index=1&max-results=5",
+ "nextLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:123456&dimensions=ga:country,ga:city&metrics=ga:pageViews&start-date=2011-12-01&end-date=2011-12-31&start-index=6&max-results=5",
+ "profileInfo":{
+ "profileId":"123456",
+ "accountId":"123456",
+ "webPropertyId":"UA-123456-1",
+ "internalWebPropertyId":"123456",
+ "profileName":"example.com",
+ "tableId":"ga:123456"
+ },
+ "containsSampledData":true,
+ "columnHeaders":[
+ {
+ "name":"ga:country",
+ "columnType":"DIMENSION",
+ "dataType":"STRING"
+ },
+ {
+ "name":"ga:city",
+ "columnType":"DIMENSION",
+ "dataType":"STRING"
+ },
+ {
+ "name":"ga:pageviews",
+ "columnType":"METRIC",
+ "dataType":"INTEGER"
+ }
+ ],
+ "totalsForAllResults":{
+ "ga:pageviews":"38817"
+ },
+ "rows":[
+ [
+ "(not set)",
+ "(not set)",
+ "4"
+ ],
+ [
+ "Australia",
+ "Adelaide",
+ "4"
+ ],
+ [
+ "Australia",
+ "Brisbane",
+ "17"
+ ],
+ [
+ "Australia",
+ "Geelong",
+ "1"
+ ],
+ [
+ "Australia",
+ "Melbourne",
+ "5"
+ ]
+ ]
+}
View
12 test/unit/garb/report_response_test.rb
@@ -10,24 +10,24 @@ class ReportResponseTest < MiniTest::Unit::TestCase
@json = File.read(File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.json"))
end
- should "parse results from atom xml" do
+ should "parse results from json" do
response = ReportResponse.new(@json)
- assert_equal ['33', '2', '1'], response.results.map(&:pageviews)
+ assert_equal ['4', '4', '17', '1', '5'], response.results.map(&:pageviews)
end
should "default to returning an array of OpenStruct objects" do
response = ReportResponse.new(@json)
- assert_equal [OpenStruct, OpenStruct, OpenStruct], response.results.map(&:class)
+ assert_equal [OpenStruct, OpenStruct, OpenStruct, OpenStruct, OpenStruct], response.results.map(&:class)
end
should "return an array of instances of a specified class" do
response = ReportResponse.new(@json, SpecialKlass)
- assert_equal [SpecialKlass, SpecialKlass, SpecialKlass], response.results.map(&:class)
+ assert_equal [SpecialKlass, SpecialKlass, SpecialKlass, SpecialKlass, SpecialKlass], response.results.map(&:class)
end
should "know the total number of results" do
response = ReportResponse.new(@json)
- assert_equal 18, response.results.total_results
+ assert_equal 1261, response.results.total_results
end
should "know if the data has been sampled" do
@@ -38,7 +38,7 @@ class ReportResponseTest < MiniTest::Unit::TestCase
should "return an empty array if there are no results" do
response = ReportResponse.new("result json")
- JSON.stubs(:parse).with("result json").returns({'feed' => {'entry' => nil}})
+ JSON.stubs(:parse).with("result json").returns({'rows' => []})
assert_equal [], response.results.to_a
end

0 comments on commit ef3fc65

Please sign in to comment.