songphi-gattica 0.5.3

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 = 300
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 => 'seerinteractive@gmail.com',
5
+ :password => '4461goal',
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,12 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class TestAuth < Test::Unit::TestCase
4
+ def setup
5
+
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+
12
+ end
@@ -0,0 +1,32 @@
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), "should have been able to login"
8
+ end
9
+ #
10
+ # def test_low_timeout
11
+ # ga = Gattica.new(GatticaTest::DEFAULT_AUTH.merge!({ :debug =>false, :timeout => 1 }))
12
+ # puts ga.accounts.inspect
13
+ # end
14
+
15
+ def test_login_with_bad_user_password
16
+ assert_raise GatticaError::CouldNotAuthenticate do
17
+ Gattica.new({ :email => 'bad-email@gmail.com', :password => 'bad-password'})
18
+ end
19
+ end
20
+
21
+ def test_raise_error_when_no_user_pass_or_token_specified
22
+ assert_raise GatticaError::NoLoginOrToken do
23
+ Gattica.new()
24
+ end
25
+ end
26
+ #
27
+ # def test_use_an_existing_token
28
+ # token = Gattica.new(@auth).token
29
+ # assert Gattica.new({ :token => token })
30
+ # end
31
+
32
+ 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,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: songphi-gattica
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 3
10
+ version: 0.5.3
11
+ platform: ruby
12
+ authors:
13
+ - Christopher Le, et all
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-05 00:00:00 +07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rake
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: hpricot
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ 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
50
+ email: thaolt@songphi.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.md
58
+ files:
59
+ - .rvmrc
60
+ - Gemfile
61
+ - LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - VERSION.yml
65
+ - gattica.gemspec
66
+ - lib/gattica.rb
67
+ - lib/gattica/account.rb
68
+ - lib/gattica/auth.rb
69
+ - lib/gattica/convertible.rb
70
+ - lib/gattica/data_point.rb
71
+ - lib/gattica/data_set.rb
72
+ - lib/gattica/engine.rb
73
+ - lib/gattica/exceptions.rb
74
+ - lib/gattica/hash_extensions.rb
75
+ - lib/gattica/segment.rb
76
+ - lib/gattica/settings.rb
77
+ - lib/gattica/user.rb
78
+ - test/helper.rb
79
+ - test/settings.rb
80
+ - test/suite.rb
81
+ - test/test_auth.rb
82
+ - test/test_engine.rb
83
+ - test/test_results.rb
84
+ - test/test_user.rb
85
+ has_rdoc: true
86
+ homepage: http://github.com/chrisle/gattica
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ requirements: []
113
+
114
+ rubyforge_project:
115
+ rubygems_version: 1.3.7
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Gattica is a easy to use Ruby Gem for getting data from the Google Analytics API.
119
+ test_files: []
120
+