garb-authsub 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data/README.md +250 -0
  2. data/Rakefile +56 -0
  3. data/lib/garb.rb +38 -0
  4. data/lib/garb/account.rb +29 -0
  5. data/lib/garb/authentication_request.rb +53 -0
  6. data/lib/garb/data_request.rb +51 -0
  7. data/lib/garb/filter_parameters.rb +37 -0
  8. data/lib/garb/profile.rb +57 -0
  9. data/lib/garb/profile_reports.rb +15 -0
  10. data/lib/garb/report.rb +26 -0
  11. data/lib/garb/report_parameter.rb +25 -0
  12. data/lib/garb/report_response.rb +62 -0
  13. data/lib/garb/reports.rb +5 -0
  14. data/lib/garb/reports/bounces.rb +5 -0
  15. data/lib/garb/reports/exits.rb +5 -0
  16. data/lib/garb/reports/pageviews.rb +5 -0
  17. data/lib/garb/reports/unique_pageviews.rb +5 -0
  18. data/lib/garb/reports/visits.rb +5 -0
  19. data/lib/garb/resource.rb +92 -0
  20. data/lib/garb/session.rb +29 -0
  21. data/lib/garb/version.rb +13 -0
  22. data/lib/support.rb +39 -0
  23. data/test/fixtures/cacert.pem +67 -0
  24. data/test/fixtures/profile_feed.xml +40 -0
  25. data/test/fixtures/report_feed.xml +46 -0
  26. data/test/test_helper.rb +18 -0
  27. data/test/unit/garb/account_test.rb +53 -0
  28. data/test/unit/garb/authentication_request_test.rb +121 -0
  29. data/test/unit/garb/data_request_test.rb +106 -0
  30. data/test/unit/garb/filter_parameters_test.rb +57 -0
  31. data/test/unit/garb/oauth_session_test.rb +11 -0
  32. data/test/unit/garb/profile_reports_test.rb +29 -0
  33. data/test/unit/garb/profile_test.rb +87 -0
  34. data/test/unit/garb/report_parameter_test.rb +43 -0
  35. data/test/unit/garb/report_response_test.rb +29 -0
  36. data/test/unit/garb/report_test.rb +91 -0
  37. data/test/unit/garb/resource_test.rb +38 -0
  38. data/test/unit/garb/session_test.rb +84 -0
  39. data/test/unit/garb_test.rb +14 -0
  40. data/test/unit/symbol_operator_test.rb +37 -0
  41. metadata +131 -0
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ class FilterParametersTest < MiniTest::Unit::TestCase
5
+ def self.should_define_operators(*operators)
6
+ operators.each do |operator|
7
+ should "create an operator and add to parameters for the #{operator} method" do
8
+ new_operator = stub
9
+ symbol = :foo
10
+
11
+ SymbolOperator.expects(:new).with(:bar, operator).returns(new_operator)
12
+ @filter_parameters.send(operator.to_sym, :bar, 100)
13
+
14
+ parameter = {new_operator => 100}
15
+ assert_equal parameter, @filter_parameters.parameters.last
16
+ end
17
+ end
18
+ end
19
+
20
+ context "A FilterParameters" do
21
+ setup do
22
+ @filter_parameters = FilterParameters.new
23
+ end
24
+
25
+ should_define_operators :eql, :not_eql, :gt, :gte, :lt, :lte,
26
+ :matches, :does_not_match, :contains, :does_not_contain, :substring, :not_substring
27
+
28
+ should "instance eval for filters" do
29
+ blk = lambda {"in a block"}
30
+
31
+ @filter_parameters.expects(:instance_eval)
32
+ @filter_parameters.filters(&blk)
33
+ end
34
+
35
+ context "when converting parameters hash into query string parameters" do
36
+ should "parameterize hash operators and join elements" do
37
+ @filter_parameters.filters do
38
+ eql(:city, 'New York')
39
+ eql(:state, 'New York')
40
+ end
41
+
42
+ params = {'filters' => 'ga:city%3D%3DNew+York;ga:state%3D%3DNew+York'}
43
+ assert_equal params, @filter_parameters.to_params
44
+ end
45
+
46
+ should "properly encode operators" do
47
+ @filter_parameters.filters do
48
+ contains(:page_path, 'New York')
49
+ end
50
+
51
+ params = {'filters' => 'ga:pagePath%3D~New+York'}
52
+ assert_equal params, @filter_parameters.to_params
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ class OAuthSessionTest < MiniTest::Unit::TestCase
5
+ context "An instance of OAuthSession" do
6
+ should "have tests" do
7
+ assert true
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+
5
+ Exits = Class.new
6
+
7
+ class FakeProfile
8
+ include ProfileReports
9
+ end
10
+
11
+ class ProfileReportsTest < MiniTest::Unit::TestCase
12
+ context "The ProfileReports module" do
13
+ should "define a new method when given a class" do
14
+ ProfileReports.add_report_method(Exits)
15
+ assert_equal true, FakeProfile.new.respond_to?(:exits)
16
+ end
17
+
18
+ should "return results from the given class with options" do
19
+ results = [1,2,3]
20
+ Exits.stubs(:results).returns(results)
21
+ ProfileReports.add_report_method(Exits)
22
+
23
+ profile = FakeProfile.new
24
+ assert_equal results, profile.exits(:start => "now")
25
+ assert_received(Exits, :results) {|e| e.with(profile, :start => "now")}
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,87 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ class ProfileTest < MiniTest::Unit::TestCase
5
+
6
+ context "The Profile class" do
7
+ setup {@session = Session.new}
8
+
9
+ should "be able to return a list of all profiles" do
10
+ url = 'https://www.google.com/analytics/feeds/accounts/default'
11
+
12
+ xml = read_fixture('profile_feed.xml')
13
+
14
+ data_request = mock
15
+ data_request.expects(:send_request).with().returns(stub(:body => xml))
16
+
17
+ DataRequest.expects(:new).with(@session, url).returns(data_request)
18
+
19
+ entries = [stub]
20
+
21
+ Profile::Entry.expects(:parse).with(xml).returns(entries)
22
+
23
+ profiles = []
24
+ entries.each do |entry|
25
+ profile = stub
26
+ profiles << profile
27
+ Garb::Profile.expects(:new).with(entry, @session).returns(profile)
28
+ end
29
+
30
+ assert_equal profiles, Profile.all(@session)
31
+ end
32
+
33
+ should "return the first profile for a given web property id" do
34
+ profile1 = stub(:web_property_id => '12345', :id => 'abcdef')
35
+ profile2 = stub(:web_property_id => '67890', :id => 'ghijkl')
36
+ entries = [profile1, profile2]
37
+
38
+ Garb::Profile.stubs(:all).returns(entries)
39
+
40
+ assert_equal profile1, Garb::Profile.first('12345', @session)
41
+
42
+ assert_received(Garb::Profile, :all) {|e| e.with(@session)}
43
+ end
44
+
45
+ should "return the first profile for a given table id" do
46
+ profile1 = stub(:id => '12345', :web_property_id => 'abcdef')
47
+ profile2 = stub(:id => '67890', :web_property_id => 'ghijkl')
48
+ entries = [profile1, profile2]
49
+
50
+ Garb::Profile.stubs(:all).returns(entries)
51
+
52
+ assert_equal profile2, Garb::Profile.first('67890', @session)
53
+
54
+ assert_received(Garb::Profile, :all) {|e| e.with(@session)}
55
+ end
56
+ end
57
+
58
+ context "An instance of the Profile class" do
59
+
60
+ setup do
61
+ @entry = (Profile::Entry.parse(read_fixture('profile_feed.xml'))).first
62
+ @profile = Profile.new(@entry, Session)
63
+ end
64
+
65
+ should "have a value for :title" do
66
+ assert_equal "Historical", @profile.title
67
+ end
68
+
69
+ should "have a value for :table_id" do
70
+ assert_equal 'ga:12345', @profile.table_id
71
+ end
72
+
73
+ should "have a value for :id" do
74
+ assert_equal '12345', @profile.id
75
+ end
76
+
77
+ should "have a value for :account_id" do
78
+ assert_equal '1111', @profile.account_id
79
+ end
80
+
81
+ should "have a value for :account_name" do
82
+ assert_equal 'Blog Beta', @profile.account_name
83
+ end
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ class ReportParameterTest < MiniTest::Unit::TestCase
5
+
6
+ context "An instance of the ReportParameter class" do
7
+ setup do
8
+ @metrics = ReportParameter.new(:metrics)
9
+ end
10
+
11
+ should "have a name" do
12
+ assert_equal "metrics", @metrics.name
13
+ end
14
+
15
+ should "have a list of elements" do
16
+ assert_equal [], @metrics.elements
17
+ end
18
+
19
+ should "be able to add new elements" do
20
+ assert_equal(@metrics, @metrics << :page_path)
21
+ assert_equal [:page_path], @metrics.elements
22
+ end
23
+
24
+ should "merge an array of elements" do
25
+ assert_equal(@metrics, @metrics << [:page_path])
26
+ assert_equal [:page_path], @metrics.elements
27
+ end
28
+
29
+ context "converting to params" do
30
+ should "be able to format the parameters into strings" do
31
+ @metrics << :page_path
32
+ assert_equal({'metrics' => 'ga:pagePath'}, @metrics.to_params)
33
+ end
34
+
35
+ should "join multiple symbol elements" do
36
+ @metrics << :page_path << :city
37
+ assert_equal({'metrics' => 'ga:pagePath,ga:city'}, @metrics.to_params)
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ class ReportResponseTest < MiniTest::Unit::TestCase
5
+ context "An instance of the ReportResponse class" do
6
+ setup do
7
+ @xml = File.read(File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml"))
8
+ @response = ReportResponse.new(@xml)
9
+ end
10
+
11
+ should "parse xml response with happymapper" do
12
+ h1 = {"city"=>"(not set)", "pageviews"=>"33", "country"=>"(not set)"}
13
+ h2 = {"city"=>"Kabul", "pageviews"=>"2", "country"=>"Afghanistan"}
14
+ h3 = {"city"=>"Tirana", "pageviews"=>"1", "country"=>"Albania"}
15
+
16
+ OpenStruct.expects(:new).with(h1).returns('entry1')
17
+ OpenStruct.expects(:new).with(h2).returns('entry2')
18
+ OpenStruct.expects(:new).with(h3).returns('entry3')
19
+
20
+ assert_equal(['entry1', 'entry2', 'entry3'], @response.parse)
21
+ end
22
+
23
+ should "have results or parse them" do
24
+ @response.expects(:parse)
25
+ @response.results
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,91 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ # Also tests Garb::Resource, which is the basis for Garb::Report
5
+ class ReportTest < MiniTest::Unit::TestCase
6
+ context "An instance of the Report class" do
7
+ setup do
8
+ @now = Time.now
9
+ Time.stubs(:now).returns(@now)
10
+ @profile = stub(:table_id => 'ga:1234', :session => Session)
11
+ @report = Report.new(@profile)
12
+ end
13
+
14
+ %w(metrics dimensions sort).each do |param|
15
+ should "have parameters for #{param}" do
16
+ assert @report.send(:"#{param}").is_a?(ReportParameter)
17
+ end
18
+
19
+ should "clear parameters for #{param}" do
20
+ assert_equal({}, @report.send(:"clear_#{param}").to_params)
21
+ end
22
+ end
23
+
24
+ should "have filter parameters for filters" do
25
+ assert @report.filters.is_a?(FilterParameters)
26
+ end
27
+
28
+ should "add new filters to filter parameters" do
29
+ @report.clear_filters
30
+ hash = {:thing.gt => 'val'}
31
+ @report.filters hash
32
+
33
+ assert_equal hash, @report.filters.parameters.first
34
+ end
35
+
36
+ should "clear filter parameters for filters" do
37
+ assert_equal({}, @report.clear_filters.to_params)
38
+ end
39
+
40
+ should "have default parameters" do
41
+ @report.stubs(:format_time).returns('2009-08-01')
42
+ params = {'ids' => 'ga:1234', 'start-date' => '2009-08-01', 'end-date' => '2009-08-01'}
43
+ assert_equal params, @report.default_params
44
+ end
45
+
46
+ should "collect params from metrics, dimensions, filters, sort, and defaults" do
47
+ @report.stubs(:metrics).returns(stub(:to_params => {'metrics' => 6}))
48
+ @report.stubs(:dimensions).returns(stub(:to_params => {'dimensions' => 5}))
49
+ @report.stubs(:filters).returns(stub(:to_params => {'filters' => 4}))
50
+ @report.stubs(:sort).returns(stub(:to_params => {'sort' => 3}))
51
+ @report.stubs(:page_params).returns({'page_params' => 2})
52
+ @report.stubs(:default_params).returns({'default_params' => 1})
53
+
54
+ params = {'metrics' => 6, 'dimensions' => 5, 'filters' => 4, 'sort' => 3, 'page_params' => 2, 'default_params' => 1}
55
+ assert_equal params, @report.params
56
+ end
57
+
58
+ should "format time" do
59
+ assert_equal @now.strftime('%Y-%m-%d'), @report.format_time(@now)
60
+ end
61
+
62
+ should "send a data request to GA" do
63
+ response = mock {|m| m.expects(:body).returns('response body') }
64
+ request = mock {|m| m.expects(:send_request).returns(response) }
65
+ @report.expects(:params).returns('params')
66
+
67
+ DataRequest.expects(:new).with(Session, Garb::Report::URL, 'params').returns(request)
68
+ assert_equal 'response body', @report.send_request_for_body
69
+ end
70
+
71
+ should "fetch and parse results from GA" do
72
+ @report.expects(:send_request_for_body).with().returns('xml')
73
+ ReportResponse.expects(:new).with('xml').returns(mock(:results => ['entry']))
74
+ assert_equal ['entry'], @report.results
75
+ end
76
+ end
77
+
78
+ context "An instance of the Report class with initial options" do
79
+ setup do
80
+ @profile = stub(:table_id => 'ga:1234')
81
+ @report = Report.new(@profile, :limit => 10, :offset => 20)
82
+ end
83
+
84
+ should "have page paramaters" do
85
+ params = {'max-results' => 10, 'start-index' => 20}
86
+ assert_equal params, @report.page_params
87
+ end
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ class TestReport
4
+ extend Garb::Resource
5
+ end
6
+
7
+ # Most of the resource testing is done as a part of ReportTest
8
+ class ResourceTest < MiniTest::Unit::TestCase
9
+
10
+ context "A class with Garb::Resource mixed in" do
11
+ should "get results from GA" do
12
+ profile = stub(:is_a? => true)
13
+ TestReport.expects(:send_request_for_body).returns('xml')
14
+ Garb::ReportResponse.expects(:new).with('xml').returns(mock(:results => 'analytics'))
15
+
16
+ assert_equal 'analytics', TestReport.results(profile)
17
+ end
18
+
19
+ should "get results from GA using a specific user session" do
20
+ profile = '123'
21
+ session = Garb::Session.new
22
+ TestReport.expects(:send_request_for_body).returns('xml')
23
+ Garb::ReportResponse.expects(:new).with('xml').returns(mock(:results => 'analytics'))
24
+ Garb::Profile.expects(:first).with(profile, session).returns(mock('Garb::Profile'))
25
+
26
+ assert_equal 'analytics', TestReport.results(profile, :session => session)
27
+ end
28
+
29
+ should "return an empty result set if profile is invalid" do
30
+ profile = '123'
31
+ TestReport.expects(:send_request_for_body).never
32
+ Garb::ReportResponse.expects(:new).never
33
+
34
+ Garb::Profile.expects(:first).returns(nil)
35
+ assert_equal [], TestReport.results(profile)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,84 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ class SessionTest < MiniTest::Unit::TestCase
5
+
6
+ context "The Session class" do
7
+
8
+ should "be able retrieve an auth_token for a user" do
9
+ auth_request = mock {|m| m.expects(:auth_token).with({}).returns('toke') }
10
+ AuthenticationRequest.expects(:new).with('email', 'password', {}).returns(auth_request)
11
+
12
+ Session.login('email', 'password')
13
+ assert_equal 'toke', Session.auth_token
14
+ end
15
+
16
+ should "be able retrieve an auth_token for a user with secure ssl" do
17
+ opts = {:secure => true, :account_type => 'GOOGLE'}
18
+ auth_request = mock {|m| m.expects(:auth_token).with(opts).returns('toke') }
19
+ AuthenticationRequest.expects(:new).with('email', 'password', opts).returns(auth_request)
20
+
21
+ Session.login('email', 'password', opts)
22
+ assert_equal 'toke', Session.auth_token
23
+ end
24
+
25
+ should "retain the email address for this session" do
26
+ AuthenticationRequest.stubs(:new).returns(stub(:auth_token => 'toke'))
27
+
28
+ Session.login('email', 'password')
29
+ assert_equal 'email', Session.email
30
+ end
31
+
32
+ should "know if the Session is for a single user" do
33
+ Session.auth_token = "abcdefg1234567"
34
+ assert_equal true, Session.single_user?
35
+ end
36
+
37
+ should "know if the Session is for oauth" do
38
+ Session.access_token = 'some_oauth_access_token'
39
+ assert_equal true, Session.oauth_user?
40
+ end
41
+ end
42
+
43
+ context "A Session" do
44
+ setup do
45
+ @session = Session.new
46
+ end
47
+
48
+ should "be able retrieve an auth_token for a user" do
49
+ auth_request = mock {|m| m.expects(:auth_token).with({}).returns('toke') }
50
+ AuthenticationRequest.expects(:new).with('email', 'password', {}).returns(auth_request)
51
+
52
+ @session.login('email', 'password')
53
+ assert_equal 'toke', @session.auth_token
54
+ end
55
+
56
+ should "be able retrieve an auth_token for a user with secure ssl" do
57
+ opts = {:secure => true, :account_type => 'GOOGLE'}
58
+ auth_request = mock {|m| m.expects(:auth_token).with(opts).returns('toke') }
59
+ AuthenticationRequest.expects(:new).with('email', 'password', opts).returns(auth_request)
60
+
61
+ @session.login('email', 'password', opts)
62
+ assert_equal 'toke', @session.auth_token
63
+ end
64
+
65
+ should "retain the email address for this session" do
66
+ AuthenticationRequest.stubs(:new).returns(stub(:auth_token => 'toke'))
67
+
68
+ @session.login('email', 'password')
69
+ assert_equal 'email', @session.email
70
+ end
71
+
72
+ should "know if the Session is for a single user" do
73
+ @session.auth_token = "abcdefg1234567"
74
+ assert_equal true, @session.single_user?
75
+ end
76
+
77
+ should "know if the Session is for oauth" do
78
+ @session.access_token = 'some_oauth_access_token'
79
+ assert_equal true, @session.oauth_user?
80
+ end
81
+ end
82
+
83
+ end
84
+ end