macros-garb 0.2.6
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.
- data/README.md +202 -0
- data/Rakefile +54 -0
- data/lib/extensions/array.rb +16 -0
- data/lib/extensions/operator.rb +20 -0
- data/lib/extensions/string.rb +17 -0
- data/lib/extensions/symbol.rb +36 -0
- data/lib/garb.rb +71 -0
- data/lib/garb/account.rb +15 -0
- data/lib/garb/authentication_request.rb +52 -0
- data/lib/garb/data_request.rb +38 -0
- data/lib/garb/oauth_session.rb +21 -0
- data/lib/garb/profile.rb +50 -0
- data/lib/garb/report.rb +31 -0
- data/lib/garb/report_parameter.rb +36 -0
- data/lib/garb/report_response.rb +62 -0
- data/lib/garb/resource.rb +89 -0
- data/lib/garb/session.rb +19 -0
- data/lib/garb/version.rb +13 -0
- data/test/fixtures/cacert.pem +67 -0
- data/test/fixtures/profile_feed.xml +33 -0
- data/test/fixtures/report_feed.xml +46 -0
- data/test/test_helper.rb +16 -0
- data/test/unit/account_test.rb +38 -0
- data/test/unit/authentication_request_test.rb +121 -0
- data/test/unit/data_request_test.rb +52 -0
- data/test/unit/garb_test.rb +9 -0
- data/test/unit/oauth_session_test.rb +11 -0
- data/test/unit/operator_test.rb +37 -0
- data/test/unit/profile_test.rb +64 -0
- data/test/unit/report_parameter_test.rb +62 -0
- data/test/unit/report_response_test.rb +29 -0
- data/test/unit/report_test.rb +71 -0
- data/test/unit/resource_test.rb +19 -0
- data/test/unit/session_test.rb +34 -0
- data/test/unit/string_test.rb +13 -0
- data/test/unit/symbol_test.rb +44 -0
- metadata +122 -0
@@ -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&dimensions=ga:country,ga:city&metrics=ga:pageViews&start-date=2008-01-01&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&start-date=2008-01-01&metrics=ga%3ApageViews&ids=ga%3A983247&dimensions=ga%3Acountry%2Cga%3Acity" rel="self" type="application/atom+xml"/>
|
11
|
+
<link href="http://www.google.com/analytics/feeds/data?start-index=1001&max-results=1000&end-date=2008-01-02&start-date=2008-01-01&metrics=ga%3ApageViews&ids=ga%3A983247&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&ga:country=%28not%20set%29&ga:city=%28not%20set%29&start-date=2008-01-01&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&ga:country=Afghanistan&ga:city=Kabul&start-date=2008-01-01&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&ga:country=Albania&ga:city=Tirana&start-date=2008-01-01&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>
|
data/test/test_helper.rb
ADDED
@@ -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,38 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', '/test_helper')
|
2
|
+
|
3
|
+
module Garb
|
4
|
+
class AccountTest < Test::Unit::TestCase
|
5
|
+
context "The Account class" do
|
6
|
+
should "have an array of accounts with all profiles" do
|
7
|
+
p1 = stub(:account_id => '1111', :account_name => 'Blog 1')
|
8
|
+
p2 = stub(:account_id => '1112', :account_name => 'Blog 2')
|
9
|
+
Profile.stubs(:all).returns([p1,p2,p1,p2])
|
10
|
+
Account.expects(:new).with([p1,p1]).returns('account1')
|
11
|
+
Account.expects(:new).with([p2,p2]).returns('account2')
|
12
|
+
assert_equal ['account1','account2'], Account.all
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "An instance of the Account class" do
|
17
|
+
context "when creating a new account from an array of profiles" do
|
18
|
+
setup do
|
19
|
+
profile = stub(:account_id => '1111', :account_name => 'Blog 1')
|
20
|
+
@profiles = [profile,profile]
|
21
|
+
@account = Account.new(@profiles)
|
22
|
+
end
|
23
|
+
|
24
|
+
should "take the account id from the first profile" do
|
25
|
+
assert_equal @profiles.first.account_id, @account.id
|
26
|
+
end
|
27
|
+
|
28
|
+
should "take the account name from the first profile" do
|
29
|
+
assert_equal @profiles.first.account_name, @account.name
|
30
|
+
end
|
31
|
+
|
32
|
+
should "store the array of profiles" do
|
33
|
+
assert_equal @profiles, @account.profiles
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
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().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,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,64 @@
|
|
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
|
+
url = 'https://www.google.com/analytics/feeds/accounts/default'
|
10
|
+
|
11
|
+
xml = read_fixture('profile_feed.xml')
|
12
|
+
|
13
|
+
data_request = mock
|
14
|
+
data_request.expects(:send_request).with().returns(stub(:body => xml))
|
15
|
+
|
16
|
+
DataRequest.expects(:new).with(url).returns(data_request)
|
17
|
+
|
18
|
+
entries = [stub]
|
19
|
+
|
20
|
+
Profile::Entry.expects(:parse).with(xml).returns(entries)
|
21
|
+
|
22
|
+
profiles = []
|
23
|
+
entries.each do |entry|
|
24
|
+
profile = stub
|
25
|
+
profiles << profile
|
26
|
+
Garb::Profile.expects(:new).with(entry).returns(profile)
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_equal profiles, Profile.all
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
context "An instance of the Profile class" do
|
35
|
+
|
36
|
+
setup do
|
37
|
+
@entry = (Profile::Entry.parse(read_fixture('profile_feed.xml'))).first
|
38
|
+
@profile = Profile.new(@entry)
|
39
|
+
end
|
40
|
+
|
41
|
+
should "have a value for :title" do
|
42
|
+
assert_equal "Historical", @profile.title
|
43
|
+
end
|
44
|
+
|
45
|
+
should "have a value for :table_id" do
|
46
|
+
assert_equal 'ga:12345', @profile.table_id
|
47
|
+
end
|
48
|
+
|
49
|
+
should "have a value for :id" do
|
50
|
+
assert_equal '12345', @profile.id
|
51
|
+
end
|
52
|
+
|
53
|
+
should "have a value for :account_id" do
|
54
|
+
assert_equal '1111', @profile.account_id
|
55
|
+
end
|
56
|
+
|
57
|
+
should "have a value for :account_name" do
|
58
|
+
assert_equal 'Blog Beta', @profile.account_name
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
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
|