Permalink
Browse files

support setting an instance klass on reports extending Garb::Resource

  • Loading branch information...
1 parent a464c31 commit 50ecc7943cdab1bae8ea8e7c013e7fb1edceb8f0 @tpitale tpitale committed Sep 28, 2010
Showing with 27 additions and 4 deletions.
  1. +14 −2 lib/garb/resource.rb
  2. +13 −2 test/unit/garb/resource_test.rb
View
@@ -44,6 +44,18 @@ def set_segment_id(id)
@segment = "gaid::#{id.to_i}"
end
+ def segment
+ @segment
+ end
+
+ def set_instance_klass(klass)
+ @instance_klass = klass
+ end
+
+ def instance_klass
+ @instance_klass || OpenStruct
+ end
+
def results(profile, opts = {}, &block)
@profile = profile.is_a?(Profile) ? profile : Profile.first(profile, opts.fetch(:session, Session))
@@ -55,7 +67,7 @@ def results(profile, opts = {}, &block)
instance_eval(&block) if block_given?
- ReportResponse.new(send_request_for_body).results
+ ReportResponse.new(send_request_for_body, instance_klass).results
else
[]
end
@@ -72,7 +84,7 @@ def default_params
end
def segment_params
- @segment ? {'segment' => @segment} : {}
+ segment.nil? ? {} : {'segment' => segment}
end
def params
@@ -11,7 +11,7 @@ class ResourceTest < MiniTest::Unit::TestCase
should "get results from GA" do
profile = stub(:is_a? => true)
TestReport.expects(:send_request_for_body).returns('xml')
- Garb::ReportResponse.expects(:new).with('xml').returns(mock(:results => 'analytics'))
+ Garb::ReportResponse.expects(:new).with('xml', OpenStruct).returns(mock(:results => 'analytics'))
assert_equal 'analytics', TestReport.results(profile)
end
@@ -20,12 +20,23 @@ class ResourceTest < MiniTest::Unit::TestCase
profile = '123'
session = Garb::Session.new
TestReport.expects(:send_request_for_body).returns('xml')
- Garb::ReportResponse.expects(:new).with('xml').returns(mock(:results => 'analytics'))
+ Garb::ReportResponse.expects(:new).with('xml', OpenStruct).returns(mock(:results => 'analytics'))
Garb::Profile.expects(:first).with(profile, session).returns(mock('Garb::Profile'))
assert_equal 'analytics', TestReport.results(profile, :session => session)
end
+ should "permit setting a segment id" do
+ TestReport.set_segment_id 1
+ assert_equal "gaid::1", TestReport.segment
+ end
+
+ should "permit setting a klass used for instantiation of results" do
+ TestKlass = Class.new(OpenStruct)
+ TestReport.set_instance_klass TestKlass
+ assert_equal TestKlass, TestReport.instance_klass
+ end
+
should "return an empty result set if profile is invalid" do
profile = '123'
TestReport.expects(:send_request_for_body).never

0 comments on commit 50ecc79

Please sign in to comment.