Permalink
Browse files

fixing some odd bugs, minor whitespace, starting to add custom class …

…support for response instances
  • Loading branch information...
1 parent af5f4f2 commit 09b36feb23f213bd4d255e095e0eff3784f80e65 @tpitale tpitale committed Jun 4, 2010
Showing with 30 additions and 15 deletions.
  1. +3 −2 lib/garb/report_response.rb
  2. +6 −8 test/unit/garb/data_request_test.rb
  3. +21 −5 test/unit/garb/report_response_test.rb
View
5 lib/garb/report_response.rb
@@ -2,8 +2,9 @@ module Garb
class ReportResponse
KEYS = ['dxp:metric', 'dxp:dimension']
- def initialize(response_body)
+ def initialize(response_body, instance_klass = OpenStruct)
@xml = response_body
+ @instance_klass = instance_klass
end
def results
@@ -17,7 +18,7 @@ def parse
h.merge(Garb.from_ga(v['name']) => v['value'])
end
- OpenStruct.new(hash)
+ @instance_klass.new(hash)
end
end
View
14 test/unit/garb/data_request_test.rb
@@ -26,14 +26,12 @@ class DataRequestTest < MiniTest::Unit::TestCase
data_request = DataRequest.new(@session, "")
assert_equal "", data_request.query_string
end
-
+
should "be able to build a uri" do
- url = 'http://example.com'
- expected = URI.parse('http://example.com')
-
- assert_equal expected, DataRequest.new(@session, url).uri
+ url = 'http://example.com'
+ assert_equal URI.parse(url), DataRequest.new(@session, url).uri
end
-
+
should "be able to send a request for a single user" do
@session.stubs(:single_user?).returns(true)
response = mock('Net::HTTPOK') do |m|
@@ -82,7 +80,7 @@ class DataRequestTest < MiniTest::Unit::TestCase
assert_equal 'responseobject', data_request.oauth_user_request
assert_received(@session, :access_token)
- assert_received(access_token, :get) {|e| e.with('https://example.com/data?key=value')}
+ assert_received(access_token, :get) {|e| e.with('https://example.com/data?key=value', {'GData-Version' => '2'})}
end
should "be able to request via http with an auth token" do
@@ -92,7 +90,7 @@ class DataRequestTest < MiniTest::Unit::TestCase
http = mock do |m|
m.expects(:use_ssl=).with(true)
m.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
- m.expects(:get).with('/data?key=value', 'Authorization' => 'GoogleLogin auth=toke').returns(response)
+ m.expects(:get).with('/data?key=value', {'Authorization' => 'GoogleLogin auth=toke', 'GData-Version' => '2'}).returns(response)
end
Net::HTTP.expects(:new).with('example.com', 443).returns(http)
View
26 test/unit/garb/report_response_test.rb
@@ -1,13 +1,29 @@
require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
module Garb
+ SpecialKlass = Class.new(OpenStruct)
+
class ReportResponseTest < MiniTest::Unit::TestCase
context "A ReportResponse" do
- should "parse results from atom xml" do
- filename = File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml")
- response = ReportResponse.new(File.read(filename))
-
- assert_equal ['33', '2', '1'], response.results.map(&:pageviews)
+ context "with a report feed" do
+ setup do
+ @file = File.read(File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml"))
+ end
+
+ should "parse results from atom xml" do
+ response = ReportResponse.new(@file)
+ assert_equal ['33', '2', '1'], response.results.map(&:pageviews)
+ end
+
+ should "default to returning an array of OpenStruct objects" do
+ response = ReportResponse.new(@file)
+ assert_equal [OpenStruct, OpenStruct, OpenStruct], response.results.map(&:class)
+ end
+
+ should "return an array of instances of a specified class" do
+ response = ReportResponse.new(@file, SpecialKlass)
+ assert_equal [SpecialKlass, SpecialKlass, SpecialKlass], response.results.map(&:class)
+ end
end
should "return an empty array if there are no results" do

0 comments on commit 09b36fe

Please sign in to comment.