garb 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,12 +5,14 @@ garb
5
5
 
6
6
  http://github.com/vigetlabs/garb
7
7
 
8
- Changes
9
- =======
8
+ Important Changes
9
+ =================
10
+
11
+ Version 0.2.4 requires happymapper from rubygems, version 0.2.5. Be sure to update.
10
12
 
11
13
  Version 0.2.0 makes major changes (compared to 0.1.0) 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
+ There is now both a module that gets included for generating defined classes,
15
+ as well as, slight changes to the way that the Report class can be used.
14
16
 
15
17
  Description
16
18
  -----------
@@ -27,9 +29,15 @@ Login
27
29
 
28
30
  > Garb::Session.login(username, password)
29
31
 
32
+ Accounts
33
+ --------
34
+ > Garb::Account.all
35
+
30
36
  Profiles
31
37
  --------
32
38
 
39
+ > Garb::Account.first.profiles
40
+
33
41
  > Garb::Profile.all
34
42
  > profile = Garb::Profile.all.first
35
43
 
@@ -156,12 +164,15 @@ TODOS
156
164
  Requirements
157
165
  ------------
158
166
 
159
- libxml
160
- happymapper
167
+ happymapper >= 0.2.5 (should also install libxml)
161
168
 
162
169
  Install
163
170
  -------
164
171
 
172
+ sudo gem install garb
173
+
174
+ OR
175
+
165
176
  sudo gem install vigetlabs-garb -s http://gems.github.com
166
177
 
167
178
  License
data/Rakefile CHANGED
@@ -17,7 +17,7 @@ spec = Gem::Specification.new do |s|
17
17
  s.files = %w(README.md Rakefile) + Dir.glob("lib/**/*")
18
18
  s.test_files = Dir.glob("test/**/*")
19
19
 
20
- s.add_dependency("jnunemaker-happymapper", [">= 0.2.2"])
20
+ s.add_dependency("happymapper", [">= 0.2.5"])
21
21
  end
22
22
 
23
23
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -0,0 +1,16 @@
1
+ class Array
2
+ def group_by
3
+ h = Hash.new
4
+
5
+ each do |element|
6
+ key = yield(element)
7
+ if h.has_key?(key)
8
+ h[key] << element
9
+ else
10
+ h[key] = [element]
11
+ end
12
+ end
13
+
14
+ h.map{|k,v| v}
15
+ end
16
+ end
@@ -10,4 +10,8 @@ class String
10
10
  def to_ga
11
11
  "ga:#{self}"
12
12
  end
13
- end
13
+
14
+ def from_ga
15
+ self.gsub(/^ga\:/, '')
16
+ end
17
+ end
@@ -12,6 +12,7 @@ require 'garb/authentication_request'
12
12
  require 'garb/data_request'
13
13
  require 'garb/session'
14
14
  require 'garb/profile'
15
+ require 'garb/account'
15
16
  require 'garb/report_parameter'
16
17
  require 'garb/report_response'
17
18
  require 'garb/resource'
@@ -20,7 +21,7 @@ require 'garb/report'
20
21
  require 'extensions/string'
21
22
  require 'extensions/operator'
22
23
  require 'extensions/symbol'
23
- require 'extensions/happymapper'
24
+ require 'extensions/array'
24
25
 
25
26
  module Garb
26
27
  # :stopdoc:
@@ -0,0 +1,15 @@
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
12
+ Profile.all.group_by{|p| p.account_id}.map{|profiles| new(profiles)}
13
+ end
14
+ end
15
+ end
@@ -1,7 +1,7 @@
1
1
  module Garb
2
2
  class Profile
3
3
 
4
- attr_reader :table_id, :title, :account_name
4
+ attr_reader :table_id, :title, :account_name, :account_id
5
5
 
6
6
  class Property
7
7
  include HappyMapper
@@ -11,35 +11,40 @@ module Garb
11
11
 
12
12
  attribute :name, String
13
13
  attribute :value, String
14
+
15
+ def instance_name
16
+ name.from_ga.underscored
17
+ end
14
18
  end
15
19
 
16
20
  class Entry
17
21
  include HappyMapper
18
22
 
19
23
  tag 'entry'
20
-
21
- element :id, Integer
24
+
22
25
  element :title, String
23
26
  element :tableId, String, :namespace => 'dxp'
24
-
25
- # has_one :table_id, TableId
27
+
26
28
  has_many :properties, Property
27
29
  end
28
30
 
29
31
  def initialize(entry)
30
32
  @title = entry.title
31
33
  @table_id = entry.tableId
32
- @account_name = entry.properties.detect{|p| p.name == 'ga:accountName'}.value
34
+
35
+ entry.properties.each do |p|
36
+ instance_variable_set :"@#{p.instance_name}", p.value
37
+ end
33
38
  end
34
-
39
+
35
40
  def id
36
- @table_id.sub(/^ga:/, '')
41
+ @table_id.from_ga
37
42
  end
38
-
43
+
39
44
  def self.all
40
45
  url = "https://www.google.com/analytics/feeds/accounts/#{Session.email}"
41
46
  response = DataRequest.new(url).send_request
42
- Entry.parse(response.body).map {|e| Garb::Profile.new(e)}
47
+ Entry.parse(response.body).map {|entry| new(entry)}
43
48
  end
44
49
  end
45
50
  end
