garb 0.2.1
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 +178 -0
- data/Rakefile +54 -0
- data/lib/extensions/happymapper.rb +67 -0
- data/lib/extensions/operator.rb +20 -0
- data/lib/extensions/string.rb +13 -0
- data/lib/extensions/symbol.rb +36 -0
- data/lib/garb.rb +70 -0
- data/lib/garb/authentication_request.rb +46 -0
- data/lib/garb/data_request.rb +28 -0
- data/lib/garb/oauth_session.rb +21 -0
- data/lib/garb/profile.rb +45 -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/profile_feed.xml +33 -0
- data/test/fixtures/report_feed.xml +46 -0
- data/test/test_helper.rb +16 -0
- data/test/unit/authentication_request_test.rb +91 -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 +58 -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 +26 -0
- data/test/unit/string_test.rb +9 -0
- data/test/unit/symbol_test.rb +44 -0
- metadata +99 -0
data/README.md
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
garb
|
2
|
+
====
|
3
|
+
|
4
|
+
by Tony Pitale with much help from Justin Marney, Patrick Reagan and others at Viget Labs
|
5
|
+
|
6
|
+
http://github.com/vigetlabs/garb
|
7
|
+
|
8
|
+
Changes
|
9
|
+
=======
|
10
|
+
|
11
|
+
Version 0.2.0 makes major changes to the way garb is used to build reports.
|
12
|
+
There is now both a module that gets included for generating defined classes.
|
13
|
+
As well as, slight changes to the way that the Report class can be used.
|
14
|
+
|
15
|
+
Description
|
16
|
+
-----------
|
17
|
+
|
18
|
+
Provides a Ruby API to the Google Analytics API.
|
19
|
+
|
20
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataDeveloperGuide.html
|
21
|
+
|
22
|
+
Basic Usage
|
23
|
+
===========
|
24
|
+
|
25
|
+
Login
|
26
|
+
-----
|
27
|
+
|
28
|
+
> Garb::Session.login(username, password)
|
29
|
+
|
30
|
+
Profiles
|
31
|
+
--------
|
32
|
+
|
33
|
+
> Garb::Profile.all
|
34
|
+
> profile = Garb::Profile.all.first
|
35
|
+
|
36
|
+
Define a Report Class and Get Results
|
37
|
+
-------------------------------------
|
38
|
+
|
39
|
+
class Exits
|
40
|
+
include Garb::Resource
|
41
|
+
|
42
|
+
metrics :exits, :pageviews, :exit_rate
|
43
|
+
dimensions :request_uri
|
44
|
+
end
|
45
|
+
|
46
|
+
Parameters
|
47
|
+
----------
|
48
|
+
|
49
|
+
* start_date: The date of the period you would like this report to start
|
50
|
+
* end_date: The date to end, inclusive
|
51
|
+
* limit: The maximum number of results to be returned
|
52
|
+
* offset: The starting index
|
53
|
+
|
54
|
+
Metrics & Dimensions
|
55
|
+
--------------------
|
56
|
+
|
57
|
+
Metrics and Dimensions are very complex because of the ways in which the can and cannot be combined.
|
58
|
+
|
59
|
+
I suggest reading the google documentation to familiarize yourself with this.
|
60
|
+
|
61
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html#bounceRate
|
62
|
+
|
63
|
+
When you've returned, you can pass the appropriate combinations (up to 50 metrics and 2 dimenstions)
|
64
|
+
to garb, as an array, of symbols. Or you can simply push a symbol into the array.
|
65
|
+
|
66
|
+
Sorting
|
67
|
+
-------
|
68
|
+
|
69
|
+
Sorting can be done on any metric or dimension defined in the request, with .desc reversing the sort.
|
70
|
+
|
71
|
+
Building a Report
|
72
|
+
-----------------
|
73
|
+
|
74
|
+
Given the class, session, and profile from above we can do:
|
75
|
+
|
76
|
+
Exits.results(profile, :limit => 10, :offset => 19)
|
77
|
+
|
78
|
+
Or, with sorting and filters:
|
79
|
+
|
80
|
+
Exits.results(profile, :limit => 10, :offset => 19) do
|
81
|
+
filter :request_uri.contains => 'season', :exits.gt => 100
|
82
|
+
sort :exits
|
83
|
+
end
|
84
|
+
|
85
|
+
reports will be an array of OpenStructs with methods for the metrics and dimensions returned.
|
86
|
+
|
87
|
+
Build a One-Off Report
|
88
|
+
----------------------
|
89
|
+
|
90
|
+
report = Garb::Report.new(profile)
|
91
|
+
report.metrics :pageviews
|
92
|
+
report.dimensions :request_uri
|
93
|
+
|
94
|
+
report.filter :request_uri.contains => 'season', :exits.gte => 10
|
95
|
+
report.sort :exits
|
96
|
+
|
97
|
+
report.results
|
98
|
+
|
99
|
+
Filtering
|
100
|
+
---------
|
101
|
+
|
102
|
+
Google Analytics supports a significant number of filtering options.
|
103
|
+
|
104
|
+
http://code.google.com/apis/analytics/docs/gdata/gdataReference.html#filtering
|
105
|
+
|
106
|
+
We handle filtering as an array of hashes that you can push into,
|
107
|
+
which will be joined together (AND'd)
|
108
|
+
|
109
|
+
Here is what we can do currently:
|
110
|
+
(the operator is a method on a symbol metric or dimension)
|
111
|
+
|
112
|
+
Operators on metrics:
|
113
|
+
|
114
|
+
:eql => '==',
|
115
|
+
:not_eql => '!=',
|
116
|
+
:gt => '>',
|
117
|
+
:gte => '>=',
|
118
|
+
:lt => '<',
|
119
|
+
:lte => '<='
|
120
|
+
|
121
|
+
Operators on dimensions:
|
122
|
+
|
123
|
+
:matches => '==',
|
124
|
+
:does_not_match => '!=',
|
125
|
+
:contains => '=~',
|
126
|
+
:does_not_contain => '!~',
|
127
|
+
:substring => '=@',
|
128
|
+
:not_substring => '!@'
|
129
|
+
|
130
|
+
Given the previous example one-off report, we can add a line for filter:
|
131
|
+
|
132
|
+
report.filters << {:request_uri.eql => '/extend/effectively-using-git-with-subversion/'}
|
133
|
+
|
134
|
+
TODOS
|
135
|
+
-----
|
136
|
+
|
137
|
+
* Sessions are currently global, which isn't awesome
|
138
|
+
* Single user login is the only supported method currently.
|
139
|
+
Intend to add hooks for using OAuth
|
140
|
+
* Read opensearch header in results
|
141
|
+
* OR joining filter parameters
|
142
|
+
|
143
|
+
Requirements
|
144
|
+
------------
|
145
|
+
|
146
|
+
libxml
|
147
|
+
happymapper
|
148
|
+
|
149
|
+
Install
|
150
|
+
-------
|
151
|
+
|
152
|
+
sudo gem install vigetlabs-garb -s http://gems.github.com
|
153
|
+
|
154
|
+
License
|
155
|
+
-------
|
156
|
+
|
157
|
+
(The MIT License)
|
158
|
+
|
159
|
+
Copyright (c) 2008 Viget Labs
|
160
|
+
|
161
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
162
|
+
a copy of this software and associated documentation files (the
|
163
|
+
'Software'), to deal in the Software without restriction, including
|
164
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
165
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
166
|
+
permit persons to whom the Software is furnished to do so, subject to
|
167
|
+
the following conditions:
|
168
|
+
|
169
|
+
The above copyright notice and this permission notice shall be
|
170
|
+
included in all copies or substantial portions of the Software.
|
171
|
+
|
172
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
173
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
174
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
175
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
176
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
177
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
178
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
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.summary = "Google Analytics API Ruby Wrapper"
|
14
|
+
s.authors = ['Tony Pitale','Justin Marney', 'Patrick Reagan']
|
15
|
+
s.email = 'tony.pitale@viget.com'
|
16
|
+
s.homepage = 'http://github.com/vigetlabs/garb'
|
17
|
+
s.files = %w(README.md Rakefile) + Dir.glob("lib/**/*")
|
18
|
+
s.test_files = Dir.glob("test/**/*")
|
19
|
+
|
20
|
+
s.add_dependency("jnunemaker-happymapper", [">= 0.2.2"])
|
21
|
+
end
|
22
|
+
|
23
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
24
|
+
pkg.gem_spec = spec
|
25
|
+
end
|
26
|
+
|
27
|
+
Rake::TestTask.new do |t|
|
28
|
+
t.libs << 'test'
|
29
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
30
|
+
t.verbose = true
|
31
|
+
end
|
32
|
+
|
33
|
+
desc 'Generate the gemspec to serve this Gem from Github'
|
34
|
+
task :github do
|
35
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
36
|
+
File.open(file, 'w') {|f| f << spec.to_ruby }
|
37
|
+
puts "Created gemspec: #{file}"
|
38
|
+
end
|
39
|
+
|
40
|
+
begin
|
41
|
+
require 'rcov/rcovtask'
|
42
|
+
|
43
|
+
desc "Generate RCov coverage report"
|
44
|
+
Rcov::RcovTask.new(:rcov) do |t|
|
45
|
+
t.test_files = FileList['test/**/*_test.rb']
|
46
|
+
t.rcov_opts << "-x lib/garb.rb -x lib/garb/version.rb"
|
47
|
+
end
|
48
|
+
rescue LoadError
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
52
|
+
task :default => 'test'
|
53
|
+
|
54
|
+
# EOF
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'libxml'
|
2
|
+
|
3
|
+
module HappyMapper
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
include LibXML
|
7
|
+
|
8
|
+
def parse(xml, options = {})
|
9
|
+
# locally scoped copy of namespace for this parse run
|
10
|
+
namespace = @namespace
|
11
|
+
|
12
|
+
if xml.is_a?(XML::Node)
|
13
|
+
node = xml
|
14
|
+
else
|
15
|
+
if xml.is_a?(XML::Document)
|
16
|
+
node = xml.root
|
17
|
+
else
|
18
|
+
node = XML::Parser.string(xml).parse.root
|
19
|
+
end
|
20
|
+
|
21
|
+
root = node.name == tag_name
|
22
|
+
end
|
23
|
+
|
24
|
+
# This is the entry point into the parsing pipeline, so the default
|
25
|
+
# namespace prefix registered here will propagate down
|
26
|
+
namespaces = node.namespaces
|
27
|
+
if namespaces && namespaces.default
|
28
|
+
already_assigned = namespaces.definitions.detect do |defn|
|
29
|
+
namespaces.default && namespaces.default.href == defn.href && defn.prefix
|
30
|
+
end
|
31
|
+
namespaces.default_prefix = DEFAULT_NS unless already_assigned
|
32
|
+
namespace ||= DEFAULT_NS
|
33
|
+
end
|
34
|
+
|
35
|
+
xpath = root ? '/' : './/'
|
36
|
+
xpath += "#{namespace}:" if namespace
|
37
|
+
xpath += tag_name
|
38
|
+
# puts "parse: #{xpath}"
|
39
|
+
|
40
|
+
nodes = node.find(xpath)
|
41
|
+
collection = nodes.collect do |n|
|
42
|
+
obj = new
|
43
|
+
|
44
|
+
attributes.each do |attr|
|
45
|
+
obj.send("#{attr.method_name}=",
|
46
|
+
attr.from_xml_node(n, namespace))
|
47
|
+
end
|
48
|
+
|
49
|
+
elements.each do |elem|
|
50
|
+
obj.send("#{elem.method_name}=",
|
51
|
+
elem.from_xml_node(n, namespace))
|
52
|
+
end
|
53
|
+
|
54
|
+
obj
|
55
|
+
end
|
56
|
+
|
57
|
+
# per http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Document.html#M000354
|
58
|
+
nodes = nil
|
59
|
+
|
60
|
+
if options[:single] || root
|
61
|
+
collection.first
|
62
|
+
else
|
63
|
+
collection
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Concept from dm-core
|
2
|
+
class Operator
|
3
|
+
attr_reader :target, :operator, :prefix
|
4
|
+
|
5
|
+
def initialize(target, operator, prefix=false)
|
6
|
+
@target = target.to_ga
|
7
|
+
@operator = operator
|
8
|
+
@prefix = prefix
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_ga
|
12
|
+
@prefix ? "#{operator}#{target}" : "#{target}#{operator}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(rhs)
|
16
|
+
target == rhs.target &&
|
17
|
+
operator == rhs.operator &&
|
18
|
+
prefix == rhs.prefix
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class Symbol
|
2
|
+
# OPERATORS
|
3
|
+
|
4
|
+
def self.operator(operators)
|
5
|
+
operators.each do |method, operator|
|
6
|
+
class_eval <<-CODE
|
7
|
+
def #{method}
|
8
|
+
Operator.new(self, '#{operator}')
|
9
|
+
end
|
10
|
+
CODE
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Sorting
|
15
|
+
def desc
|
16
|
+
Operator.new(self, '-', true)
|
17
|
+
end
|
18
|
+
|
19
|
+
operator :eql => '==',
|
20
|
+
:not_eql => '!=',
|
21
|
+
:gt => '>',
|
22
|
+
:gte => '>=',
|
23
|
+
:lt => '<',
|
24
|
+
:lte => '<=',
|
25
|
+
:matches => '==',
|
26
|
+
:does_not_match => '!=',
|
27
|
+
:contains => '=~',
|
28
|
+
:does_not_contain => '!~',
|
29
|
+
:substring => '=@',
|
30
|
+
:not_substring => '!@'
|
31
|
+
|
32
|
+
# Metric filters
|
33
|
+
def to_ga
|
34
|
+
"ga:#{self.to_s.lower_camelized}"
|
35
|
+
end
|
36
|
+
end
|
data/lib/garb.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'cgi'
|
7
|
+
require 'ostruct'
|
8
|
+
require 'happymapper'
|
9
|
+
|
10
|
+
require 'garb/version'
|
11
|
+
require 'garb/authentication_request'
|
12
|
+
require 'garb/data_request'
|
13
|
+
require 'garb/session'
|
14
|
+
require 'garb/profile'
|
15
|
+
require 'garb/report_parameter'
|
16
|
+
require 'garb/report_response'
|
17
|
+
require 'garb/resource'
|
18
|
+
require 'garb/report'
|
19
|
+
|
20
|
+
require 'extensions/string'
|
21
|
+
require 'extensions/operator'
|
22
|
+
require 'extensions/symbol'
|
23
|
+
require 'extensions/happymapper'
|
24
|
+
|
25
|
+
module Garb
|
26
|
+
# :stopdoc:
|
27
|
+
GA = "http://schemas.google.com/analytics/2008"
|
28
|
+
|
29
|
+
VERSION = '0.1.2'
|
30
|
+
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
31
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
32
|
+
# :startdoc:
|
33
|
+
|
34
|
+
# Returns the version string for the library.
|
35
|
+
#
|
36
|
+
def self.version
|
37
|
+
VERSION
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns the library path for the module. If any arguments are given,
|
41
|
+
# they will be joined to the end of the libray path using
|
42
|
+
# <tt>File.join</tt>.
|
43
|
+
#
|
44
|
+
def self.libpath( *args )
|
45
|
+
args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns the lpath for the module. If any arguments are given,
|
49
|
+
# they will be joined to the end of the path using
|
50
|
+
# <tt>File.join</tt>.
|
51
|
+
#
|
52
|
+
def self.path( *args )
|
53
|
+
args.empty? ? PATH : ::File.join(PATH, args.flatten)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Utility method used to rquire all files ending in .rb that lie in the
|
57
|
+
# directory below this file that has the same name as the filename passed
|
58
|
+
# in. Optionally, a specific _directory_ name can be passed in such that
|
59
|
+
# the _filename_ does not have to be equivalent to the directory.
|
60
|
+
#
|
61
|
+
def self.require_all_libs_relative_to( fname, dir = nil )
|
62
|
+
dir ||= ::File.basename(fname, '.*')
|
63
|
+
search_me = ::File.expand_path(
|
64
|
+
::File.join(::File.dirname(fname), dir, '*', '*.rb'))
|
65
|
+
|
66
|
+
Dir.glob(search_me).sort.each {|rb| require rb}
|
67
|
+
end
|
68
|
+
end # module Garb
|
69
|
+
|
70
|
+
# EOF
|
@@ -0,0 +1,46 @@
|
|
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)
|
8
|
+
@email = email
|
9
|
+
@password = password
|
10
|
+
end
|
11
|
+
|
12
|
+
def parameters
|
13
|
+
{
|
14
|
+
'Email' => @email,
|
15
|
+
'Passwd' => @password,
|
16
|
+
'accountType' => 'HOSTED_OR_GOOGLE',
|
17
|
+
'service' => 'analytics',
|
18
|
+
'source' => 'vigetLabs-garb-001'
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def uri
|
23
|
+
URI.parse(URL)
|
24
|
+
end
|
25
|
+
|
26
|
+
def send_request
|
27
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
28
|
+
http.use_ssl = true
|
29
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
30
|
+
http.request(build_request) do |response|
|
31
|
+
raise AuthError unless response.is_a?(Net::HTTPOK)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def build_request
|
36
|
+
post = Net::HTTP::Post.new(uri.path)
|
37
|
+
post.set_form_data(parameters)
|
38
|
+
post
|
39
|
+
end
|
40
|
+
|
41
|
+
def auth_token
|
42
|
+
send_request.body.match(/^Auth=(.*)$/)[1]
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|