jonuts-garb 0.2.3

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.
@@ -0,0 +1,33 @@
1
+ <?xml version='1.0' encoding='UTF-8'?>
2
+ <feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:dxp='http://schemas.google.com/analytics/2009'>
3
+ <id>http://www.google.com/analytics/feeds/accounts/email@example.com</id>
4
+ <updated>2009-03-27T08:14:28.000-07:00</updated>
5
+ <title type='text'>Profile list for email@example.com</title>
6
+ <link rel='self' type='application/atom+xml' href='http://www.google.com/analytics/feeds/accounts/email@example.com'/>
7
+ <author><name>Google Analytics</name></author>
8
+ <generator version='1.0'>Google Analytics</generator>
9
+ <openSearch:totalResults>2</openSearch:totalResults>
10
+ <openSearch:startIndex>1</openSearch:startIndex>
11
+ <openSearch:itemsPerPage>2</openSearch:itemsPerPage>
12
+ <entry>
13
+ <id>http://www.google.com/analytics/feeds/accounts/ga:12345</id>
14
+ <updated>2008-07-21T14:05:57.000-07:00</updated>
15
+ <title type='text'>Historical</title>
16
+ <dxp:tableId>ga:12345</dxp:tableId>
17
+ <dxp:property name='ga:accountId' value='1111'/>
18
+ <dxp:property name='ga:accountName' value='Blog Beta'/>
19
+ <dxp:property name='ga:profileId' value='1212'/>
20
+ <dxp:property name='ga:webPropertyId' value='UA-1111-1'/>
21
+ </entry>
22
+ <entry>
23
+ <id>http://www.google.com/analytics/feeds/accounts/ga:12346</id>
24
+ <updated>2008-11-24T11:51:07.000-08:00</updated>
25
+ <title type='text'>Presently</title>
26
+ <dxp:tableId>ga:12346</dxp:tableId>
27
+ <dxp:property name='ga:accountId' value='1111'/>
28
+ <dxp:property name='ga:accountName' value='Blog Beta'/>
29
+ <dxp:property name='ga:profileId' value='1213'/>
30
+ <dxp:property name='ga:webPropertyId' value='UA-1111-2'/>
31
+ </entry>
32
+ </feed>
33
+
@@ -0,0 +1,46 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <feed xmlns='http://www.w3.org/2005/Atom'
3
+ xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
4
+ xmlns:dxp='http://schemas.google.com/dataexport/2009'
5
+ xmlns:ga='http://schemas.google.com/analytics/2008'>
6
+ <id>http://www.google.com/analytics/feeds/data?ids=ga:983247&amp;dimensions=ga:country,ga:city&amp;metrics=ga:pageViews&amp;start-date=2008-01-01&amp;end-date=2008-01-02</id>
7
+ <updated>2008-01-02T15:59:59.999-08:00 </updated>
8
+ <title type="text">Google Analytics Data for Profile 983247</title>
9
+ <link href="http://www.google.com/analytics/feeds/data" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"/>
10
+ <link href="http://www.google.com/analytics/feeds/data?end-date=2008-01-02&amp;start-date=2008-01-01&amp;metrics=ga%3ApageViews&amp;ids=ga%3A983247&amp;dimensions=ga%3Acountry%2Cga%3Acity" rel="self" type="application/atom+xml"/>
11
+ <link href="http://www.google.com/analytics/feeds/data?start-index=1001&amp;max-results=1000&amp;end-date=2008-01-02&amp;start-date=2008-01-01&amp;metrics=ga%3ApageViews&amp;ids=ga%3A983247&amp;dimensions=ga%3Acountry%2Cga%3Acity" rel="next" type="application/atom+xml"/>
12
+ <author>
13
+ <name>Google Analytics</name>
14
+ </author>
15
+ <openSearch:startIndex>3</openSearch:startIndex>
16
+ <openSearch:itemsPerPage>4</openSearch:itemsPerPage>
17
+ <ga:webPropertyID>UA-983247-67</ga:webPropertyID>
18
+ <ga:start-date>2008-01-01</ga:start-date>
19
+ <ga:end-date>2008-01-02</ga:end-date>
20
+
21
+ <entry>
22
+ <id> http://www.google.com/analytics/feeds/data?ids=ga:1174&amp;ga:country=%28not%20set%29&amp;ga:city=%28not%20set%29&amp;start-date=2008-01-01&amp;end-date=2008-01-02 </id>
23
+ <updated> 2008-01-01T16:00:00.001-08:00 </updated>
24
+ <title type="text"> ga:country=(not set) | ga:city=(not set) </title>
25
+ <link href="http://www.google.com/analytics/feeds/data" rel="self" type="application/atom+xml"/>
26
+ <dxp:dimension name="ga:country" value="(not set)" />
27
+ <dxp:dimension name="ga:city" value="(not set)" />
28
+ <dxp:metric name="ga:pageviews" value="33" />
29
+ </entry>
30
+ <entry>
31
+ <id> http://www.google.com/analytics/feeds/data?ids=ga:1174&amp;ga:country=Afghanistan&amp;ga:city=Kabul&amp;start-date=2008-01-01&amp;end-date=2008-01-02 </id>
32
+ <updated> 2008-01-01T16:00:00.001-08:00 </updated>
33
+ <title type="text"> ga:country=Afghanistan | ga:city=Kabul </title>
34
+ <dxp:dimension name="ga:country" value="Afghanistan" />
35
+ <dxp:dimension name="ga:city" value="Kabul" />
36
+ <dxp:metric name="ga:pageviews" value="2" />
37
+ </entry>
38
+ <entry>
39
+ <id> http://www.google.com/analytics/feeds/data?ids=ga:1174&amp;ga:country=Albania&amp;ga:city=Tirana&amp;start-date=2008-01-01&amp;end-date=2008-01-02 </id>
40
+ <updated> 2008-01-01T16:00:00.001-08:00 </updated>
41
+ <title type="text"> ga:country=Albania | ga:city=Tirana </title>
42
+ <dxp:dimension name="ga:country" value="Albania" />
43
+ <dxp:dimension name="ga:city" value="Tirana" />
44
+ <dxp:metric name="ga:pageviews" value="1" />
45
+ </entry>
46
+ </feed>
@@ -0,0 +1,16 @@
1
+ $:.reject! { |e| e.include? 'TextMate' }
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'shoulda'
6
+ require 'mocha'
7
+
8
+ require File.dirname(__FILE__) + '/../lib/garb'
9
+
10
+ class Test::Unit::TestCase
11
+
12
+ def read_fixture(filename)
13
+ File.read(File.dirname(__FILE__) + "/fixtures/#{filename}")
14
+ end
15
+
16
+ end
@@ -0,0 +1,121 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ CA_CERT_FILE = File.join(File.dirname(__FILE__), '..', '/cacert.pem')
4
+
5
+ module Garb
6
+ class AuthenticationRequestTest < Test::Unit::TestCase
7
+
8
+ context "An instance of the AuthenticationRequest class" do
9
+
10
+ setup { @request = AuthenticationRequest.new('email', 'password') }
11
+
12
+ should "have a collection of parameters that include the email and password" do
13
+ expected =
14
+ {
15
+ 'Email' => 'user@example.com',
16
+ 'Passwd' => 'fuzzybunnies',
17
+ 'accountType' => 'HOSTED_OR_GOOGLE',
18
+ 'service' => 'analytics',
19
+ 'source' => 'vigetLabs-garb-001'
20
+ }
21
+
22
+ request = AuthenticationRequest.new('user@example.com', 'fuzzybunnies')
23
+ assert_equal expected, request.parameters
24
+ end
25
+
26
+ should "have a URI" do
27
+ assert_equal URI.parse('https://www.google.com/accounts/ClientLogin'), @request.uri
28
+ end
29
+
30
+ should "be able to send a request to the GAAPI service with proper ssl" do
31
+ @request.expects(:build_request).returns('post')
32
+
33
+ response = mock {|m| m.expects(:is_a?).with(Net::HTTPOK).returns(true) }
34
+
35
+ http = mock do |m|
36
+ m.expects(:use_ssl=).with(true)
37
+ m.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
38
+ m.expects(:ca_file=).with(CA_CERT_FILE)
39
+ m.expects(:request).with('post').yields(response)
40
+ end
41
+
42
+ Net::HTTP.expects(:new).with('www.google.com', 443).returns(http)
43
+
44
+ @request.send_request(OpenSSL::SSL::VERIFY_PEER)
45
+ end
46
+
47
+ should "be able to send a request to the GAAPI service with ignoring ssl" do
48
+ @request.expects(:build_request).returns('post')
49
+
50
+ response = mock {|m| m.expects(:is_a?).with(Net::HTTPOK).returns(true) }
51
+
52
+ http = mock do |m|
53
+ m.expects(:use_ssl=).with(true)
54
+ m.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
55
+ m.expects(:request).with('post').yields(response)
56
+ end
57
+
58
+ Net::HTTP.expects(:new).with('www.google.com', 443).returns(http)
59
+
60
+ @request.send_request(OpenSSL::SSL::VERIFY_NONE)
61
+ end
62
+
63
+ should "be able to build a request for the GAAPI service" do
64
+ params = "param"
65
+ @request.expects(:parameters).with().returns(params)
66
+
67
+ post = mock
68
+ post.expects(:set_form_data).with(params)
69
+
70
+ Net::HTTP::Post.expects(:new).with('/accounts/ClientLogin').returns(post)
71
+
72
+ @request.build_request
73
+ end
74
+
75
+ should "be able to retrieve an auth_token from the body" do
76
+ response_data =
77
+ "SID=mysid\n" +
78
+ "LSID=mylsid\n" +
79
+ "Auth=auth_token\n"
80
+
81
+ @request.expects(:send_request).with(OpenSSL::SSL::VERIFY_NONE).returns(stub(:body => response_data))
82
+
83
+ assert_equal 'auth_token', @request.auth_token
84
+ end
85
+
86
+ should "use VERIFY_PEER if auth_token needs to be secure" do
87
+ response_data =
88
+ "SID=mysid\n" +
89
+ "LSID=mylsid\n" +
90
+ "Auth=auth_token\n"
91
+
92
+ @request.expects(:send_request).with(OpenSSL::SSL::VERIFY_PEER).returns(stub(:body => response_data))
93
+
94
+ assert_equal 'auth_token', @request.auth_token(:secure => true)
95
+ end
96
+
97
+ should "raise an exception when requesting an auth_token when the authorization fails" do
98
+ @request.stubs(:build_request)
99
+ response = mock do |m|
100
+ m.expects(:is_a?).with(Net::HTTPOK).returns(false)
101
+ end
102
+
103
+ http = stub do |s|
104
+ s.stubs(:use_ssl=)
105
+ s.stubs(:verify_mode=)
106
+ s.stubs(:request).yields(response)
107
+ end
108
+
109
+ Net::HTTP.stubs(:new).with('www.google.com', 443).returns(http)
110
+
111
+ assert_raise(Garb::AuthenticationRequest::AuthError) do
112
+ @request.send_request(OpenSSL::SSL::VERIFY_NONE)
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+
119
+
120
+ end
121
+ end
@@ -0,0 +1,52 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ module Garb
4
+ class DataRequestTest < Test::Unit::TestCase
5
+
6
+ context "An instance of the DataRequest class" do
7
+
8
+ should "be able to build the query string from parameters" do
9
+ parameters = {'ids' => '12345', 'metrics' => 'country'}
10
+ data_request = DataRequest.new("", parameters)
11
+
12
+ query_string = data_request.query_string
13
+
14
+ assert_match(/^\?/, query_string)
15
+
16
+ query_string.sub!(/^\?/, '')
17
+
18
+ assert_equal ["ids=12345", "metrics=country"], query_string.split('&').sort
19
+ end
20
+
21
+ should "return an empty query string if parameters are empty" do
22
+ data_request = DataRequest.new("")
23
+ assert_equal "", data_request.query_string
24
+ end
25
+
26
+ should "be able to build a uri" do
27
+ url = 'http://example.com'
28
+ expected = URI.parse('http://example.com')
29
+
30
+ assert_equal expected, DataRequest.new(url).uri
31
+ end
32
+
33
+ should "be able to make a request to the GAAPI" do
34
+ Session.expects(:auth_token).with(nil).returns('toke')
35
+ response = mock
36
+ response.expects(:is_a?).with(Net::HTTPOK).returns(true)
37
+
38
+ http = mock do |m|
39
+ m.expects(:use_ssl=).with(true)
40
+ m.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
41
+ m.expects(:get).with('/data?key=value', 'Authorization' => 'GoogleLogin auth=toke').returns(response)
42
+ end
43
+
44
+ Net::HTTP.expects(:new).with('example.com', 443).returns(http)
45
+
46
+ data_request = DataRequest.new('https://example.com/data', 'key' => 'value')
47
+ assert_equal response, data_request.send_request
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ class GarbTest < Test::Unit::TestCase
4
+ context "A green egg" do
5
+ should "be served with ham" do
6
+ assert true
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ module Garb
4
+ class OAuthSessionTest < Test::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,37 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ class OperatorTest < Test::Unit::TestCase
4
+ context "An instance of an Operator" do
5
+ should "lower camelize the target" do
6
+ assert_equal "ga:uniqueVisits=", Operator.new(:unique_visits, "=").to_ga
7
+ end
8
+
9
+ should "return target and operator together" do
10
+ assert_equal "ga:metric=", Operator.new(:metric, "=").to_ga
11
+ end
12
+
13
+ should "prefix the operator to the target" do
14
+ assert_equal "-ga:metric", Operator.new(:metric, "-", true).to_ga
15
+ end
16
+
17
+ should "know if it is equal to another operator" do
18
+ op1 = Operator.new(:hello, "==")
19
+ op2 = Operator.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 = Operator.new(:hello, "==")
25
+ op2 = Operator.new(:hello, "==", true)
26
+ assert_not_equal op1, op2
27
+
28
+ op1 = Operator.new(:hello1, "==")
29
+ op2 = Operator.new(:hello2, "==")
30
+ assert_not_equal op1, op2
31
+
32
+ op1 = Operator.new(:hello, "!=")
33
+ op2 = Operator.new(:hello, "==")
34
+ assert_not_equal op1, op2
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,59 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ module Garb
4
+ class ProfileTest < Test::Unit::TestCase
5
+
6
+ context "The Profile class" do
7
+
8
+ should "be able to return a list of all profiles" do
9
+ Session.stubs(:email).with(nil).returns('user@host.com')
10
+
11
+ url = 'https://www.google.com/analytics/feeds/accounts/user@host.com'
12
+
13
+ xml = read_fixture('profile_feed.xml')
14
+
15
+ data_request = mock
16
+ data_request.expects(:send_request).with('user@host.com').returns(stub(:body => xml))
17
+
18
+ DataRequest.expects(:new).with(url).returns(data_request)
19
+
20
+ entries = [stub]
21
+
22
+ Profile::Entry.expects(:parse).with(xml).returns(entries)
23
+
24
+ profiles = []
25
+ entries.each do |entry|
26
+ profile = stub
27
+ profiles << profile
28
+ Garb::Profile.expects(:new).with(entry,'user@host.com').returns(profile)
29
+ end
30
+
31
+ assert_equal profiles, Profile.all
32
+ end
33
+
34
+ end
35
+
36
+ context "An instance of the Profile class" do
37
+
38
+ setup do
39
+ Session.stubs(:email).with(nil).returns('user@host.com')
40
+ @entry = (Profile::Entry.parse(read_fixture('profile_feed.xml'))).first
41
+ @profile = Profile.new(@entry)
42
+ end
43
+
44
+ should "have a value for :title" do
45
+ assert_equal "Historical", @profile.title
46
+ end
47
+
48
+ should "have a value for :table_id" do
49
+ assert_equal 'ga:12345', @profile.table_id
50
+ end
51
+
52
+ should "have a value for :id" do
53
+ assert_equal '12345', @profile.id
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,62 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ module Garb
4
+ class ReportParameterTest < Test::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 << :request_uri)
21
+ assert_equal [:request_uri], @metrics.elements
22
+ end
23
+
24
+ should "merge an array of elements" do
25
+ assert_equal(@metrics, @metrics << [:request_uri])
26
+ assert_equal [:request_uri], @metrics.elements
27
+ end
28
+
29
+ context "converting to params" do
30
+ should "be able to format the parameters into strings" do
31
+ @metrics << :request_uri
32
+ assert_equal({'metrics' => 'ga:requestUri'}, @metrics.to_params)
33
+ end
34
+
35
+ should "join multiple symbol elements" do
36
+ @metrics << :request_uri << :city
37
+ assert_equal({'metrics' => 'ga:requestUri,ga:city'}, @metrics.to_params)
38
+ end
39
+
40
+ should "join operator elements" do
41
+ @metrics << :city.desc
42
+ assert_equal({'metrics' => '-ga:city'}, @metrics.to_params)
43
+ end
44
+
45
+ should "parameterize hash operators and join elements" do
46
+ @metrics << {:city.eql => 'New York'}
47
+ params = {'metrics' => 'ga:city%3D%3DNew+York'}
48
+
49
+ assert_equal params, @metrics.to_params
50
+ end
51
+
52
+ should "properly encode operators" do
53
+ @metrics << {:request_uri.contains => 'New York'}
54
+ params = {'metrics' => 'ga:requestUri%3D~New+York'}
55
+
56
+ assert_equal params, @metrics.to_params
57
+ end
58
+ end
59
+ end
60
+
61
+ end
62
+ end