Created
January 12, 2012 14:37
-
-
Save jamescook/1600873 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/garb.rb b/lib/garb.rb | |
index fed7a16..e00b5b0 100644 | |
--- a/lib/garb.rb | |
+++ b/lib/garb.rb | |
@@ -11,6 +11,8 @@ rescue LoadError | |
end | |
module Garb | |
+ autoload :Attributes, 'garb/attributes' | |
+ autoload :PathAttribute, 'garb/path_attribute' | |
autoload :Destination, 'garb/destination' | |
autoload :FilterParameters, 'garb/filter_parameters' | |
autoload :Model, 'garb/model' | |
diff --git a/lib/garb/attributes.rb b/lib/garb/attributes.rb | |
new file mode 100644 | |
index 0000000..6b937ba | |
--- /dev/null | |
+++ b/lib/garb/attributes.rb | |
@@ -0,0 +1,33 @@ | |
+module Garb | |
+ module Attributes | |
+ def self.extended(base) | |
+ base.extend(ClassMethods) | |
+ end | |
+ | |
+ module ClassMethods | |
+ def ga_attribute(*keys) | |
+ hashes, symbols = keys.partition{|k| k.is_a?(Hash) } | |
+ symbols.each do |key| | |
+ define_a_method_for(key) | |
+ end | |
+ | |
+ hashes.map{|hash| map_values_for(hash) } | |
+ end | |
+ | |
+ private | |
+ def define_a_method_for(key,custom_key=nil) | |
+ define_method(key) do | |
+ var = "@#{key}" | |
+ hash_key = custom_key.nil? ? key.to_s : custom_key.to_s | |
+ instance_variable_defined?(var) ? instance_variable_get(var) : instance_variable_set(var, @entry[hash_key]) | |
+ end unless method_defined?(key) | |
+ end | |
+ | |
+ def map_values_for(hash) | |
+ hash.each_pair do |key,value| | |
+ define_a_method_for(key,value) | |
+ end | |
+ end | |
+ end | |
+ end | |
+end | |
diff --git a/lib/garb/management/account.rb b/lib/garb/management/account.rb | |
index 701ac72..9f0157b 100644 | |
--- a/lib/garb/management/account.rb | |
+++ b/lib/garb/management/account.rb | |
@@ -1,22 +1,21 @@ | |
module Garb | |
module Management | |
class Account | |
- attr_accessor :session, :path | |
- attr_accessor :id, :title, :name | |
+ extend Garb::Attributes | |
+ include PathAttribute | |
- def self.all(session = Session) | |
- feed = Feed.new(session, '/accounts') | |
- feed.entries.map {|entry| new_from_entry(entry, session)} | |
+ attr_reader :session | |
+ | |
+ ga_attribute :id, :name | |
+ | |
+ def initialize(entry, session) | |
+ @entry = entry | |
+ @session = session | |
end | |
- def self.new_from_entry(entry, session) | |
- account = new | |
- account.session = session | |
- account.path = entry["selfLink"].gsub(Feed::BASE_URL, '') | |
- account.title = entry['name'] # TODO is this correct? | |
- account.id = entry["id"] | |
- account.name = entry["name"] | |
- account | |
+ def self.all(session = Session) | |
+ feed = Feed.new(session, '/accounts') | |
+ feed.entries.map {|entry| new(entry, session)} | |
end | |
def web_properties | |
diff --git a/lib/garb/management/feed.rb b/lib/garb/management/feed.rb | |
index f7e064e..3f08830 100644 | |
--- a/lib/garb/management/feed.rb | |
+++ b/lib/garb/management/feed.rb | |
@@ -1,7 +1,6 @@ | |
module Garb | |
module Management | |
class Feed | |
- #BASE_URL = "https://www.google.com/analytics/feeds/datasources/ga" | |
BASE_URL = "https://www.googleapis.com/analytics/v3/management" | |
attr_reader :request | |
diff --git a/lib/garb/management/goal.rb b/lib/garb/management/goal.rb | |
index f90c159..e45c739 100644 | |
--- a/lib/garb/management/goal.rb | |
+++ b/lib/garb/management/goal.rb | |
@@ -1,15 +1,17 @@ | |
module Garb | |
module Management | |
class Goal | |
+ extend Attributes | |
+ include PathAttribute | |
- attr_accessor :session, :path | |
- attr_accessor :name, :number, :value, :destination, :active | |
+ attr_reader :session | |
+ ga_attribute :name, :number, :value, :active | |
alias :active? :active | |
def self.all(session = Session, path = '/accounts/~all/webproperties/~all/profiles/~all/goals') | |
feed = Feed.new(session, path) | |
- feed.entries.map {|entry| new_from_entry(entry, session)} | |
+ feed.entries.map {|entry| new(entry, session)} | |
end | |
def self.for_account(account) | |
@@ -24,15 +26,13 @@ module Garb | |
all(profile.session, profile.path + '/goals') | |
end | |
- def self.new_from_entry(entry, session) | |
- goal = new | |
- goal.session = session | |
- goal.path = entry["selfLink"].gsub(Feed::BASE_URL, '') | |
- goal.name = entry["name"] | |
- goal.value = entry["value"] | |
- goal.active = entry["active"] | |
- goal.destination = Destination.new(entry["urlDestinationDetails"]) | |
- goal | |
+ def initialize(entry, session) | |
+ @entry = entry | |
+ @session = session | |
+ end | |
+ | |
+ def destination | |
+ @destination ||= Destination.new(@entry["urlDestinationDetails"]) | |
end | |
end | |
end | |
diff --git a/lib/garb/management/profile.rb b/lib/garb/management/profile.rb | |
index 57c8322..8db9884 100644 | |
--- a/lib/garb/management/profile.rb | |
+++ b/lib/garb/management/profile.rb | |
@@ -3,13 +3,15 @@ module Garb | |
class Profile | |
include ProfileReports | |
+ include PathAttribute | |
+ extend Attributes | |
- attr_accessor :session, :path | |
- attr_accessor :id, :name, :account_id, :web_property_id | |
+ attr_reader :session | |
+ ga_attribute :id, :name, { :account_id => "accountId", :web_property_id => "webPropertyId" } | |
def self.all(session = Session, path = '/accounts/~all/webproperties/~all/profiles') | |
feed = Feed.new(session, path) | |
- feed.entries.map {|entry| new_from_entry(entry, session)} | |
+ feed.entries.map {|entry| new(entry, session)} | |
end | |
def self.for_account(account) | |
@@ -20,15 +22,9 @@ module Garb | |
all(web_property.session, web_property.path+'/profiles') | |
end | |
- def self.new_from_entry(entry, session) | |
- profile = new | |
- profile.session = session | |
- profile.path = entry["selfLink"].gsub(Feed::BASE_URL, '') | |
- profile.id = entry['id'] | |
- profile.name = entry['name'] | |
- profile.account_id = entry['accountId'] | |
- profile.web_property_id = entry['webPropertyId'] | |
- profile | |
+ def initialize(entry, session) | |
+ @entry = entry | |
+ @session = session | |
end | |
def goals | |
diff --git a/lib/garb/management/segment.rb b/lib/garb/management/segment.rb | |
index d736c6c..e15bc08 100644 | |
--- a/lib/garb/management/segment.rb | |
+++ b/lib/garb/management/segment.rb | |
@@ -1,22 +1,20 @@ | |
module Garb | |
module Management | |
class Segment | |
- attr_accessor :session, :path | |
- attr_accessor :id, :name, :definition | |
+ extend Attributes | |
+ include PathAttribute | |
+ | |
+ attr_reader :session | |
+ ga_attribute :name, :definition, { :id => "segmentId" } | |
def self.all(session = Session) | |
feed = Feed.new(session, '/segments') | |
- feed.entries.map {|entry| new_from_entry(entry, session)} | |
+ feed.entries.map {|entry| new(entry, session)} | |
end | |
- def self.new_from_entry(entry, session) | |
- segment = new | |
- segment.session = session | |
- segment.path = entry["selfLink"].gsub(Feed::BASE_URL, '') | |
- segment.id = entry['segmentId'] | |
- segment.name = entry['name'] | |
- segment.definition = entry['definition'] | |
- segment | |
+ def initialize(entry, session) | |
+ @entry = entry | |
+ @session = session | |
end | |
end | |
end | |
diff --git a/lib/garb/management/web_property.rb b/lib/garb/management/web_property.rb | |
index 96f22da..57f48e0 100644 | |
--- a/lib/garb/management/web_property.rb | |
+++ b/lib/garb/management/web_property.rb | |
@@ -1,25 +1,24 @@ | |
module Garb | |
module Management | |
class WebProperty | |
- attr_accessor :session, :path | |
- attr_accessor :id, :account_id | |
+ extend Attributes | |
+ include PathAttribute | |
+ | |
+ attr_reader :session | |
+ ga_attribute :id, { :account_id => "accountId" } | |
def self.all(session = Session, path='/accounts/~all/webproperties') | |
feed = Feed.new(session, path) | |
- feed.entries.map {|entry| new_from_entry(entry, session)} | |
+ feed.entries.map {|entry| new(entry, session)} | |
end | |
def self.for_account(account) | |
all(account.session, account.path+'/webproperties') | |
end | |
- def self.new_from_entry(entry, session) | |
- web_property = new | |
- web_property.session = session | |
- web_property.path = entry["selfLink"].gsub(Feed::BASE_URL, '') | |
- web_property.id = entry["id"] | |
- web_property.account_id = entry["accountId"] | |
- web_property | |
+ def initialize(entry, session) | |
+ @entry = entry | |
+ @session = session | |
end | |
def profiles | |
diff --git a/lib/garb/model.rb b/lib/garb/model.rb | |
index deeb10e..8336a2b 100644 | |
--- a/lib/garb/model.rb | |
+++ b/lib/garb/model.rb | |
@@ -49,7 +49,6 @@ module Garb | |
def send_request_for_data(profile, params) | |
request = Request::Data.new(profile.session, URL, params) | |
response = request.send_request | |
- puts response.body | |
response.body | |
end | |
diff --git a/lib/garb/path_attribute.rb b/lib/garb/path_attribute.rb | |
new file mode 100644 | |
index 0000000..0d55369 | |
--- /dev/null | |
+++ b/lib/garb/path_attribute.rb | |
@@ -0,0 +1,7 @@ | |
+module Garb | |
+ module PathAttribute | |
+ def path | |
+ @path ||= @entry['selfLink'].gsub(Management::Feed::BASE_URL, '') | |
+ end | |
+ end | |
+end | |
diff --git a/test/unit/garb/management/account_test.rb b/test/unit/garb/management/account_test.rb | |
index f57ca46..9d70f09 100644 | |
--- a/test/unit/garb/management/account_test.rb | |
+++ b/test/unit/garb/management/account_test.rb | |
@@ -8,23 +8,22 @@ module Garb | |
feed = stub(:entries => ["entry1"]) | |
Feed.stubs(:new).returns(feed) | |
- Account.stubs(:new_from_entry) | |
+ Account.stubs(:new) | |
Account.all | |
assert_received(Feed, :new) {|e| e.with(Session, '/accounts')} | |
assert_received(feed, :entries) | |
- assert_received(Account, :new_from_entry) {|e| e.with("entry1", Session)} | |
+ assert_received(Account, :new) {|e| e.with("entry1", Session)} | |
end | |
end | |
context "an Account" do | |
setup do | |
entry = JSON.parse(read_fixture("account_management.json"))['items'].first | |
- @account = Account.new_from_entry(entry, Session) | |
+ @account = Account.new(entry, Session) | |
end | |
- should "extract id and title from GA entry" do | |
- assert_equal "www.google.com", @account.title | |
+ should "extract id from GA entry" do | |
assert_equal "1234", @account.id | |
end | |
diff --git a/test/unit/garb/management/goal_test.rb b/test/unit/garb/management/goal_test.rb | |
index 3bcfaa9..b2acd83 100644 | |
--- a/test/unit/garb/management/goal_test.rb | |
+++ b/test/unit/garb/management/goal_test.rb | |
@@ -8,12 +8,12 @@ module Garb | |
feed = stub(:entries => ["entry1"]) | |
Feed.stubs(:new).returns(feed) | |
- Goal.stubs(:new_from_entry) | |
+ Goal.stubs(:new) | |
Goal.all | |
assert_received(Feed, :new) {|e| e.with(Session, '/accounts/~all/webproperties/~all/profiles/~all/goals')} | |
assert_received(feed, :entries) | |
- assert_received(Goal, :new_from_entry) {|e| e.with("entry1", Session)} | |
+ assert_received(Goal, :new) {|e| e.with("entry1", Session)} | |
end | |
should "find all goals for a given account" do | |
@@ -38,7 +38,7 @@ module Garb | |
context "a Goal" do | |
setup do | |
entry = JSON.parse(read_fixture("goal_management.json"))["items"].first | |
- @goal = Goal.new_from_entry(entry, Session) | |
+ @goal = Goal.new(entry, Session) | |
end | |
should "have a name" do | |
@@ -54,7 +54,7 @@ module Garb | |
end | |
should "know if it is not active" do | |
- @goal.active = false | |
+ @goal.instance_variable_set("@active", false) | |
assert_equal false, @goal.active? | |
end | |
diff --git a/test/unit/garb/management/profile_test.rb b/test/unit/garb/management/profile_test.rb | |
index bf255d7..27aec40 100644 | |
--- a/test/unit/garb/management/profile_test.rb | |
+++ b/test/unit/garb/management/profile_test.rb | |
@@ -8,12 +8,12 @@ module Garb | |
feed = stub(:entries => ["entry1"]) | |
Feed.stubs(:new).returns(feed) | |
- Profile.stubs(:new_from_entry) | |
+ Profile.stubs(:new) | |
Profile.all | |
assert_received(Feed, :new) {|e| e.with(Session, '/accounts/~all/webproperties/~all/profiles')} | |
assert_received(feed, :entries) | |
- assert_received(Profile, :new_from_entry) {|e| e.with("entry1", Session)} | |
+ assert_received(Profile, :new) {|e| e.with("entry1", Session)} | |
end | |
should "find all profiles for a given account" do | |
@@ -32,7 +32,7 @@ module Garb | |
context "A Profile" do | |
setup do | |
entry = JSON.parse(read_fixture("ga_profile_management.json"))["items"].first | |
- @profile = Profile.new_from_entry(entry, Session) | |
+ @profile = Profile.new(entry, Session) | |
end | |
should "have a name" do | |
diff --git a/test/unit/garb/management/segment_test.rb b/test/unit/garb/management/segment_test.rb | |
index 8f1d800..896db79 100644 | |
--- a/test/unit/garb/management/segment_test.rb | |
+++ b/test/unit/garb/management/segment_test.rb | |
@@ -8,12 +8,12 @@ module Garb | |
feed = stub(:entries => ["entry1"]) | |
Feed.stubs(:new).returns(feed) | |
- Segment.stubs(:new_from_entry) | |
+ Segment.stubs(:new) | |
Segment.all | |
assert_received(Feed, :new) {|e| e.with(Session, '/segments')} | |
assert_received(feed, :entries) | |
- assert_received(Segment, :new_from_entry) {|e| e.with("entry1", Session)} | |
+ assert_received(Segment, :new) {|e| e.with("entry1", Session)} | |
end | |
end | |
@@ -28,7 +28,7 @@ module Garb | |
} | |
} | |
entry = JSON.parse(read_fixture("ga_segment_management.json"))["items"].first | |
- @segment = Segment.new_from_entry(entry, Session) | |
+ @segment = Segment.new(entry, Session) | |
end | |
should "have an id" do | |
diff --git a/test/unit/garb/management/web_property_test.rb b/test/unit/garb/management/web_property_test.rb | |
index 40919c1..6bc6138 100644 | |
--- a/test/unit/garb/management/web_property_test.rb | |
+++ b/test/unit/garb/management/web_property_test.rb | |
@@ -8,12 +8,12 @@ module Garb | |
feed = stub(:entries => ["entry1"]) | |
Feed.stubs(:new).returns(feed) | |
- WebProperty.stubs(:new_from_entry) | |
+ WebProperty.stubs(:new) | |
WebProperty.all | |
assert_received(Feed, :new) {|e| e.with(Session, '/accounts/~all/webproperties')} | |
assert_received(feed, :entries) | |
- assert_received(WebProperty, :new_from_entry) {|e| e.with("entry1", Session)} | |
+ assert_received(WebProperty, :new) {|e| e.with("entry1", Session)} | |
end | |
should "find all web properties for a given account" do | |
@@ -30,7 +30,7 @@ module Garb | |
setup do | |
entry = JSON.parse(read_fixture("ga_webproperty_management.json"))["items"].first | |
- @web_property = WebProperty.new_from_entry(entry, Session) | |
+ @web_property = WebProperty.new(entry, Session) | |
end | |
should "have an id" do | |
@@ -41,6 +41,10 @@ module Garb | |
assert_equal "1", @web_property.account_id | |
end | |
+ should "combine the WebProperty.path and the id into an new path" do | |
+ assert_equal "/accounts/1/webproperties/UA-7777-1", @web_property.path | |
+ end | |
+ | |
should "have profiles" do | |
Profile.stubs(:for_web_property) | |
@web_property.profiles | |
diff --git a/test/unit/garb/model_test.rb b/test/unit/garb/model_test.rb | |
index 5198bd4..659e757 100644 | |
--- a/test/unit/garb/model_test.rb | |
+++ b/test/unit/garb/model_test.rb | |
@@ -43,7 +43,7 @@ module Garb | |
setup do | |
entry = JSON.parse(read_fixture("ga_profile_management.json"))["items"].first | |
- @profile = Garb::Management::Profile.new_from_entry(entry, Session) | |
+ @profile = Garb::Management::Profile.new(entry, Session) | |
end | |
context "when getting results" do | |
@@ -57,7 +57,6 @@ module Garb | |
now = Time.now | |
Time.stubs(:now).returns(now) | |
- | |
@params = {'ids' => Garb.to_ga(@profile.id), | |
'start-date' => (now - Model::MONTH).strftime('%Y-%m-%d'), | |
'end-date' => now.strftime('%Y-%m-%d'), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment