shingara-garb 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/README.md +255 -0
  2. data/Rakefile +56 -0
  3. data/lib/garb.rb +42 -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 +42 -0
  7. data/lib/garb/filter_parameters.rb +41 -0
  8. data/lib/garb/profile.rb +37 -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 +33 -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 +35 -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 +38 -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 +119 -0
  30. data/test/unit/garb/filter_parameters_test.rb +59 -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 +75 -0
  34. data/test/unit/garb/report_parameter_test.rb +43 -0
  35. data/test/unit/garb/report_response_test.rb +14 -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 +91 -0
  39. data/test/unit/garb_test.rb +14 -0
  40. data/test/unit/symbol_operator_test.rb +37 -0
  41. metadata +146 -0
@@ -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,75 @@
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 = stub
15
+ data_request.stubs(:send_request).returns(stub(:body => xml))
16
+ DataRequest.stubs(:new).returns(data_request)
17
+
18
+ assert_equal ['12345', '12346'], Profile.all(@session).map(&:id)
19
+ assert_received(DataRequest, :new) {|e| e.with(@session, url)}
20
+ assert_received(data_request, :send_request)
21
+ end
22
+
23
+ should "return the first profile for a given web property id" do
24
+ profile1 = stub(:web_property_id => '12345', :id => 'abcdef')
25
+ profile2 = stub(:web_property_id => '67890', :id => 'ghijkl')
26
+ entries = [profile1, profile2]
27
+
28
+ Profile.stubs(:all).returns(entries)
29
+
30
+ assert_equal profile1, Profile.first('12345', @session)
31
+
32
+ assert_received(Profile, :all) {|e| e.with(@session)}
33
+ end
34
+
35
+ should "return the first profile for a given table id" do
36
+ profile1 = stub(:id => '12345', :web_property_id => 'abcdef')
37
+ profile2 = stub(:id => '67890', :web_property_id => 'ghijkl')
38
+ entries = [profile1, profile2]
39
+
40
+ Profile.stubs(:all).returns(entries)
41
+
42
+ assert_equal profile2, Profile.first('67890', @session)
43
+
44
+ assert_received(Profile, :all) {|e| e.with(@session)}
45
+ end
46
+ end
47
+
48
+ context "An instance of the Profile class" do
49
+ setup do
50
+ entry = Profile.parse(read_fixture('profile_feed.xml')).first
51
+ @profile = Profile.new(entry, Session)
52
+ end
53
+
54
+ should "have a value for :title" do
55
+ assert_equal "Historical", @profile.title
56
+ end
57
+
58
+ should "have a value for :table_id" do
59
+ assert_equal 'ga:12345', @profile.table_id
60
+ end
61
+
62
+ should "have a value for :id" do
63
+ assert_equal '12345', @profile.id
64
+ end
65
+
66
+ should "have a value for :account_id" do
67
+ assert_equal '1111', @profile.account_id
68
+ end
69
+
70
+ should "have a value for :account_name" do
71
+ assert_equal 'Blog Beta', @profile.account_name
72
+ end
73
+ end
74
+ end
75
+ 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,14 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', '/test_helper')
2
+
3
+ module Garb
4
+ class ReportResponseTest < MiniTest::Unit::TestCase
5
+ context "A ReportResponse" do
6
+ should "parse results from atom xml" do
7
+ filename = File.join(File.dirname(__FILE__), '..', '..', "/fixtures/report_feed.xml")
8
+ response = ReportResponse.new(File.read(filename))
9
+
10
+ assert_equal ['33', '2', '1'], response.results.map(&:pageviews)
11
+ end
12
+ end
13
+ end
14
+ 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,91 @@
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
+
42
+ should "define a AuthSub" do
43
+ Session.auth_sub('a_token')
44
+ assert_equal true, Session.auth_sub?
45
+ assert_equal 'a_token', Session.auth_token
46
+ end
47
+
48
+ end
49
+
50
+ context "A Session" do
51
+ setup do
52
+ @session = Session.new
53
+ end
54
+
55
+ should "be able retrieve an auth_token for a user" do
56
+ auth_request = mock {|m| m.expects(:auth_token).with({}).returns('toke') }
57
+ AuthenticationRequest.expects(:new).with('email', 'password', {}).returns(auth_request)
58
+
59
+ @session.login('email', 'password')
60
+ assert_equal 'toke', @session.auth_token
61
+ end
62
+
63
+ should "be able retrieve an auth_token for a user with secure ssl" do
64
+ opts = {:secure => true, :account_type => 'GOOGLE'}
65
+ auth_request = mock {|m| m.expects(:auth_token).with(opts).returns('toke') }
66
+ AuthenticationRequest.expects(:new).with('email', 'password', opts).returns(auth_request)
67
+
68
+ @session.login('email', 'password', opts)
69
+ assert_equal 'toke', @session.auth_token
70
+ end
71
+
72
+ should "retain the email address for this session" do
73
+ AuthenticationRequest.stubs(:new).returns(stub(:auth_token => 'toke'))
74
+
75
+ @session.login('email', 'password')
76
+ assert_equal 'email', @session.email
77
+ end
78
+
79
+ should "know if the Session is for a single user" do
80
+ @session.auth_token = "abcdefg1234567"
81
+ assert_equal true, @session.single_user?
82
+ end
83
+
84
+ should "know if the Session is for oauth" do
85
+ @session.access_token = 'some_oauth_access_token'
86
+ assert_equal true, @session.oauth_user?
87
+ end
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ class GarbTest < MiniTest::Unit::TestCase
4
+ context "The Garb module" do
5
+ should 'prefix a string with ga: for GA' do
6
+ assert_equal '-ga:bob', Garb.to_google_analytics(stub(:to_google_analytics => '-ga:bob'))
7
+ assert_equal 'ga:bob', Garb.to_google_analytics('bob')
8
+ end
9
+
10
+ should 'remove ga: prefix' do
11
+ assert_equal 'bob', Garb.from_google_analytics('ga:bob')
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ class SymbolOperatorTest < MiniTest::Unit::TestCase
4
+ context "An instance of a SymbolOperator" do
5
+ should "lower camelize the target" do
6
+ assert_equal "ga:uniqueVisits==", SymbolOperator.new(:unique_visits, :eql).to_google_analytics
7
+ end
8
+
9
+ should "return target and operator together" do
10
+ assert_equal "ga:metric==", SymbolOperator.new(:metric, :eql).to_google_analytics
11
+ end
12
+
13
+ should "prefix the operator to the target" do
14
+ assert_equal "-ga:metric", SymbolOperator.new(:metric, :desc).to_google_analytics
15
+ end
16
+
17
+ # should "know if it is equal to another operator" do
18
+ # op1 = SymbolOperator.new(:hello, "==")
19
+ # op2 = SymbolOperator.new(:hello, "==")
20
+ # assert_equal op1, op2
21
+ # end
22
+ #
23
+ # should "not be equal to another operator if target, operator, or prefix is different" do
24
+ # op1 = SymbolOperator.new(:hello, "==")
25
+ # op2 = SymbolOperator.new(:hello, "==", true)
26
+ # refute_equal op1, op2
27
+ #
28
+ # op1 = SymbolOperator.new(:hello1, "==")
29
+ # op2 = SymbolOperator.new(:hello2, "==")
30
+ # refute_equal op1, op2
31
+ #
32
+ # op1 = SymbolOperator.new(:hello, "!=")
33
+ # op2 = SymbolOperator.new(:hello, "==")
34
+ # refute_equal op1, op2
35
+ # end
36
+ end
37
+ end