betapond-gattica 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ module GatticaError
2
+ # user errors
3
+ class InvalidEmail < StandardError; end;
4
+ class InvalidPassword < StandardError; end;
5
+ # authentication errors
6
+ class CouldNotAuthenticate < StandardError; end;
7
+ class NoLoginOrToken < StandardError; end;
8
+ class InvalidToken < StandardError; end;
9
+ # profile errors
10
+ class InvalidProfileId < StandardError; end;
11
+ # search errors
12
+ class TooManyDimensions < StandardError; end;
13
+ class TooManyMetrics < StandardError; end;
14
+ class InvalidSort < StandardError; end;
15
+ class InvalidFilter < StandardError; end;
16
+ class MissingStartDate < StandardError; end;
17
+ class MissingEndDate < StandardError; end;
18
+ # errors from Analytics
19
+ class AnalyticsError < StandardError; end;
20
+ class UnknownAnalyticsError < StandardError; end;
21
+ end
@@ -0,0 +1,20 @@
1
+ module Gattica
2
+ module HashExtensions
3
+
4
+ def to_query
5
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
6
+ self.collect do |key, value|
7
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
8
+ end.sort * '&'
9
+ end
10
+
11
+ def key
12
+ self.keys.first if self.length == 1
13
+ end
14
+
15
+ def value
16
+ self.values.first if self.length == 1
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'hpricot'
3
+
4
+ module Gattica
5
+ class Segment
6
+ include Convertible
7
+
8
+ attr_reader :id, :name, :definition
9
+
10
+ def initialize(xml)
11
+ @id = xml.attributes['id']
12
+ @name = xml.attributes['name']
13
+ @definition = xml.at("dxp:definition").inner_html
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ module Gattica
2
+ module Settings
3
+
4
+ # Google server to connect to
5
+ #
6
+ # You may need to change this if you use a proxy server. However, I have not
7
+ # tested using a proxy.... YMMV
8
+ SERVER = 'www.google.com'
9
+
10
+ # Use a secure connection?
11
+ #
12
+ # If set to true, Gattica will use SSL_PORT or else it will use NON_SSL_PORT.
13
+ #
14
+ # === Example:
15
+ # +USE_SSL = true+:: # Always use a secure connection
16
+ USE_SSL = true
17
+ SSL_PORT = 443
18
+ NON_SSL_PORT = 80
19
+
20
+ # Net::HTTP timeout in seconds
21
+ #
22
+ # Increase the timeout setting if you have over 100 profiles.
23
+ #
24
+ # === Example:
25
+ # +TIMEOUT = 300+:: # Increase timeout to 5 minutes.
26
+ TIMEOUT = 100
27
+
28
+ DEFAULT_ARGS = {
29
+ :start_date => nil,
30
+ :end_date => nil,
31
+ :dimensions => [],
32
+ :metrics => [],
33
+ :filters => [],
34
+ :sort => []
35
+ }
36
+
37
+ DEFAULT_OPTIONS = {
38
+ :email => nil, # eg: 'email@gmail.com'
39
+ :password => nil, # eg: '$up3r_$ekret'
40
+ :token => nil,
41
+ :profile_id => nil,
42
+ :debug => false,
43
+ :headers => {},
44
+ :logger => Logger.new(STDOUT)
45
+ }
46
+
47
+ FILTER_METRIC_OPERATORS = %w{ == != > < >= <= }
48
+ FILTER_DIMENSION_OPERATORS = %w{ == != =~ !~ =@ ~@ }
49
+
50
+ end
51
+ end
@@ -0,0 +1,31 @@
1
+ module Gattica
2
+
3
+ # Represents a user to be authenticated by GA
4
+
5
+ class User
6
+
7
+ include Convertible
8
+
9
+ attr_accessor :email, :password
10
+
11
+ def initialize(email,password)
12
+ @email = email
13
+ @password = password
14
+ validate
15
+ end
16
+
17
+ # User gets a special +to_h+ because Google expects +Email+ and +Passwd+ instead of our nicer internal names
18
+ def to_h
19
+ { :Email => @email,
20
+ :Passwd => @password }
21
+ end
22
+
23
+ private
24
+ # Determine whether or not this is a valid user
25
+ def validate
26
+ raise GatticaError::InvalidEmail, "The email address '#{@email}' is not valid" if not @email.match(/^(?:[_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-zA-Z0-9\-\.]+)*(\.[a-z]{2,4})$/i)
27
+ raise GatticaError::InvalidPassword, "The password cannot be blank" if @password.empty? || @password.nil?
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib gattica])
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+
6
+ # include Gattica
7
+
8
+ def fixture(name)
9
+ File.read(File.join(File.dirname(__FILE__), 'fixtures', name))
10
+ end
11
+
12
+ def absolute_project_path
13
+ File.expand_path(File.join(File.dirname(__FILE__), '..'))
14
+ end
@@ -0,0 +1,28 @@
1
+ module GatticaTest
2
+
3
+ DEFAULT_AUTH = {
4
+ :email => 'name@email.com',
5
+ :password => 'password',
6
+ :debug => true
7
+ }
8
+ DEFAULT_QUERY = {
9
+ :start_date => '2010-01-01',
10
+ :end_date => '2011-01-01',
11
+ :dimensions => ['date'],
12
+ :metrics => ['visits']
13
+ }
14
+ PROFILE_ID = 23987717
15
+
16
+ def self.ga(options={}, profile_id=PROFILE_ID)
17
+ unless defined? @ga
18
+ @ga = Gattica.new(DEFAULT_AUTH)
19
+ @ga.profile_id = profile_id
20
+ end
21
+ @ga
22
+ end
23
+
24
+ def self.get(options={}, profile_id=PROFILE_ID)
25
+ ga.get(DEFAULT_QUERY.merge(options))
26
+ end
27
+
28
+ end
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+
3
+ tests = Dir["#{File.dirname(__FILE__)}/test_*.rb"]
4
+ tests.each do |file|
5
+ require file
6
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+ require File.expand_path('../settings', __FILE__)
3
+
4
+ class TestEngine < Test::Unit::TestCase
5
+
6
+ def test_login_with_good_password
7
+ assert Gattica.new(GatticaTest::DEFAULT_AUTH),
8
+ "should have been able to login"
9
+ end
10
+
11
+ def test_login_with_bad_user_password
12
+ assert_raise GatticaError::CouldNotAuthenticate do
13
+ Gattica.new({ :email => 'bad-email@gmail.com',
14
+ :password => 'bad-password' })
15
+ end
16
+ end
17
+
18
+ def test_accounts
19
+ ga = Gattica.new(GatticaTest::DEFAULT_AUTH)
20
+ accounts = ga.accounts
21
+ assert accounts.count > 900, "should have gotten at least 900 accounts"
22
+ end
23
+
24
+ # def test_timeout_too_short
25
+ # ga = Gattica.new(GatticaTest::DEFAULT_AUTH.merge!(:timeout => 0))
26
+ # assert_raise Timeout::Error do
27
+ # ga.accounts
28
+ # end
29
+ # end
30
+
31
+ def test_setting_timeout
32
+ ga = Gattica.new((GatticaTest::DEFAULT_AUTH).merge!(:timeout => 300))
33
+ http = ga.instance_variable_get('@http')
34
+ assert http.read_timeout == 300, "http timeout should be 300"
35
+ end
36
+
37
+ # def test_raise_error_when_no_user_pass_or_token_specified
38
+ # assert_raise GatticaError::NoLoginOrToken do
39
+ # Gattica.new
40
+ # end
41
+ # end
42
+ #
43
+ # def test_use_an_existing_token
44
+ # token = Gattica.new(@auth).token
45
+ # assert Gattica.new({ :token => token })
46
+ # end
47
+
48
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+ require File.expand_path('../settings', __FILE__)
3
+
4
+ class TestResults < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @response = GatticaTest::get({ :start_index => 5, :max_results => 5 })
8
+ end
9
+
10
+ def test_max_results
11
+ assert @response.points.count == 5, "should only return 5 results"
12
+ end
13
+
14
+ def test_start_index
15
+ assert @response.points.first.title == "ga:date=20100105", "should start on the 5th"
16
+ end
17
+
18
+ def test_conversions
19
+ assert @response.class.inspect == 'Gattica::DataSet', "should be a Gattica:DataSet"
20
+ assert @response.to_h.class.inspect == 'Hash', "Should be a hash"
21
+ end
22
+
23
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class TestUser < Test::Unit::TestCase
4
+ def setup
5
+
6
+ end
7
+
8
+ def test_can_create_user
9
+ assert Gattica::User.new('anonymous@anon.com','none')
10
+ end
11
+
12
+ def test_invalid_email
13
+ assert_raise GatticaError::InvalidEmail do Gattica::User.new('','') end
14
+ assert_raise ArgumentError do Gattica::User.new('') end
15
+ assert_raise GatticaError::InvalidEmail do Gattica::User.new('anonymous','none') end
16
+ assert_raise GatticaError::InvalidEmail do Gattica::User.new('anonymous@asdfcom','none') end
17
+ end
18
+
19
+ def test_invalid_password
20
+ assert_raise GatticaError::InvalidPassword do Gattica::User.new('anonymous@anon.com','') end
21
+ assert_raise ArgumentError do Gattica::User.new('anonymous@anon.com') end
22
+ end
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: betapond-gattica
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.5.1
6
+ platform: ruby
7
+ authors:
8
+ - Christopher Le, et all
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-06 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: gattica
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: test-unit
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rake
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: hpricot
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :runtime
59
+ version_requirements: *id004
60
+ description: Gattica is a easy to use Ruby Gem for getting data from the Google Analytics API. It supports metrics, dimensions, sort, filters, goals, and segments. It can handle accounts with 1000+ profiles, and can return data in CSV, Hash, or JSON
61
+ email: chrisl@seerinteractive.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE
68
+ - README.md
69
+ files:
70
+ - .rvmrc
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - VERSION.yml
76
+ - gattica.gemspec
77
+ - lib/gattica.rb
78
+ - lib/gattica/account.rb
79
+ - lib/gattica/auth.rb
80
+ - lib/gattica/convertible.rb
81
+ - lib/gattica/data_point.rb
82
+ - lib/gattica/data_set.rb
83
+ - lib/gattica/engine.rb
84
+ - lib/gattica/exceptions.rb
85
+ - lib/gattica/hash_extensions.rb
86
+ - lib/gattica/segment.rb
87
+ - lib/gattica/settings.rb
88
+ - lib/gattica/user.rb
89
+ - test/helper.rb
90
+ - test/settings.rb
91
+ - test/suite.rb
92
+ - test/test_engine.rb
93
+ - test/test_results.rb
94
+ - test/test_user.rb
95
+ has_rdoc: true
96
+ homepage: http://github.com/chrisle/gattica
97
+ licenses: []
98
+
99
+ post_install_message:
100
+ rdoc_options: []
101
+
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "0"
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.6.2
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Gattica is a easy to use Ruby Gem for getting data from the Google Analytics API.
123
+ test_files: []
124
+