Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first step before I start to add result set features to expose total …

…results
  • Loading branch information...
commit 686548ff7620128b4660775ed9e563e2c8b66efa 1 parent e4c9ad9
@tpitale tpitale authored
View
10 lib/garb/model.rb
@@ -26,7 +26,9 @@ def instance_klass
end
def results(profile, options = {})
- default_params = build_default_params(profile)
+ start_date = options.fetch(:start_date, Time.now - MONTH)
+ end_date = options.fetch(:end_date, Time.now)
+ default_params = build_default_params(profile, start_date, end_date)
param_set = [
default_params,
@@ -70,11 +72,11 @@ def parse_sort(options)
sort
end
- def build_default_params(profile)
+ def build_default_params(profile, start_date, end_date)
{
'ids' => Garb.to_ga(profile.id),
- 'start-date' => format_time(Time.now - Model::MONTH),
- 'end-date' => format_time(Time.now)
+ 'start-date' => format_time(start_date),
+ 'end-date' => format_time(end_date)
}
end
View
21 lib/garb/report_response.rb
@@ -11,6 +11,16 @@ def results
@results ||= parse
end
+ def total_results
+ feed? ? parsed_xml['feed']['openSearch:totalResults'].to_i : 0
+ end
+
+ alias :size :total_results
+ alias :count :total_results
+
+ def sampled?
+ end
+
private
def parse
entries.map do |entry|
@@ -21,8 +31,15 @@ def parse
end
def entries
- entry_hash = Crack::XML.parse(@xml)
- entry_hash ? [entry_hash['feed']['entry']].flatten.compact : []
+ feed? ? [parsed_xml['feed']['entry']].flatten.compact : []
+ end
+
+ def parsed_xml
+ @parsed_xml ||= Crack::XML.parse(@xml)
+ end
+
+ def feed?
+ !parsed_xml['feed'].nil?
end
def values_for(entry)
View
1  test/fixtures/report_feed.xml
@@ -14,6 +14,7 @@
</author>
<openSearch:startIndex>3</openSearch:startIndex>
<openSearch:itemsPerPage>4</openSearch:itemsPerPage>
+ <openSearch:totalResults>18</openSearch:totalResults>
<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>
View
11 test/unit/garb/model_test.rb
@@ -64,7 +64,7 @@ class ModelTest < MiniTest::Unit::TestCase
@test_model.stubs(:dimensions).returns(stub(:to_params => {'dimensions' => 'ga:pagePath'}))
now = Time.now
- Time.stubs(:new).returns(now)
+ Time.stubs(:now).returns(now)
# p @profile.id
@@ -115,7 +115,13 @@ class ModelTest < MiniTest::Unit::TestCase
assert_data_params(@params.merge({'start-index' => 10}))
end
- # should "be able to shift the date range"
+ should "be able to shift the date range" do
+ start_date = (Time.now - 1296000)
+ end_date = Time.now
+
+ assert_equal ['result'], @test_model.results(@profile, :start_date => start_date, :end_date => end_date)
+ assert_data_params(@params.merge({'start-date' => start_date.strftime('%Y-%m-%d'), 'end-date' => end_date.strftime('%Y-%m-%d')}))
+ end
should "return a set of results in the defined class" do
@test_model.stubs(:instance_klass).returns(ResultKlass)
@@ -125,7 +131,6 @@ class ModelTest < MiniTest::Unit::TestCase
end
end
- # should "have a block syntax for filtering results"
# should "return results as an array of the class it belongs to, if that class is an ActiveRecord descendant"
# should "return results as an array of the class it belongs to, if that class is a DataMapper descendant"
# should "return results as an array of the class it belongs to, if that class is a MongoMapper descendant"
View
9 test/unit/garb/report_response_test.rb
@@ -24,6 +24,15 @@ class ReportResponseTest < MiniTest::Unit::TestCase
response = ReportResponse.new(@file, SpecialKlass)
assert_equal [SpecialKlass, SpecialKlass, SpecialKlass], response.results.map(&:class)
end
+
+ should "know the total number of results" do
+ response = ReportResponse.new(@file)
+ assert_equal 18, response.total_results
+ assert_equal 18, response.size
+ assert_equal 18, response.count
+ end
+
+ should "know if the data has been sampled"
end
should "return an empty array if there are no results" do
Please sign in to comment.
Something went wrong with that request. Please try again.