Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an enumerator over all results. #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 0.9.x

* Added Garb::Model.all that paginates all data

Version 0.9.2

* Removed all deprecated features: Garb::Report, Garb::Resource, Garb::Profile, and Garb::Account
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ Get the Results

Be forewarned, these numbers are for the last **30** days and may be slightly different from the numbers displayed in Google Analytics' dashboard for **1 month**.

Get all Results
---------------

Google Analytics returns 1000 results at a time. To paginate over all results:

> Exits.all(profile) { |exit| ... }

Other Parameters
----------------

Expand Down
16 changes: 15 additions & 1 deletion lib/garb/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def results(profile, options = {})
data = send_request_for_data(profile, build_params(param_set))
ReportResponse.new(data, instance_klass).results
end

def all(profile, options = {}, &block)
limit = options.delete(:limit)
total = 0
while ((rs = results(profile, options)) && rs.any?)
rs.each do |r|
yield r
total += 1
return self if limit and total >= limit
end
options[:offset] = total
end
self
end

private
def send_request_for_data(profile, params)
Expand Down Expand Up @@ -86,4 +100,4 @@ def format_time(t)
t.strftime('%Y-%m-%d')
end
end
end
end
24 changes: 23 additions & 1 deletion test/unit/garb/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ModelTest < MiniTest::Unit::TestCase
assert_equal ['result'], @test_model.results(@profile, :limit => 20)
assert_data_params(@params.merge({'max-results' => 20}))
end

should "be able to offset" do
assert_equal ['result'], @test_model.results(@profile, :offset => 10)
assert_data_params(@params.merge({'start-index' => 10}))
Expand All @@ -128,6 +128,28 @@ class ModelTest < MiniTest::Unit::TestCase
assert_equal ['result'], @test_model.results(@profile)
assert_received(ReportResponse, :new) {|e| e.with("raw report data", ResultKlass)}
end

should "be able to fetch multiple pages of results" do
results = []
count = 0
@test_model.all(@profile) do |result|
results << result
# stub a subsequent response
count += 1
ReportResponse.stubs(:new).returns(stub(:results => [])) if count == 2
end
assert_equal ['result', 'result'], results
end

should "be able to fetch multiple pages of results with a limit" do
results = []
@test_model.all(@profile, :limit => 1) do |result|
results << result
end
assert_equal ['result'], results
end


end

# should "return results as an array of the class it belongs to, if that class is an ActiveRecord descendant"
Expand Down