garb 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -57,10 +57,7 @@ module Garb
57
57
  alias :from_ga :from_google_analytics
58
58
 
59
59
  def parse_properties(entry)
60
- entry['dxp:property'].inject({}) do |hash, p|
61
- hash[Garb.from_ga(p['name'])] = p['value']
62
- hash
63
- end
60
+ Hash[entry['dxp:property'].map {|p| [Garb.from_ga(p['name']),p['value']]}]
64
61
  end
65
62
 
66
63
  def parse_link(entry, rel)
@@ -1,23 +1,27 @@
1
1
  module Garb
2
2
  module Management
3
3
  class Account
4
- attr_reader :session, :path
5
- attr_reader :id, :title, :name
4
+ attr_accessor :session, :path
5
+ attr_accessor :id, :title, :name
6
6
 
7
7
  def self.all(session = Session)
8
8
  feed = Feed.new(session, '/accounts') # builds request and parses response
9
9
 
10
- feed.entries.map {|entry| new(entry, session)}
10
+ feed.entries.map {|entry| new_from_entry(entry, session)}
11
11
  end
12
12
 
13
- def initialize(entry, session)
14
- @session = session
15
- @path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
16
- @title = entry['title'].gsub('Google Analytics Account ', '')
13
+ def self.new_from_entry(entry, session)
14
+ account = new
15
+ account.session = session
16
+ account.path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
17
+ account.title = entry['title'].gsub('Google Analytics Account ', '')
18
+ account.properties = Garb.parse_properties(entry)
19
+ account
20
+ end
17
21
 
18
- properties = Garb.parse_properties(entry)
19
- @id = properties["account_id"]
20
- @name = properties["account_name"]
22
+ def properties=(properties)
23
+ self.id = properties["account_id"]
24
+ self.name = properties["account_name"]
21
25
  end
22
26
 
23
27
  def web_properties
@@ -4,12 +4,12 @@ module Garb
4
4
 
5
5
  include ProfileReports
6
6
 
7
- attr_reader :session, :path
8
- attr_reader :id, :table_id, :title, :account_id, :web_property_id
7
+ attr_accessor :session, :path
8
+ attr_accessor :id, :table_id, :title, :account_id, :web_property_id
9
9
 
10
10
  def self.all(session = Session, path = '/accounts/~all/webproperties/~all/profiles')
11
11
  feed = Feed.new(session, path)
12
- feed.entries.map {|entry| new(entry, session)}
12
+ feed.entries.map {|entry| new_from_entry(entry, session)}
13
13
  end
14
14
 
15
15
  def self.for_account(account)
@@ -20,16 +20,22 @@ module Garb
20
20
  all(web_property.session, web_property.path+'/profiles')
21
21
  end
22
22
 
23
- def initialize(entry, session)
24
- @session = session
25
- @path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
23
+ def self.new_from_entry(entry, session)
24
+ profile = new
26
25
 
27
- properties = Garb.parse_properties(entry)
28
- @id = properties['profile_id']
29
- @table_id = properties['table_id']
30
- @title = properties['profile_name']
31
- @account_id = properties['account_id']
32
- @web_property_id = properties['web_property_id']
26
+ profile.session = session
27
+ profile.path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
28
+ profile.properties = Garb.parse_properties(entry)
29
+
30
+ profile
31
+ end
32
+
33
+ def properties=(properties)
34
+ self.id = properties['profile_id']
35
+ self.table_id = properties['dxp:table_id']
36
+ self.title = properties['profile_name']
37
+ self.account_id = properties['account_id']
38
+ self.web_property_id = properties['web_property_id']
33
39
  end
34
40
 
35
41
  # def goals
@@ -1,25 +1,29 @@
1
1
  module Garb
2
2
  module Management
3
3
  class WebProperty
4
- attr_reader :session, :path
5
- attr_reader :id, :account_id
4
+ attr_accessor :session, :path
5
+ attr_accessor :id, :account_id
6
6
 
7
7
  def self.all(session = Session, path='/accounts/~all/webproperties')
8
8
  feed = Feed.new(session, path)
9
- feed.entries.map {|entry| new(entry, session)}
9
+ feed.entries.map {|entry| new_from_entry(entry, session)}
10
10
  end
11
11
 
12
12
  def self.for_account(account)
13
13
  all(account.session, account.path+'/webproperties')
14
14
  end
15
15
 
16
- def initialize(entry, session)
17
- @session = session
18
- @path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
16
+ def self.new_from_entry(entry, session)
17
+ web_property = new
18
+ web_property.session = session
19
+ web_property.path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
20
+ web_property.properties = Garb.parse_properties(entry)
21
+ web_property
22
+ end
19
23
 