@@ -3,11 +3,11 @@ module Garb
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 2
6
- TINY = 3
6
+ TINY = 4
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
10
10
  end
11
11
 
12
12
  end
13
- end
13
+ end
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), '..', '/test_helper')
2
+
3
+ module Garb
4
+ class AccountTest < Test::Unit::TestCase
5
+ context "The Account class" do
6
+ should "have an array of accounts with all profiles" do
7
+ p1 = stub(:account_id => '1111', :account_name => 'Blog 1')
8
+ p2 = stub(:account_id => '1112', :account_name => 'Blog 2')
9
+ Profile.stubs(:all).returns([p1,p2,p1,p2])
10
+ Account.expects(:new).with([p1,p1]).returns('account1')
11
+ Account.expects(:new).with([p2,p2]).returns('account2')
12
+ assert_equal ['account1','account2'], Account.all
13
+ end
14
+ end
15
+
16
+ context "An instance of the Account class" do
17
+ context "when creating a new account from an array of profiles" do
18
+ setup do
19
+ profile = stub(:account_id => '1111', :account_name => 'Blog 1')
20
+ @profiles = [profile,profile]
21
+ @account = Account.new(@profiles)
22
+ end
23
+
24
+ should "take the account id from the first profile" do
25
+ assert_equal @profiles.first.account_id, @account.id
26
+ end
27
+
28
+ should "take the account name from the first profile" do
29
+ assert_equal @profiles.first.account_name, @account.name
30
+ end
31
+
32
+ should "store the array of profiles" do
33
+ assert_equal @profiles, @account.profiles
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -51,6 +51,14 @@ module Garb
51
51
  should "have a value for :id" do
52
52
  assert_equal '12345', @profile.id
53
53
  end
54
+
55
+ should "have a value for :account_id" do
56
+ assert_equal '1111', @profile.account_id
57
+ end
58
+
59
+ should "have a value for :account_name" do
60
+ assert_equal 'Blog Beta', @profile.account_name
61
+ end
54
62
 
55
63
  end
56
64
 
@@ -5,5 +5,9 @@ class StringTest < Test::Unit::TestCase
5
5
  should 'prefix a string with ga: for GA' do
6
6
  assert_equal 'ga:bob', 'bob'.to_ga
7
7
  end
8
+
9
+ should 'remove ga: prefix' do
10
+ assert_equal 'bob', 'ga:bob'.from_ga
11
+ end
8
12
  end
9
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Pitale
@@ -11,18 +11,18 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-05-11 00:00:00 -04:00
14
+ date: 2009-06-19 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
- name: jnunemaker-happymapper
18
+ name: happymapper
19
19
  type: :runtime
20
20
  version_requirement:
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: 0.2.2
25
+ version: 0.2.5
26
26
  version:
27
27
  description:
28
28
  email: tony.pitale@viget.com
@@ -35,12 +35,11 @@ extra_rdoc_files: []
35
35
  files:
36
36
  - README.md
37
37
  - Rakefile
38
- - lib/extensions
39
- - lib/extensions/happymapper.rb
38
+ - lib/extensions/array.rb
40
39
  - lib/extensions/operator.rb
41
40
  - lib/extensions/string.rb
42
41
  - lib/extensions/symbol.rb
43
- - lib/garb
42
+ - lib/garb/account.rb
44
43
  - lib/garb/authentication_request.rb
45
44
  - lib/garb/data_request.rb
46
45
  - lib/garb/oauth_session.rb
@@ -52,12 +51,11 @@ files:
52
51
  - lib/garb/session.rb
53
52
  - lib/garb/version.rb
54
53
  - lib/garb.rb
55
- - test/fixtures
56
54
  - test/fixtures/cacert.pem
57
55
  - test/fixtures/profile_feed.xml
58
56
  - test/fixtures/report_feed.xml
59
57
  - test/test_helper.rb
60
- - test/unit
58
+ - test/unit/account_test.rb
61
59
  - test/unit/authentication_request_test.rb
62
60
  - test/unit/data_request_test.rb
63
61
  - test/unit/garb_test.rb
@@ -71,8 +69,10 @@ files:
71
69
  - test/unit/session_test.rb
72
70
  - test/unit/string_test.rb
73
71
  - test/unit/symbol_test.rb
74
- has_rdoc: false
72
+ has_rdoc: true
75
73
  homepage: http://github.com/vigetlabs/garb
74
+ licenses: []
75
+
76
76
  post_install_message:
77
77
  rdoc_options: []
78
78
 
@@ -93,17 +93,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  requirements: []
94
94
 
95
95
  rubyforge_project:
96
- rubygems_version: 1.3.1
96
+ rubygems_version: 1.3.4
97
97
  signing_key:
98
- specification_version: 2
98
+ specification_version: 3
99
99
  summary: Google Analytics API Ruby Wrapper
100
100
  test_files:
101
- - test/fixtures
102
101
  - test/fixtures/cacert.pem
103
102
  - test/fixtures/profile_feed.xml
104
103
  - test/fixtures/report_feed.xml
105
104
  - test/test_helper.rb
106
- - test/unit
105
+ - test/unit/account_test.rb
107
106
  - test/unit/authentication_request_test.rb
108
107
  - test/unit/data_request_test.rb
109
108
  - test/unit/garb_test.rb
@@ -1,67 +0,0 @@
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