Permalink
Browse files

add total_results and totals_for_all_results methods in Query and Res…

…ponse
  • Loading branch information...
Patrick Roby
Patrick Roby committed Mar 29, 2012
1 parent f83388b commit c70a3781b2f50a23ce7cbad66db6d5892510d67b
Showing with 53 additions and 5 deletions.
  1. +14 −1 lib/legato/query.rb
  2. +13 −0 lib/legato/response.rb
  3. +16 −2 spec/lib/legato/query_spec.rb
  4. +10 −2 spec/lib/legato/response_spec.rb
View
@@ -108,7 +108,10 @@ def loaded?
end
def load
- @collection = request_for_query.collection
+ response = request_for_query
+ @collection = response.collection
+ @total_results = response.total_results
+ @totals_for_all_results = response.totals_for_all_results
@loaded = true
end
@@ -118,6 +121,16 @@ def collection
end
alias :to_a :collection
+ def total_results
+ load unless loaded?
+ @total_results
+ end
+
+ def totals_for_all_results
+ load unless loaded?
+ @totals_for_all_results
+ end
+
def each(&block)
collection.each(&block)
end
View
@@ -13,6 +13,14 @@ def collection
raw_attributes.map {|attributes| @instance_klass.new(attributes)}
end
+ def total_results
+ data["totalResults"]
+ end
+
+ def totals_for_all_results
+ Hash[data["totalsForAllResults"].map{|k,v| [Legato.from_ga_string(k), number_for(v)]}]
+ end
+
private
def headers
data['columnHeaders']
@@ -29,5 +37,10 @@ def rows
def raw_attributes
rows.map {|row| Hash[fields.zip(row)]}
end
+
+ def number_for(str)
+ return str.to_f if str.index('.')
+ str.to_i
+ end
end
end
@@ -47,7 +47,7 @@ def self.it_defines_operators(*operators)
end
it "loads a collection of results" do
- response = stub(:collection => [])
+ response = stub(:collection => [], :total_results => 0, :totals_for_all_results => {})
user = stub(:request => response)
@query.stubs(:profile => stub(:user => user))
@@ -56,15 +56,29 @@ def self.it_defines_operators(*operators)
@query.loaded?.should be_true
@query.profile.user.should have_received(:request).with(@query)
response.should have_received(:collection)
+ response.should have_received(:total_results)
+ response.should have_received(:totals_for_all_results)
end
it "returns the collection" do
- @query.stubs(:request_for_query).returns(stub(:collection => [1,2,3]))
+ @query.stubs(:request_for_query).returns(stub(:collection => [1,2,3], :total_results => 3, :totals_for_all_results => {'foo' => 34.2}))
@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.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.load
+ @query.totals_for_all_results.should == {'foo' => 34.2}
+ end
+
it "behaves like an enumerable delegating to the collection" do
collection = []
collection.stubs(:each)
@@ -12,9 +12,17 @@
@response.collection.first.should == OpenStruct.new({:browser=>"Android Browser", :pageviews=>"93"})
end
+ it 'has the number of total results' do
+ @response.total_results.should == 13
+ end
+
+ it 'has the totals for all results hash' do
+ @response.totals_for_all_results.should == {'pageviews' => 3710}
+ end
+
it 'handles no rows returned' do
- @response.stubs(:data).returns({'rows' => nil})
- @response.collection.should == []
+ @response.stubs(:data).returns({'rows' => nil})
+ @response.collection.should == []
end
end
end

0 comments on commit c70a378

Please sign in to comment.