Skip to content

Commit

Permalink
Merge pull request #126 from wrocla/master
Browse files Browse the repository at this point in the history
Add sampled flag for response data of google analytics api
  • Loading branch information
tpitale committed Aug 10, 2016
2 parents 9716585 + 827a59b commit c925834
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/legato/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Query
include Enumerable

MONTH = 2592000
REQUEST_FIELDS = 'columnHeaders/name,rows,totalResults,totalsForAllResults'
REQUEST_FIELDS = 'columnHeaders/name,rows,totalResults,totalsForAllResults,containsSampledData'

BASIC_OPTION_KEYS = [
:sort, :limit, :offset, :start_date, :end_date, :quota_user,
Expand Down Expand Up @@ -160,6 +160,7 @@ def load
@collection = response.collection
@total_results = response.total_results
@totals_for_all_results = response.totals_for_all_results
@sampled = response.sampled
@loaded = true
end

Expand All @@ -179,6 +180,11 @@ def totals_for_all_results
@totals_for_all_results
end

def sampled
load unless loaded?
@sampled
end

def each(&block)
collection.each(&block)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/legato/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def totals_for_all_results
Hash[data["totalsForAllResults"].map{|k,v| [Legato.from_ga_string(k), number_for(v)]}]
end

def sampled
data["containsSampledData"]
end

def rows
Array.wrap(data['rows']).compact
end
Expand Down
9 changes: 5 additions & 4 deletions spec/lib/legato/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def self.it_defines_operators(*operators)
end

it "loads a collection of results" do
response = stub(:collection => [], :total_results => 0, :totals_for_all_results => {})
response = stub(:collection => [], :total_results => 0, :totals_for_all_results => {}, :sampled => false)
user = stub(:request => response)
query.stubs(:profile => stub(:user => user))

Expand All @@ -71,23 +71,24 @@ def self.it_defines_operators(*operators)
response.should have_received(:collection)
response.should have_received(:total_results)
response.should have_received(:totals_for_all_results)
response.should have_received(:sampled)
end

it "returns the collection" do
query.stubs(:request_for_query).returns(stub(:collection => [1,2,3], :total_results => 3, :totals_for_all_results => {'foo' => 34.2}))
query.stubs(:request_for_query).returns(stub(:collection => [1,2,3], :total_results => 3, :totals_for_all_results => {'foo' => 34.2}, :sampled => false))
query.load
query.collection.should == [1,2,3]
query.to_a.should == [1,2,3]
end

it "returns the total number of results" do
query.stubs(:request_for_query).returns(stub(:collection => [1,2,3], :total_results => 3, :totals_for_all_results => {'foo' => 34.2}))
query.stubs(:request_for_query).returns(stub(:collection => [1,2,3], :total_results => 3, :totals_for_all_results => {'foo' => 34.2}, :sampled => false))
query.load
query.total_results.should == 3
end

it "returns the totals for all results" do
query.stubs(:request_for_query).returns(stub(:collection => [1,2,3], :total_results => 3, :totals_for_all_results => {'foo' => 34.2}))
query.stubs(:request_for_query).returns(stub(:collection => [1,2,3], :total_results => 3, :totals_for_all_results => {'foo' => 34.2}, :sampled => false))
query.load
query.totals_for_all_results.should == {'foo' => 34.2}
end
Expand Down

0 comments on commit c925834

Please sign in to comment.