Permalink
Browse files

detect if results are sampled

  • Loading branch information...
1 parent 48173c2 commit 20ec4e45f1f851c17b623f83edfed5cba62ba2cf @tpitale tpitale committed Jan 9, 2011
@@ -11,6 +11,7 @@ def results
if @results.nil?
@results = ResultSet.new(parse)
@results.total_results = parse_total_results
+ @results.sampled = parse_sampled_flag
end
@results
@@ -36,6 +37,10 @@ def parse_total_results
feed? ? parsed_xml['feed']['openSearch:totalResults'].to_i : 0
end
+ def parse_sampled_flag
+ feed? ? (parsed_xml['feed']['dxp:containsSampledData'] == 'true') : false
+ end
+
def parsed_xml
@parsed_xml ||= Crack::XML.parse(@xml)
end
@@ -2,7 +2,9 @@ module Garb
class ResultSet
include Enumerable
- attr_accessor :total_results
+ attr_accessor :total_results, :sampled
+
+ alias :sampled? :sampled
def initialize(results)
@results = results
@@ -15,6 +15,7 @@
<openSearch:startIndex>3</openSearch:startIndex>
<openSearch:itemsPerPage>4</openSearch:itemsPerPage>
<openSearch:totalResults>18</openSearch:totalResults>
+ <dxp:containsSampledData>true</dxp:containsSampledData>
<ga:webPropertyID>UA-983247-67</ga:webPropertyID>
<ga:start-date>2008-01-01</ga:start-date>
<ga:end-date>2008-01-02</ga:end-date>
@@ -7,30 +7,33 @@ class ReportResponseTest < MiniTest::Unit::TestCase
context "A ReportResponse" do
context "with a report feed" do
setup do
- @file = File.read(File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml"))
+ @xml = File.read(File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml"))
end
should "parse results from atom xml" do
- response = ReportResponse.new(@file)
+ response = ReportResponse.new(@xml)
assert_equal ['33', '2', '1'], response.results.map(&:pageviews)
end
should "default to returning an array of OpenStruct objects" do
- response = ReportResponse.new(@file)
+ response = ReportResponse.new(@xml)
assert_equal [OpenStruct, OpenStruct, OpenStruct], response.results.map(&:class)
end
should "return an array of instances of a specified class" do
- response = ReportResponse.new(@file, SpecialKlass)
+ response = ReportResponse.new(@xml, SpecialKlass)
assert_equal [SpecialKlass, SpecialKlass, SpecialKlass], response.results.map(&:class)
end
should "know the total number of results" do
- response = ReportResponse.new(@file)
+ response = ReportResponse.new(@xml)
assert_equal 18, response.results.total_results
end
- should "know if the data has been sampled"
+ should "know if the data has been sampled" do
+ response = ReportResponse.new(@xml)
+ assert_equal true, response.results.sampled?
+ end
end
should "return an empty array if there are no results" do

0 comments on commit 20ec4e4

Please sign in to comment.