garb 0.8.1 → 0.8.2
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 +8 -1
- data/lib/garb.rb +2 -0
- data/lib/garb/management/profile.rb +0 -4
- data/lib/garb/model.rb +83 -0
- data/lib/garb/report.rb +2 -0
- data/lib/garb/version.rb +1 -1
- data/test/test_helper.rb +4 -0
- data/test/unit/garb/model_test.rb +129 -0
- metadata +7 -4
data/README.md
CHANGED
@@ -6,7 +6,14 @@ Garb
|
|
6
6
|
Important Changes
|
7
7
|
=================
|
8
8
|
|
9
|
-
|
9
|
+
I've started to work on a new module for reporting. You can see that work
|
10
|
+
in lib/garb/model.rb. This will be the API to replace both Garb::Resource
|
11
|
+
and Garb::Report. However, it isn't complete yet, so I would advise against
|
12
|
+
using it right now. Resource and Report will be deprecated around 0.9.0 and
|
13
|
+
I'd like to remove them for 1.0.0. I will start this process after I've
|
14
|
+
updated the documentation and I feel that Garb::Model is in a solid place.
|
15
|
+
|
16
|
+
Please read CHANGELOG
|
10
17
|
|
11
18
|
Description
|
12
19
|
-----------
|
data/lib/garb.rb
CHANGED
data/lib/garb/model.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
module Garb
|
2
|
+
module Model
|
3
|
+
MONTH = 2592000
|
4
|
+
URL = "https://www.google.com/analytics/feeds/data"
|
5
|
+
|
6
|
+
def metrics(*fields)
|
7
|
+
@metrics ||= ReportParameter.new(:metrics)
|
8
|
+
@metrics << fields
|
9
|
+
end
|
10
|
+
|
11
|
+
def dimensions(*fields)
|
12
|
+
@dimensions ||= ReportParameter.new(:dimensions)
|
13
|
+
@dimensions << fields
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_instance_klass(klass)
|
17
|
+
@instance_klass = klass
|
18
|
+
end
|
19
|
+
|
20
|
+
def instance_klass
|
21
|
+
@instance_klass || OpenStruct
|
22
|
+
end
|
23
|
+
|
24
|
+
def results(profile, options = {})
|
25
|
+
default_params = build_default_params(profile)
|
26
|
+
|
27
|
+
param_set = [
|
28
|
+
default_params,
|
29
|
+
parse_filters(options).to_params,
|
30
|
+
parse_segment(options),
|
31
|
+
parse_sort(options).to_params,
|
32
|
+
build_page_params(options)
|
33
|
+
]
|
34
|
+
|
35
|
+
data = send_request_for_data(profile, build_params(param_set))
|
36
|
+
ReportResponse.new(data, instance_klass).results
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def send_request_for_data(profile, params)
|
41
|
+
request = DataRequest.new(profile.session, URL, params)
|
42
|
+
response = request.send_request
|
43
|
+
response.body
|
44
|
+
end
|
45
|
+
|
46
|
+
def build_params(param_set)
|
47
|
+
param_set.inject({}) {|p,i| p.merge(i)}.reject{|k,v| v.nil?}
|
48
|
+
end
|
49
|
+
|
50
|
+
def parse_filters(options)
|
51
|
+
filters = FilterParameters.new
|
52
|
+
filters.parameters << options[:filters] if options.has_key?(:filters)
|
53
|
+
filters
|
54
|
+
end
|
55
|
+
|
56
|
+
def parse_segment(options)
|
57
|
+
segment_id = "gaid::#{options[:segment_id].to_i}" if options.has_key?(:segment_id)
|
58
|
+
{'segment' => segment_id}
|
59
|
+
end
|
60
|
+
|
61
|
+
def parse_sort(options)
|
62
|
+
sort = ReportParameter.new(:sort)
|
63
|
+
sort << options[:sort] if options.has_key?(:sort)
|
64
|
+
sort
|
65
|
+
end
|
66
|
+
|
67
|
+
def build_default_params(profile)
|
68
|
+
{
|
69
|
+
'ids' => Garb.to_ga(profile.id),
|
70
|
+
'start-date' => format_time(Time.now - Model::MONTH),
|
71
|
+
'end-date' => format_time(Time.now)
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
def build_page_params(options)
|
76
|
+
{'max-results' => options[:limit], 'start-index' => options[:offset]}
|
77
|
+
end
|
78
|
+
|
79
|
+
def format_time(t)
|
80
|
+
t.strftime('%Y-%m-%d')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/lib/garb/report.rb
CHANGED
@@ -6,6 +6,8 @@ module Garb
|
|
6
6
|
URL = "https://www.google.com/analytics/feeds/data"
|
7
7
|
|
8
8
|
def initialize(profile, opts={})
|
9
|
+
# ActiveSupport::Deprecation.warn("The use of report will be removed in favor of extend Garb::Scheme")
|
10
|
+
|
9
11
|
@profile = profile
|
10
12
|
|
11
13
|
@start_date = opts.fetch(:start_date, Time.now - MONTH)
|
data/lib/garb/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -28,6 +28,10 @@ class MiniTest::Unit::TestCase
|
|
28
28
|
def read_fixture(filename)
|
29
29
|
File.read(File.dirname(__FILE__) + "/fixtures/#{filename}")
|
30
30
|
end
|
31
|
+
|
32
|
+
def assert_data_params(expected)
|
33
|
+
assert_received(Garb::DataRequest, :new) {|e| e.with(Garb::Session, Garb::Model::URL, expected)}
|
34
|
+
end
|
31
35
|
end
|
32
36
|
|
33
37
|
MiniTest::Unit.autorun
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ResultKlass
|
4
|
+
def initialize(attrs)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module Garb
|
9
|
+
class ModelTest < MiniTest::Unit::TestCase
|
10
|
+
context "A class extended with Garb::Model" do
|
11
|
+
setup do
|
12
|
+
@test_model = Class.new
|
13
|
+
@test_model.extend(Garb::Model)
|
14
|
+
end
|
15
|
+
|
16
|
+
# public API
|
17
|
+
should "be able to define metrics" do
|
18
|
+
report_parameter = stub(:<<)
|
19
|
+
ReportParameter.stubs(:new).returns(report_parameter)
|
20
|
+
|
21
|
+
@test_model.metrics :visits, :pageviews
|
22
|
+
|
23
|
+
assert_received(ReportParameter, :new) {|e| e.with(:metrics)}
|
24
|
+
assert_received(report_parameter, :<<) {|e| e.with([:visits, :pageviews])}
|
25
|
+
end
|
26
|
+
|
27
|
+
should "be able to define dimensions" do
|
28
|
+
report_parameter = stub(:<<)
|
29
|
+
ReportParameter.stubs(:new).returns(report_parameter)
|
30
|
+
|
31
|
+
@test_model.dimensions :page_path, :event_category
|
32
|
+
|
33
|
+
assert_received(ReportParameter, :new) {|e| e.with(:dimensions)}
|
34
|
+
assert_received(report_parameter, :<<) {|e| e.with([:page_path, :event_category])}
|
35
|
+
end
|
36
|
+
|
37
|
+
should "be able to se the instance klass" do
|
38
|
+
@test_model.set_instance_klass ResultKlass
|
39
|
+
assert_equal ResultKlass, @test_model.instance_klass
|
40
|
+
end
|
41
|
+
|
42
|
+
context "with a profile" do
|
43
|
+
setup do
|
44
|
+
entry = {
|
45
|
+
"title" => "Google Analytics Profile example.com",
|
46
|
+
"link" => [{"rel" => "self", "href" => Garb::Management::Feed::BASE_URL+"/accounts/1189765/webproperties/UA-1189765-1/profiles/98765"}],
|
47
|
+
"dxp:property" => [
|
48
|
+
{"name" => "ga:profileId", "value" => "98765"},
|
49
|
+
{"name" => "ga:accountId", "value" => "1189765"},
|
50
|
+
{"name" => "ga:webPropertyId", "value" => 'UA-1189765-1'}
|
51
|
+
]
|
52
|
+
}
|
53
|
+
|
54
|
+
@profile = Garb::Management::Profile.new(entry, Session)
|
55
|
+
end
|
56
|
+
|
57
|
+
context "when getting results" do
|
58
|
+
setup do
|
59
|
+
@response = stub(:body => "raw report data")
|
60
|
+
DataRequest.stubs(:new).returns(stub(:send_request => @response))
|
61
|
+
ReportResponse.stubs(:new).returns(stub(:results => ['result']))
|
62
|
+
|
63
|
+
now = Time.now
|
64
|
+
Time.stubs(:new).returns(now)
|
65
|
+
|
66
|
+
@params = {'ids' => Garb.to_ga(@profile.id),
|
67
|
+
'start-date' => (now - Model::MONTH).strftime('%Y-%m-%d'),
|
68
|
+
'end-date' => now.strftime('%Y-%m-%d')}
|
69
|
+
end
|
70
|
+
|
71
|
+
should "get all results" do
|
72
|
+
assert_equal ['result'], @test_model.results(@profile)
|
73
|
+
assert_received(ReportResponse, :new) {|e| e.with("raw report data", OpenStruct)}
|
74
|
+
assert_data_params(@params)
|
75
|
+
end
|
76
|
+
|
77
|
+
should "be able to filter" do
|
78
|
+
filter_parameters = stub(:<<)
|
79
|
+
FilterParameters.stubs(:new).returns(stub(:parameters => filter_parameters, :to_params => {'filters' => "params"}))
|
80
|
+
assert_equal ['result'], @test_model.results(@profile, :filters => {:page_path => '/'})
|
81
|
+
|
82
|
+
assert_data_params(@params.merge({'filters' => 'params'}))
|
83
|
+
assert_received(filter_parameters, :<<) {|e| e.with({:page_path => '/'})}
|
84
|
+
end
|
85
|
+
|
86
|
+
should "be able to set the filter segment by id" do
|
87
|
+
assert_equal ['result'], @test_model.results(@profile, :segment_id => 1)
|
88
|
+
assert_data_params(@params.merge({'segment' => 'gaid::1'}))
|
89
|
+
end
|
90
|
+
|
91
|
+
should "be able to sort" do
|
92
|
+
sort_parameter = stub(:<<)
|
93
|
+
sort_parameter.stubs(:to_params => {'sort' => 'sort value'})
|
94
|
+
ReportParameter.stubs(:new).returns(sort_parameter)
|
95
|
+
|
96
|
+
assert_equal ['result'], @test_model.results(@profile, :sort => [:visits])
|
97
|
+
assert_received(sort_parameter, :<<) {|e| e.with([:visits])}
|
98
|
+
assert_data_params(@params.merge({'sort' => 'sort value'}))
|
99
|
+
end
|
100
|
+
|
101
|
+
should "be able to limit" do
|
102
|
+
assert_equal ['result'], @test_model.results(@profile, :limit => 20)
|
103
|
+
assert_data_params(@params.merge({'max-results' => 20}))
|
104
|
+
end
|
105
|
+
|
106
|
+
should "be able to offset" do
|
107
|
+
assert_equal ['result'], @test_model.results(@profile, :offset => 10)
|
108
|
+
assert_data_params(@params.merge({'start-index' => 10}))
|
109
|
+
end
|
110
|
+
|
111
|
+
# should "be able to shift the date range"
|
112
|
+
|
113
|
+
should "return a set of results in the defined class" do
|
114
|
+
@test_model.stubs(:instance_klass).returns(ResultKlass)
|
115
|
+
|
116
|
+
assert_equal ['result'], @test_model.results(@profile)
|
117
|
+
assert_received(ReportResponse, :new) {|e| e.with("raw report data", ResultKlass)}
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# should "have a block syntax for filtering results"
|
122
|
+
# should "return results as an array of the class it belongs to, if that class is an ActiveRecord descendant"
|
123
|
+
# should "return results as an array of the class it belongs to, if that class is a DataMapper descendant"
|
124
|
+
# should "return results as an array of the class it belongs to, if that class is a MongoMapper descendant"
|
125
|
+
# should "return results as an array of the class it belongs to, if that class is a Mongoid descendant"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: garb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 59
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 8
|
9
|
-
-
|
10
|
-
version: 0.8.
|
9
|
+
- 2
|
10
|
+
version: 0.8.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tony Pitale
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-10 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/garb/management/goal.rb
|
74
74
|
- lib/garb/management/profile.rb
|
75
75
|
- lib/garb/management/web_property.rb
|
76
|
+
- lib/garb/model.rb
|
76
77
|
- lib/garb/profile.rb
|
77
78
|
- lib/garb/profile_reports.rb
|
78
79
|
- lib/garb/report.rb
|
@@ -104,6 +105,7 @@ files:
|
|
104
105
|
- test/unit/garb/management/account_test.rb
|
105
106
|
- test/unit/garb/management/profile_test.rb
|
106
107
|
- test/unit/garb/management/web_property_test.rb
|
108
|
+
- test/unit/garb/model_test.rb
|
107
109
|
- test/unit/garb/oauth_session_test.rb
|
108
110
|
- test/unit/garb/profile_reports_test.rb
|
109
111
|
- test/unit/garb/profile_test.rb
|
@@ -164,6 +166,7 @@ test_files:
|
|
164
166
|
- test/unit/garb/management/account_test.rb
|
165
167
|
- test/unit/garb/management/profile_test.rb
|
166
168
|
- test/unit/garb/management/web_property_test.rb
|
169
|
+
- test/unit/garb/model_test.rb
|
167
170
|
- test/unit/garb/oauth_session_test.rb
|
168
171
|
- test/unit/garb/profile_reports_test.rb
|
169
172
|
- test/unit/garb/profile_test.rb
|