vigetlabs-garb 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
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().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().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).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
+ @entry = (Profile::Entry.parse(read_fixture('profile_feed.xml'))).first
40
+ @profile = Profile.new(@entry)
41
+ end
42
+
43
+ should "have a value for :title" do
44
+ assert_equal "Historical", @profile.title
45
+ end
46
+
47
+ should "have a value for :table_id" do
48
+ assert_equal 'ga:12345', @profile.table_id
49
+ end
50
+
51
+ should "have a value for :id" do
52
+ assert_equal '12345', @profile.id
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,55 @@
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
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ module Garb
4
+ class ReportResponseTest < Test::Unit::TestCase
5
+ context "An instance of the ReportResponse class" do
6
+ setup do
7
+ @xml = File.read(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
+ end
23
+ end
24
+ end
@@ -0,0 +1,124 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ module Garb
4
+ class ReportTest < Test::Unit::TestCase
5
+
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')
11
+ @report = Report.new(@profile)
12
+ end
13
+
14
+ %w(metrics dimensions sort filters).each do |param|
15
+ should "have parameters for #{param}" do
16
+ assert @report.send(:"#{param}").is_a?(ReportParameter)
17
+ end
18
+ end
19
+
20
+ should "have a start_date" do
21
+ report = Report.new(@profile, :start_date => @now)
22
+ assert_equal @now, report.start_date
23
+ end
24
+
25
+ should "have a default start_date of one month ago" do
26
+ month_ago = @now - Report::MONTH
27
+ assert_equal month_ago, @report.start_date
28
+ end
29
+
30
+ should "have an end_date" do
31
+ report = Report.new(@profile, :end_date => @now)
32
+ assert_equal @now, report.end_date
33
+ end
34
+
35
+ should "have a default end_date of now" do
36
+ assert_equal @now, @report.end_date
37
+ end
38
+
39
+ should "have max results if set" do
40
+ @report.max_results = 5
41
+ assert_equal({'max-results' => 5}, @report.page_params)
42
+ end
43
+
44
+ should "have an empty hash if max results not set" do
45
+ assert_equal({}, @report.page_params)
46
+ end
47
+
48
+ should "have a profile" do
49
+ assert_equal @profile, @report.profile
50
+ end
51
+
52
+ should "return default params with no options" do
53
+ Report.expects(:format_time).with(@report.start_date).returns('start')
54
+ Report.expects(:format_time).with(@report.end_date).returns('end')
55
+
56
+ params = {'ids' => 'ga:1234', 'start-date' => 'start', 'end-date' => 'end'}
57
+ assert_equal params, @report.default_params
58
+ end
59
+
60
+ should "combine parameters for request" do
61
+ @report.sort.expects(:to_params).returns({'sort' => 'value'})
62
+ @report.filters.expects(:to_params).returns({'filters' => 'value'})
63
+ @report.metrics.expects(:to_params).returns({'metrics' => 'value'})
64
+ @report.dimensions.expects(:to_params).returns({'dimensions' => 'value'})
65
+
66
+ @report.expects(:page_params).returns({})
67
+ @report.expects(:default_params).returns({'ids' => 'ga:1234'})
68
+
69
+ params = {'ids' => 'ga:1234', 'metrics' => 'value', 'dimensions' => 'value', 'sort' => 'value', 'filters' => 'value'}
70
+
71
+ assert_equal params, @report.params
72
+ end
73
+
74
+ should "send a request and get the response body" do
75
+ response = stub(:body => 'feed')
76
+ data_request = mock
77
+ data_request.expects(:send_request).with().returns(response)
78
+
79
+ @report.stubs(:params).returns({'key' => 'value'})
80
+ DataRequest.expects(:new).with(Report::URL, {'key' => 'value'}).returns(data_request)
81
+
82
+ assert_equal 'feed', @report.send_request_for_body
83
+ end
84
+
85
+ should "fetch and parse all entries" do
86
+ @report.expects(:send_request_for_body).with().returns('xml')
87
+ ReportResponse.expects(:new).with('xml').returns(mock(:parse => ['entry']))
88
+ assert_equal ['entry'], @report.all
89
+ end
90
+ end
91
+
92
+ context "The Report class" do
93
+
94
+ # should "get the value for a given entry and property" do
95
+ # # entry = mock do |m|
96
+ # # m.expects(:/).with().returns(['balding'])
97
+ # # end
98
+ # # assert_equal 'balding', Report.property_value(entry, Report.element_id(:balding))
99
+ # end
100
+ #
101
+ # should "get the values for a given entry and array of properties" do
102
+ # entry = stub
103
+ # Report.stubs(:property_value).with(entry, "balding").returns('balding')
104
+ # Report.stubs(:property_value).with(entry, "spaulding").returns('spaulding')
105
+ #
106
+ # data = Report.property_values(entry, [:balding, :spaulding])
107
+ # assert_equal 'balding', data.balding
108
+ # assert_equal 'spaulding', data.spaulding
109
+ # end
110
+ #
111
+ # should "return an ostruct for an entry" do
112
+ # entry = stub
113
+ # Report.stubs(:property_value).with(entry, "balding").returns('balding')
114
+ # assert_equal true, Report.property_values(entry, [:balding]).is_a?(OpenStruct)
115
+ # end
116
+
117
+ should "format time" do
118
+ t = Time.now
119
+ assert_equal t.strftime('%Y-%m-%d'), Report.format_time(t)
120
+ end
121
+ end
122
+
123
+ end
124
+ end
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ module Garb
4
+ class SessionTest < Test::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 "retain the email address for this session" do
17
+ AuthenticationRequest.stubs(:new).returns(stub(:auth_token => 'toke'))
18
+
19
+ Session.login('email', 'password')
20
+ assert_equal 'email', Session.email
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class SymbolTest < Test::Unit::TestCase
4
+
5
+ context "An instance of the Symbol class" do
6
+
7
+ should "properly format itself for ga" do
8
+ assert_equal "ga:requestUri", :request_uri.to_ga
9
+ end
10
+
11
+ should "define a :desc operator" do
12
+ operator = stub()
13
+ symbol = :foo
14
+
15
+ Operator.expects(:new).with(:foo, '-', true).returns(operator)
16
+ assert_equal operator, :foo.desc
17
+ end
18
+
19
+ def self.should_define_operator(operators)
20
+ operators.each do |method, operator|
21
+ should "define an :#{method} operator" do
22
+ new_operator = stub()
23
+ symbol = :foo
24
+
25
+ Operator.expects(:new).with(:foo, operator).returns(new_operator)
26
+ assert_equal new_operator, :foo.send(method)
27
+ end
28
+ end
29
+ end
30
+
31
+ should_define_operator :eql => '==',
32
+ :not_eql => '!=',
33
+ :gt => '>',
34
+ :gte => '>=',
35
+ :lt => '<',
36
+ :lte => '<=',
37
+ :matches => '==',
38
+ :does_not_match => '!=',
39
+ :contains => '=~',
40
+ :does_not_contain => '!~',
41
+ :substring => '=@',
42
+ :not_substring => '!@'
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'mocha'
4
+ require 'shoulda'
5
+ require 'garb'
6
+
7
+ class Test::Unit::TestCase
8
+
9
+ def read_fixture(filename)
10
+ File.read(File.dirname(__FILE__) + "/fixtures/#{filename}")
11
+ end
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vigetlabs-garb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Tony Pitale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: jnunemaker-happymapper
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: libxml-ruby
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.8
34
+ version:
35
+ description: A ruby gem to aid in the use of the Google Analytics API
36
+ email: tony.pitale@viget.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - History.txt
45
+ - README.md
46
+ - Rakefile
47
+ - garb.gemspec
48
+ - lib/garb.rb
49
+ - lib/garb/authentication_request.rb
50
+ - lib/garb/data_request.rb
51
+ - lib/garb/profile.rb
52
+ - lib/garb/report.rb
53
+ - lib/garb/report_parameter.rb
54
+ - lib/garb/report_response.rb
55
+ - lib/garb/session.rb
56
+ - lib/extensions/symbol.rb
57
+ - lib/extensions/string.rb
58
+ - lib/extensions/operator.rb
59
+ - lib/extensions/happymapper.rb
60
+ has_rdoc: false
61
+ homepage: http://github.com/vigetlabs/garb
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.2.0
83
+ signing_key:
84
+ specification_version: 2
85
+ summary: Google Analytics API Ruby Wrapper
86
+ test_files:
87
+ - test/authentication_request_test.rb
88
+ - test/data_request_test.rb
89
+ - test/garb_test.rb
90
+ - test/operator_test.rb
91
+ - test/profile_test.rb
92
+ - test/report_parameter_test.rb
93
+ - test/report_response_test.rb
94
+ - test/report_test.rb
95
+ - test/session_test.rb
96
+ - test/symbol_test.rb
97
+ - test/test_helper.rb
98
+ - test/fixtures/profile_feed.xml
99
+ - test/fixtures/report_feed.xml