Permalink
Browse files

moved from using initialize to using new_from_entry class method

  • Loading branch information...
1 parent 113c200 commit 713d131b995e6ca2fa28bb7359fe281a3982f5d3 @tpitale tpitale committed Oct 12, 2010
@@ -1,23 +1,27 @@
module Garb
module Management
class Account
- attr_reader :session, :path
- attr_reader :id, :title, :name
+ attr_accessor :session, :path
+ attr_accessor :id, :title, :name
def self.all(session = Session)
feed = Feed.new(session, '/accounts') # builds request and parses response
- feed.entries.map {|entry| new(entry, session)}
+ feed.entries.map {|entry| new_from_entry(entry, session)}
end
- def initialize(entry, session)
- @session = session
- @path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
- @title = entry['title'].gsub('Google Analytics Account ', '')
+ def self.new_from_entry(entry, session)
+ account = new
+ account.session = session
+ account.path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
+ account.title = entry['title'].gsub('Google Analytics Account ', '')
+ account.properties = Garb.parse_properties(entry)
+ account
+ end
- properties = Garb.parse_properties(entry)
- @id = properties["account_id"]
- @name = properties["account_name"]
+ def properties=(properties)
+ self.id = properties["account_id"]
+ self.name = properties["account_name"]
end
def web_properties
@@ -4,12 +4,12 @@ class Profile
include ProfileReports
- attr_reader :session, :path
- attr_reader :id, :table_id, :title, :account_id, :web_property_id
+ attr_accessor :session, :path
+ attr_accessor :id, :table_id, :title, :account_id, :web_property_id
def self.all(session = Session, path = '/accounts/~all/webproperties/~all/profiles')
feed = Feed.new(session, path)
- feed.entries.map {|entry| new(entry, session)}
+ feed.entries.map {|entry| new_from_entry(entry, session)}
end
def self.for_account(account)
@@ -20,16 +20,22 @@ def self.for_web_property(web_property)
all(web_property.session, web_property.path+'/profiles')
end
- def initialize(entry, session)
- @session = session
- @path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
+ def self.new_from_entry(entry, session)
+ profile = new
- properties = Garb.parse_properties(entry)
- @id = properties['profile_id']
- @table_id = properties['table_id']
- @title = properties['profile_name']
- @account_id = properties['account_id']
- @web_property_id = properties['web_property_id']
+ profile.session = session
+ profile.path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
+ profile.properties = Garb.parse_properties(entry)
+
+ profile
+ end
+
+ def properties=(properties)
+ self.id = properties['profile_id']
+ self.table_id = properties['table_id']
+ self.title = properties['profile_name']
+ self.account_id = properties['account_id']
+ self.web_property_id = properties['web_property_id']
end
# def goals
@@ -1,25 +1,29 @@
module Garb
module Management
class WebProperty
- attr_reader :session, :path
- attr_reader :id, :account_id
+ attr_accessor :session, :path
+ attr_accessor :id, :account_id
def self.all(session = Session, path='/accounts/~all/webproperties')
feed = Feed.new(session, path)
- feed.entries.map {|entry| new(entry, session)}
+ feed.entries.map {|entry| new_from_entry(entry, session)}
end
def self.for_account(account)
all(account.session, account.path+'/webproperties')
end
- def initialize(entry, session)
- @session = session
- @path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
+ def self.new_from_entry(entry, session)
+ web_property = new
+ web_property.session = session
+ web_property.path = Garb.parse_link(entry, "self").gsub(Feed::BASE_URL, '')
+ web_property.properties = Garb.parse_properties(entry)
+ web_property
+ end
- properties = Garb.parse_properties(entry)
- @id = properties["web_property_id"]
- @account_id = properties["account_id"]
+ def properties=(properties)
+ self.id = properties["web_property_id"]
+ self.account_id = properties["account_id"]
end
def profiles
@@ -8,12 +8,12 @@ class AccountTest < MiniTest::Unit::TestCase
feed = stub(:entries => ["entry1"])
Feed.stubs(:new).returns(feed)
- Account.stubs(:new)
+ Account.stubs(:new_from_entry)
Account.all
assert_received(Feed, :new) {|e| e.with(Session, '/accounts')}
assert_received(feed, :entries)
- assert_received(Account, :new) {|e| e.with("entry1", Session)}
+ assert_received(Account, :new_from_entry) {|e| e.with("entry1", Session)}
end
end
@@ -27,7 +27,7 @@ class AccountTest < MiniTest::Unit::TestCase
{"name" => "ga:accountName", "value" => "Garb"}
]
}
- @account = Account.new(entry, Session)
+ @account = Account.new_from_entry(entry, Session)
end
should "extract id and title from GA entry" do
@@ -8,12 +8,12 @@ class ProfileTest < MiniTest::Unit::TestCase
feed = stub(:entries => ["entry1"])
Feed.stubs(:new).returns(feed)
- Profile.stubs(:new)
+ Profile.stubs(:new_from_entry)
Profile.all
assert_received(Feed, :new) {|e| e.with(Session, '/accounts/~all/webproperties/~all/profiles')}
assert_received(feed, :entries)
- assert_received(Profile, :new) {|e| e.with("entry1", Session)}
+ assert_received(Profile, :new_from_entry) {|e| e.with("entry1", Session)}
end
should "find all web properties for a given account"
@@ -31,7 +31,7 @@ class ProfileTest < MiniTest::Unit::TestCase
{"name" => "ga:profileName", "value" => "example.com"}
]
}
- @profile = Profile.new(entry, Session)
+ @profile = Profile.new_from_entry(entry, Session)
end
should "have a title" do
@@ -8,12 +8,12 @@ class WebPropertyTest < MiniTest::Unit::TestCase
feed = stub(:entries => ["entry1"])
Feed.stubs(:new).returns(feed)
- WebProperty.stubs(:new)
+ WebProperty.stubs(:new_from_entry)
WebProperty.all
assert_received(Feed, :new) {|e| e.with(Session, '/accounts/~all/webproperties')}
assert_received(feed, :entries)
- assert_received(WebProperty, :new) {|e| e.with("entry1", Session)}
+ assert_received(WebProperty, :new_from_entry) {|e| e.with("entry1", Session)}
end
should "find all web properties for a given account" do
@@ -36,7 +36,7 @@ class WebPropertyTest < MiniTest::Unit::TestCase
]
}
- @web_property = WebProperty.new(entry, Session)
+ @web_property = WebProperty.new_from_entry(entry, Session)
end
should "have an id" do

0 comments on commit 713d131

Please sign in to comment.