20
- properties = Garb.parse_properties(entry)
21
- @id = properties["web_property_id"]
22
- @account_id = properties["account_id"]
24
+ def properties=(properties)
25
+ self.id = properties["web_property_id"]
26
+ self.account_id = properties["account_id"]
23
27
  end
24
28
 
25
29
  def profiles
@@ -3,7 +3,7 @@ module Garb
3
3
  def self.add_report_method(klass)
4
4
  # demodulize leaves potential to redefine
5
5
  # these methods given different namespaces
6
- method_name = klass.name.demodulize.underscore
6
+ method_name = klass.name.to_s.demodulize.underscore
7
7
  return unless method_name.length > 0
8
8
 
9
9
  class_eval <<-CODE
@@ -14,11 +14,9 @@ module Garb
14
14
  private
15
15
  def parse
16
16
  entries.map do |entry|
17
- hash = values_for(entry).inject({}) do |h, v|
18
- h.merge(Garb.from_ga(v['name']) => v['value'])
19
- end
20
-
21
- @instance_klass.new(hash)
17
+ @instance_klass.new(Hash[
18
+ values_for(entry).map {|v| [Garb.from_ga(v['name']), v['value']]}
19
+ ])
22
20
  end
23
21
  end
24
22
 
@@ -3,7 +3,7 @@ module Garb
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 8
6
- TINY = 4
6
+ TINY = 5
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -8,12 +8,12 @@ module Garb
8
8
  feed = stub(:entries => ["entry1"])
9
9
  Feed.stubs(:new).returns(feed)
10
10
 
11
- Account.stubs(:new)
11
+ Account.stubs(:new_from_entry)
12
12
  Account.all
13
13
 
14
14
  assert_received(Feed, :new) {|e| e.with(Session, '/accounts')}
15
15
  assert_received(feed, :entries)
16
- assert_received(Account, :new) {|e| e.with("entry1", Session)}
16
+ assert_received(Account, :new_from_entry) {|e| e.with("entry1", Session)}
17
17
  end
18
18
  end
19
19
 
@@ -27,7 +27,7 @@ module Garb
27
27
  {"name" => "ga:accountName", "value" => "Garb"}
28
28
  ]
29
29
  }
30
- @account = Account.new(entry, Session)
30
+ @account = Account.new_from_entry(entry, Session)
31
31
  end
32
32
 
33
33
  should "extract id and title from GA entry" do
@@ -8,12 +8,12 @@ module Garb
8
8
  feed = stub(:entries => ["entry1"])
9
9
  Feed.stubs(:new).returns(feed)
10
10
 
11
- Profile.stubs(:new)
11
+ Profile.stubs(:new_from_entry)
12
12
  Profile.all
13
13
 
14
14
  assert_received(Feed, :new) {|e| e.with(Session, '/accounts/~all/webproperties/~all/profiles')}
15
15
  assert_received(feed, :entries)
16
- assert_received(Profile, :new) {|e| e.with("entry1", Session)}
16
+ assert_received(Profile, :new_from_entry) {|e| e.with("entry1", Session)}
17
17
  end
18
18
 
19
19
  should "find all web properties for a given account"
@@ -31,7 +31,7 @@ module Garb
31
31
  {"name" => "ga:profileName", "value" => "example.com"}
32
32
  ]
33
33
  }
34
- @profile = Profile.new(entry, Session)
34
+ @profile = Profile.new_from_entry(entry, Session)
35
35
  end
36
36
 
37
37
  should "have a title" do
@@ -8,12 +8,12 @@ module Garb
8
8
  feed = stub(:entries => ["entry1"])
9
9
  Feed.stubs(:new).returns(feed)
10
10
 
11
- WebProperty.stubs(:new)
11
+ WebProperty.stubs(:new_from_entry)
12
12
  WebProperty.all
13
13
 
14
14
  assert_received(Feed, :new) {|e| e.with(Session, '/accounts/~all/webproperties')}
15
15
  assert_received(feed, :entries)
16
- assert_received(WebProperty, :new) {|e| e.with("entry1", Session)}
16
+ assert_received(WebProperty, :new_from_entry) {|e| e.with("entry1", Session)}
17
17
  end
18
18
 
19
19
  should "find all web properties for a given account" do
@@ -36,7 +36,7 @@ module Garb
36
36
  ]
37
37
  }
38
38
 
39
- @web_property = WebProperty.new(entry, Session)
39
+ @web_property = WebProperty.new_from_entry(entry, Session)
40
40
  end
41
41
 
42
42
  should "have an id" do
@@ -51,7 +51,7 @@ module Garb
51
51
  ]
