garb-authsub 0.7.0
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 +250 -0
- data/Rakefile +56 -0
- data/lib/garb.rb +38 -0
- data/lib/garb/account.rb +29 -0
- data/lib/garb/authentication_request.rb +53 -0
- data/lib/garb/data_request.rb +51 -0
- data/lib/garb/filter_parameters.rb +37 -0
- data/lib/garb/profile.rb +57 -0
- data/lib/garb/profile_reports.rb +15 -0
- data/lib/garb/report.rb +26 -0
- data/lib/garb/report_parameter.rb +25 -0
- data/lib/garb/report_response.rb +62 -0
- data/lib/garb/reports.rb +5 -0
- data/lib/garb/reports/bounces.rb +5 -0
- data/lib/garb/reports/exits.rb +5 -0
- data/lib/garb/reports/pageviews.rb +5 -0
- data/lib/garb/reports/unique_pageviews.rb +5 -0
- data/lib/garb/reports/visits.rb +5 -0
- data/lib/garb/resource.rb +92 -0
- data/lib/garb/session.rb +29 -0
- data/lib/garb/version.rb +13 -0
- data/lib/support.rb +39 -0
- data/test/fixtures/cacert.pem +67 -0
- data/test/fixtures/profile_feed.xml +40 -0
- data/test/fixtures/report_feed.xml +46 -0
- data/test/test_helper.rb +18 -0
- data/test/unit/garb/account_test.rb +53 -0
- data/test/unit/garb/authentication_request_test.rb +121 -0
- data/test/unit/garb/data_request_test.rb +106 -0
- data/test/unit/garb/filter_parameters_test.rb +57 -0
- data/test/unit/garb/oauth_session_test.rb +11 -0
- data/test/unit/garb/profile_reports_test.rb +29 -0
- data/test/unit/garb/profile_test.rb +87 -0
- data/test/unit/garb/report_parameter_test.rb +43 -0
- data/test/unit/garb/report_response_test.rb +29 -0
- data/test/unit/garb/report_test.rb +91 -0
- data/test/unit/garb/resource_test.rb +38 -0
- data/test/unit/garb/session_test.rb +84 -0
- data/test/unit/garb_test.rb +14 -0
- data/test/unit/symbol_operator_test.rb +37 -0
- metadata +131 -0
data/README.md
ADDED
@@ -0,0 +1,250 @@
|
|
1
|
+
Garb
|
2
|
+
====
|
3
|
+
|
4
|
+
http://github.com/vigetlabs/garb
|
5
|
+
|
6
|
+
Important Changes
|
7
|
+
=================
|
8
|
+
|
9
|
+
Please read CHANGELOG
|
10
|
+
|
11
|
+
Description
|
12
|
+
-----------
|
13
|
+
|
14
|
+
Provides a Ruby API to the Google Analytics API.
|
15
|
+
|
16
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataDeveloperGuide.html
|
17
|
+
|
18
|
+
Basic Usage
|
19
|
+
===========
|
20
|
+
|
21
|
+
Single User Login
|
22
|
+
-----------------
|
23
|
+
|
24
|
+
> Garb::Session.login(username, password)
|
25
|
+
|
26
|
+
OAuth Access Token
|
27
|
+
------------------
|
28
|
+
|
29
|
+
> Garb::Session.access_token = access_token # assign from oauth gem
|
30
|
+
|
31
|
+
Accounts
|
32
|
+
--------
|
33
|
+
|
34
|
+
> Garb::Account.all
|
35
|
+
|
36
|
+
Profiles
|
37
|
+
--------
|
38
|
+
|
39
|
+
> Garb::Account.first.profiles
|
40
|
+
|
41
|
+
> Garb::Profile.first('UA-XXXX-XX')
|
42
|
+
|
43
|
+
> Garb::Profile.all
|
44
|
+
> profile = Garb::Profile.all.first
|
45
|
+
|
46
|
+
Define a Report Class
|
47
|
+
---------------------
|
48
|
+
|
49
|
+
class Exits
|
50
|
+
extend Garb::Resource
|
51
|
+
|
52
|
+
metrics :exits, :pageviews, :exit_rate
|
53
|
+
dimensions :page_path
|
54
|
+
sort :exits
|
55
|
+
|
56
|
+
filters do
|
57
|
+
eql(:page_path, 'season')
|
58
|
+
end
|
59
|
+
|
60
|
+
# alternative:
|
61
|
+
# filters :page_path.eql => 10
|
62
|
+
end
|
63
|
+
|
64
|
+
Get the Results
|
65
|
+
---------------
|
66
|
+
|
67
|
+
> Exits.results(profile)
|
68
|
+
|
69
|
+
OR shorthand
|
70
|
+
|
71
|
+
> profile.exits
|
72
|
+
|
73
|
+
Other Parameters
|
74
|
+
----------------
|
75
|
+
|
76
|
+
* start_date: The date of the period you would like this report to start
|
77
|
+
* end_date: The date to end, inclusive
|
78
|
+
* limit: The maximum number of results to be returned
|
79
|
+
* offset: The starting index
|
80
|
+
|
81
|
+
Metrics & Dimensions
|
82
|
+
--------------------
|
83
|
+
|
84
|
+
Metrics and Dimensions are very complex because of the ways in which the can and cannot be combined.
|
85
|
+
|
86
|
+
I suggest reading the google documentation to familiarize yourself with this.
|
87
|
+
|
88
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html#bounceRate
|
89
|
+
|
90
|
+
When you've returned, you can pass the appropriate combinations (up to 50 metrics and 2 dimenstions)
|
91
|
+
to garb, as an array, of symbols. Or you can simply push a symbol into the array.
|
92
|
+
|
93
|
+
Sorting
|
94
|
+
-------
|
95
|
+
|
96
|
+
Sorting can be done on any metric or dimension defined in the request, with .desc reversing the sort.
|
97
|
+
|
98
|
+
Building a Report
|
99
|
+
-----------------
|
100
|
+
|
101
|
+
Given the class, session, and profile from above we can do:
|
102
|
+
|
103
|
+
Exits.results(profile, :limit => 10, :offset => 19)
|
104
|
+
|
105
|
+
Or, with sorting and filters:
|
106
|
+
|
107
|
+
Exits.results(profile, :limit => 10, :offset => 19) do
|
108
|
+
sort :exits
|
109
|
+
|
110
|
+
filters do
|
111
|
+
contains(:page_path, 'season')
|
112
|
+
gt(:exits, 100)
|
113
|
+
end
|
114
|
+
|
115
|
+
# or with a hash
|
116
|
+
# filters :page_path.contains => 'season', :exits.gt => 100
|
117
|
+
end
|
118
|
+
|
119
|
+
reports will be an array of OpenStructs with methods for the metrics and dimensions returned.
|
120
|
+
|
121
|
+
Build a One-Off Report
|
122
|
+
----------------------
|
123
|
+
|
124
|
+
report = Garb::Report.new(profile)
|
125
|
+
report.metrics :pageviews, :exits
|
126
|
+
report.dimensions :page_path
|
127
|
+
report.sort :exits
|
128
|
+
|
129
|
+
report.filters do
|
130
|
+
contains(:page_path, 'season')
|
131
|
+
gte(:exits, 10)
|
132
|
+
and
|
133
|
+
|
134
|
+
# or with a hash
|
135
|
+
# report.filters :page_path.contains => 'season', :exits.gt => 100
|
136
|
+
|
137
|
+
report.results
|
138
|
+
|
139
|
+
Filtering
|
140
|
+
---------
|
141
|
+
|
142
|
+
Google Analytics supports a significant number of filtering options.
|
143
|
+
|
144
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataReference.html#filtering
|
145
|
+
|
146
|
+
We handle filtering as an array of hashes that you can push into,
|
147
|
+
which will be joined together (AND'd)
|
148
|
+
|
149
|
+
Here is what we can do currently:
|
150
|
+
(the operator is a method on a symbol metric or dimension)
|
151
|
+
|
152
|
+
Operators on metrics:
|
153
|
+
|
154
|
+
eql => '==',
|
155
|
+
not_eql => '!=',
|
156
|
+
gt => '>',
|
157
|
+
gte => '>=',
|
158
|
+
lt => '<',
|
159
|
+
lte => '<='
|
160
|
+
|
161
|
+
Operators on dimensions:
|
162
|
+
|
163
|
+
matches => '==',
|
164
|
+
does_not_match => '!=',
|
165
|
+
contains => '=~',
|
166
|
+
does_not_contain => '!~',
|
167
|
+
substring => '=@',
|
168
|
+
not_substring => '!@'
|
169
|
+
|
170
|
+
Given the previous example one-off report, we can add a line for filter:
|
171
|
+
|
172
|
+
report.filters do
|
173
|
+
eql(:page_path, '/extend/effectively-using-git-with-subversion/')
|
174
|
+
end
|
175
|
+
|
176
|
+
Or, if you're comfortable using symbol operators:
|
177
|
+
|
178
|
+
report.filters :page_path.eql => '/extend/effectively-using-git-with-subversion/'
|
179
|
+
|
180
|
+
SSL
|
181
|
+
---
|
182
|
+
|
183
|
+
Version 0.2.3 includes support for real ssl encryption for SINGLE USER authentication. First do:
|
184
|
+
|
185
|
+
Garb::Session.login(username, password, :secure => true)
|
186
|
+
|
187
|
+
Next, be sure to download http://curl.haxx.se/ca/cacert.pem into your application somewhere.
|
188
|
+
Then, define a constant CA_CERT_FILE and point to that file.
|
189
|
+
|
190
|
+
For whatever reason, simply creating a new certificate store and setting the defaults would
|
191
|
+
not validate the google ssl certificate as authentic.
|
192
|
+
|
193
|
+
TODOS
|
194
|
+
-----
|
195
|
+
|
196
|
+
* Read opensearch header in results
|
197
|
+
* Investigate new features from GA to see if they're in the API, implement if so
|
198
|
+
* clarify AND/OR filtering behavior in code and documentation
|
199
|
+
|
200
|
+
Requirements
|
201
|
+
------------
|
202
|
+
|
203
|
+
* happymapper >= 0.3.0 (should also install libxml)
|
204
|
+
* active_support >= 2.3.0
|
205
|
+
|
206
|
+
Requirements for Testing
|
207
|
+
------------------------
|
208
|
+
|
209
|
+
* jferris-mocha
|
210
|
+
* tpitale-shoulda (works with minitest)
|
211
|
+
|
212
|
+
Install
|
213
|
+
-------
|
214
|
+
|
215
|
+
sudo gem install garb
|
216
|
+
|
217
|
+
Contributors
|
218
|
+
------------
|
219
|
+
|
220
|
+
Many Thanks, for all their help, goes to:
|
221
|
+
|
222
|
+
* Patrick Reagan
|
223
|
+
* Justin Marney
|
224
|
+
* Nick Plante
|
225
|
+
|
226
|
+
License
|
227
|
+
-------
|
228
|
+
|
229
|
+
(The MIT License)
|
230
|
+
|
231
|
+
Copyright (c) 2010 Viget Labs
|
232
|
+
|
233
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
234
|
+
a copy of this software and associated documentation files (the
|
235
|
+
'Software'), to deal in the Software without restriction, including
|
236
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
237
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
238
|
+
permit persons to whom the Software is furnished to do so, subject to
|
239
|
+
the following conditions:
|
240
|
+
|
241
|
+
The above copyright notice and this permission notice shall be
|
242
|
+
included in all copies or substantial portions of the Software.
|
243
|
+
|
244
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
245
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
246
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
247
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
248
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
249
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
250
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
require 'lib/garb/version'
|
6
|
+
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
spec = Gem::Specification.new do |s|
|
10
|
+
s.name = 'garb'
|
11
|
+
s.version = Garb::Version.to_s
|
12
|
+
s.has_rdoc = false
|
13
|
+
s.rubyforge_project = 'viget'
|
14
|
+
s.summary = "Google Analytics API Ruby Wrapper"
|
15
|
+
s.authors = ['Tony Pitale']
|
16
|
+
s.email = 'tony.pitale@viget.com'
|
17
|
+
s.homepage = 'http://github.com/vigetlabs/garb'
|
18
|
+
s.files = %w(README.md Rakefile) + Dir.glob("lib/**/*")
|
19
|
+
s.test_files = Dir.glob("test/**/*")
|
20
|
+
|
21
|
+
s.add_dependency("happymapper", ["~> 0.3.0"])
|
22
|
+
s.add_dependency("activesupport", [">= 2.3.0"])
|
23
|
+
end
|
24
|
+
|
25
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
26
|
+
pkg.gem_spec = spec
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::TestTask.new do |t|
|
30
|
+
t.libs << 'test'
|
31
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
32
|
+
t.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
desc 'Generate the gemspec to serve this Gem from Github'
|
36
|
+
task :github do
|
37
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
38
|
+
File.open(file, 'w') {|f| f << spec.to_ruby }
|
39
|
+
puts "Created gemspec: #{file}"
|
40
|
+
end
|
41
|
+
|
42
|
+
begin
|
43
|
+
require 'rcov/rcovtask'
|
44
|
+
|
45
|
+
desc "Generate RCov coverage report"
|
46
|
+
Rcov::RcovTask.new(:rcov) do |t|
|
47
|
+
t.test_files = FileList['test/**/*_test.rb']
|
48
|
+
t.rcov_opts << "-x lib/garb.rb -x lib/garb/version.rb"
|
49
|
+
end
|
50
|
+
rescue LoadError
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
|
54
|
+
task :default => 'test'
|
55
|
+
|
56
|
+
# EOF
|
data/lib/garb.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
|
6
|
+
require 'cgi'
|
7
|
+
require 'ostruct'
|
8
|
+
require 'happymapper'
|
9
|
+
require 'active_support'
|
10
|
+
|
11
|
+
require 'garb/version'
|
12
|
+
require 'garb/authentication_request'
|
13
|
+
require 'garb/data_request'
|
14
|
+
require 'garb/session'
|
15
|
+
require 'garb/profile_reports'
|
16
|
+
require 'garb/profile'
|
17
|
+
require 'garb/account'
|
18
|
+
require 'garb/filter_parameters'
|
19
|
+
require 'garb/report_parameter'
|
20
|
+
require 'garb/report_response'
|
21
|
+
require 'garb/resource'
|
22
|
+
require 'garb/report'
|
23
|
+
|
24
|
+
require 'support'
|
25
|
+
|
26
|
+
module Garb
|
27
|
+
GA = "http://schemas.google.com/analytics/2008"
|
28
|
+
|
29
|
+
def self.to_google_analytics(thing)
|
30
|
+
return thing.to_google_analytics if thing.respond_to?(:to_google_analytics)
|
31
|
+
|
32
|
+
"ga:#{thing.to_s.camelize(:lower)}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.from_google_analytics(thing)
|
36
|
+
thing.to_s.gsub(/^ga\:/, '').underscore
|
37
|
+
end
|
38
|
+
end
|
data/lib/garb/account.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Garb
|
2
|
+
class Account
|
3
|
+
attr_reader :id, :name, :profiles
|
4
|
+
|
5
|
+
def initialize(profiles)
|
6
|
+
@id = profiles.first.account_id
|
7
|
+
@name = profiles.first.account_name
|
8
|
+
@profiles = profiles
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.all(session = Session)
|
12
|
+
# Profile.all.group_to_array{|p| p.account_id}.map{|profiles| new(profiles)}
|
13
|
+
|
14
|
+
profile_groups = Profile.all(session).inject({}) do |hash, profile|
|
15
|
+
key = profile.account_id
|
16
|
+
|
17
|
+
if hash.has_key?(key)
|
18
|
+
hash[key] << profile
|
19
|
+
else
|
20
|
+
hash[key] = [profile]
|
21
|
+
end
|
22
|
+
|
23
|
+
hash
|
24
|
+
end
|
25
|
+
|
26
|
+
profile_groups.map {|k,v| v}.map {|profiles| new(profiles)}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Garb
|
2
|
+
class AuthenticationRequest
|
3
|
+
class AuthError < StandardError;end
|
4
|
+
|
5
|
+
URL = 'https://www.google.com/accounts/ClientLogin'
|
6
|
+
|
7
|
+
def initialize(email, password, opts={})
|
8
|
+
@email = email
|
9
|
+
@password = password
|
10
|
+
@account_type = opts.fetch(:account_type, 'HOSTED_OR_GOOGLE')
|
11
|
+
end
|
12
|
+
|
13
|
+
def parameters
|
14
|
+
{
|
15
|
+
'Email' => @email,
|
16
|
+
'Passwd' => @password,
|
17
|
+
'accountType' => @account_type,
|
18
|
+
'service' => 'analytics',
|
19
|
+
'source' => 'vigetLabs-garb-001'
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def uri
|
24
|
+
URI.parse(URL)
|
25
|
+
end
|
26
|
+
|
27
|
+
def send_request(ssl_mode)
|
28
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
29
|
+
http.use_ssl = true
|
30
|
+
http.verify_mode = ssl_mode
|
31
|
+
|
32
|
+
if ssl_mode == OpenSSL::SSL::VERIFY_PEER
|
33
|
+
http.ca_file = CA_CERT_FILE
|
34
|
+
end
|
35
|
+
|
36
|
+
http.request(build_request) do |response|
|
37
|
+
raise AuthError unless response.is_a?(Net::HTTPOK)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_request
|
42
|
+
post = Net::HTTP::Post.new(uri.path)
|
43
|
+
post.set_form_data(parameters)
|
44
|
+
post
|
45
|
+
end
|
46
|
+
|
47
|
+
def auth_token(opts={})
|
48
|
+
ssl_mode = opts[:secure] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
|
49
|
+
send_request(ssl_mode).body.match(/^Auth=(.*)$/)[1]
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Garb
|
2
|
+
class DataRequest
|
3
|
+
class ClientError < StandardError; end
|
4
|
+
|
5
|
+
def initialize(session, base_url, parameters={})
|
6
|
+
@session = session
|
7
|
+
@base_url = base_url
|
8
|
+
@parameters = parameters
|
9
|
+
end
|
10
|
+
|
11
|
+
def query_string
|
12
|
+
parameter_list = @parameters.map {|k,v| "#{k}=#{v}" }
|
13
|
+
parameter_list.empty? ? '' : "?#{parameter_list.join('&')}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def uri
|
17
|
+
URI.parse(@base_url)
|
18
|
+
end
|
19
|
+
|
20
|
+
def send_request
|
21
|
+
response = if @session.single_user?
|
22
|
+
single_user_request
|
23
|
+
elsif @session.oauth_user?
|
24
|
+
oauth_user_request
|
25
|
+
elsif @session.authsub_user?
|
26
|
+
authsub_user_request
|
27
|
+
end
|
28
|
+
|
29
|
+
raise ClientError, response.body.inspect unless response.kind_of?(Net::HTTPSuccess)
|
30
|
+
response
|
31
|
+
end
|
32
|
+
|
33
|
+
def single_user_request
|
34
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
35
|
+
http.use_ssl = true
|
36
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
37
|
+
http.get("#{uri.path}#{query_string}", 'Authorization' => "GoogleLogin auth=#{@session.auth_token}")
|
38
|
+
end
|
39
|
+
|
40
|
+
def oauth_user_request
|
41
|
+
@session.access_token.get("#{uri}#{query_string}")
|
42
|
+
end
|
43
|
+
|
44
|
+
def authsub_user_request
|
45
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
46
|
+
http.use_ssl = true
|
47
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
48
|
+
http.get("#{uri.path}#{query_string}", 'Authorization' => "AuthSub token=#{@session.authsub_token}")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|