52
52
  }
53
53
 
54
- @profile = Garb::Management::Profile.new(entry, Session)
54
+ @profile = Garb::Management::Profile.new_from_entry(entry, Session)
55
55
  end
56
56
 
57
57
  context "when getting results" do
@@ -66,6 +66,8 @@ module Garb
66
66
  now = Time.now
67
67
  Time.stubs(:new).returns(now)
68
68
 
69
+ # p @profile.id
70
+
69
71
  @params = {'ids' => Garb.to_ga(@profile.id),
70
72
  'start-date' => (now - Model::MONTH).strftime('%Y-%m-%d'),
71
73
  'end-date' => now.strftime('%Y-%m-%d'),
@@ -1,49 +1,50 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestReport
4
- extend Garb::Resource
5
- end
6
-
7
3
  # Most of the resource testing is done as a part of ReportTest
8
4
  class ResourceTest < MiniTest::Unit::TestCase
9
5
 
10
6
  context "A class with Garb::Resource mixed in" do
7
+ setup do
8
+ @test_report = Class.new
9
+ @test_report.extend(Garb::Resource)
10
+ end
11
+
11
12
  should "get results from GA" do
12
13
  profile = stub(:is_a? => true)
13
- TestReport.expects(:send_request_for_body).returns('xml')
14
+ @test_report.expects(:send_request_for_body).returns('xml')
14
15
  Garb::ReportResponse.expects(:new).with('xml', OpenStruct).returns(mock(:results => 'analytics'))
15
16
 
16
- assert_equal 'analytics', TestReport.results(profile)
17
+ assert_equal 'analytics', @test_report.results(profile)
17
18
  end
18
19
 
19
20
  should "get results from GA using a specific user session" do
20
21
  profile = '123'
21
22
  session = Garb::Session.new
22
- TestReport.expects(:send_request_for_body).returns('xml')
23
+ @test_report.expects(:send_request_for_body).returns('xml')
23
24
  Garb::ReportResponse.expects(:new).with('xml', OpenStruct).returns(mock(:results => 'analytics'))
24
25
  Garb::Profile.expects(:first).with(profile, session).returns(mock('Garb::Profile'))
25
26
 
26
- assert_equal 'analytics', TestReport.results(profile, :session => session)
27
+ assert_equal 'analytics', @test_report.results(profile, :session => session)
27
28
  end
28
29
 
29
30
  should "permit setting a segment id" do
30
- TestReport.set_segment_id 1
31
- assert_equal "gaid::1", TestReport.segment
31
+ @test_report.set_segment_id 1
32
+ assert_equal "gaid::1", @test_report.segment
32
33
  end
33
34
 
34
35
  should "permit setting a klass used for instantiation of results" do
35
36
  TestKlass = Class.new(OpenStruct)
36
- TestReport.set_instance_klass TestKlass
37
- assert_equal TestKlass, TestReport.instance_klass
37
+ @test_report.set_instance_klass TestKlass
38
+ assert_equal TestKlass, @test_report.instance_klass
38
39
  end
39
40
 
40
41
  should "return an empty result set if profile is invalid" do
41
42
  profile = '123'
42
- TestReport.expects(:send_request_for_body).never
43
+ @test_report.expects(:send_request_for_body).never
43
44
  Garb::ReportResponse.expects(:new).never
44
45
 
45
46
  Garb::Profile.expects(:first).returns(nil)
46
- assert_equal [], TestReport.results(profile)
47
+ assert_equal [], @test_report.results(profile)
47
48
  end
48
49
  end
49
50
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 8
9
- - 4
10
- version: 0.8.4
8
+ - 5
9
+ version: 0.8.5
11
10
  platform: ruby
12
11
  authors:
13
12
  - Tony Pitale
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-10-11 00:00:00 -04:00
17
+ date: 2010-12-05 00:00:00 -05:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 23
30
28
  segments:
31
29
  - 0
32
30
  - 1
@@ -42,7 +40,6 @@ dependencies:
42
40
  requirements:
43
41
  - - ">="
44
42
  - !ruby/object:Gem::Version
45
- hash: 7
46
43
  segments:
47
44
  - 2
48
45
  - 2
@@ -131,7 +128,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
128
  requirements:
132
129
  - - ">="
133
130
  - !ruby/object:Gem::Version
134
- hash: 3
135
131
  segments:
136
132
  - 0
137
133
  version: "0"
@@ -140,7 +136,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
136
  requirements:
141
137
  - - ">="
142
138
  - !ruby/object:Gem::Version
143
- hash: 3
144
139
  segments:
145
140
  - 0
146
141
  version: "0"