Permalink
Browse files

first stab at new structure for management, will probably leave it in…

… parallel and deprecate the old usage
  • Loading branch information...
1 parent 50ecc79 commit a1f99bd17c495456bb0bf7e4791ea7fcd50adbea @tpitale tpitale committed Sep 30, 2010
@@ -0,0 +1,45 @@
+module Garb
+ class Account
+ attr_reader :id, :name, :profiles
+
+ # def initialize(profiles)
+ # @id = profiles.first.account_id
+ # @name = profiles.first.account_name
+ # @profiles = profiles
+ # end
+
+ # def self.all(session = Session)
+ # profiles = {}
+ #
+ # Profile.all(session).each do |profile|
+ # (profiles[profile.account_id] ||= []) << profile
+ # end
+ #
+ # profiles.map {|k,v| v}.map {|profiles| new(profiles)}
+ # end
+
+ extend ManagementFeed
+
+ def self.all(*)
+ super # builds request and parses response
+
+ @parsed_response['entry'].map {|entry| new(entry)}
+ end
+
+ def self.path
+ '/accounts'
+ end
+
+ def path
+ [self.class.path, self.id].join('/')
+ end
+
+ def initialize(entry)
+ @id = entry[]
+ end
+
+ # def web_properties
+ # @web_properties ||= WebProperty.for_account(self) # will call path
+ # end
+ end
+end
@@ -0,0 +1,20 @@
+module Garb
+ class Goal
+ attr_reader :name, :number, :value, :destination
+
+ def initialize(attributes={})
+ return unless attributes.is_a?(Hash)
+
+ @name = attributes['name']
+ @number = attributes['number'].to_i
+ @value = attributes['value'].to_f
+ @active = (attributes['active'] == 'true')
+
+ @destination = Destination.new(attributes[Garb.to_ga('destination')])
+ end
+
+ def active?
+ @active
+ end
+ end
+end
@@ -0,0 +1,26 @@
+module Garb
+ module ManagementFeed
+ BASE_URL = "https://www.google.com/analytics/feeds/datasources/ga"
+
+ def all(session = Session)
+ build_request(session)
+ parsed_response
+ end
+
+ def url
+ BASE_URL + path
+ end
+
+ def parsed_response
+ @parsed_response ||= Crack::XML.parse(response.body)
+ end
+
+ def response
+ @response ||= request.send_request
+ end
+
+ def request(session)
+ @request = DataRequest.new(session, url)
+ end
+ end
+end
@@ -0,0 +1,33 @@
+module Garb
+ class Profile
+
+ include ProfileReports
+
+ attr_reader :session, :table_id, :title, :account_name, :account_id, :web_property_id, :goals
+
+ # def initialize(entry, session)
+ # @session = session
+ # @title = entry['title']
+ # @table_id = entry['dxp:tableId']
+ # @goals = (entry[Garb.to_ga('goal')] || []).map {|g| Goal.new(g)}
+ #
+ # entry['dxp:property'].each do |p|
+ # instance_variable_set :"@#{Garb.from_ga(p['name'])}", p['value']
+ # end
+ # end
+ #
+ # def id
+ # Garb.from_ga(@table_id)
+ # end
+ #
+ # def self.all(session = Session)
+ # AccountFeedRequest.new(session).entries.map {|entry| new(entry, session)}
+ # end
+ #
+ # def self.first(id, session = Session)
+ # all(session).detect {|profile| profile.id == id || profile.web_property_id == id }
+ # end
+
+ extend
+ end
+end
@@ -0,0 +1,5 @@
+module Garb
+ class WebProperty
+ extend ManagementFeed
+ end
+end
@@ -0,0 +1,59 @@
+require 'test_helper'
+
+module Garb
+ class AccountTest < MiniTest::Unit::TestCase
+ context "The Account class" do
+ should "have a path to the management feed" do
+ assert_equal ManagementFeed::BASE_URL+'/accounts', Account.url
+ end
+ end
+
+ # context "The Account class" do
+ # should "have an array of accounts with all profiles" do
+ # p1 = stub(:account_id => '1111', :account_name => 'Blog 1')
+ # p2 = stub(:account_id => '1112', :account_name => 'Blog 2')
+ #
+ # Profile.stubs(:all).returns([p1,p2,p1,p2])
+ # Account.stubs(:new).returns('account1', 'account2')
+ #
+ # assert_equal ['account1','account2'], Account.all
+ # assert_received(Profile, :all)
+ # assert_received(Account, :new) {|e| e.with([p1,p1])}
+ # assert_received(Account, :new) {|e| e.with([p2,p2])}
+ # end
+ # end
+ #
+ # context "An instance of the Account class" do
+ # setup do
+ # profile = stub(:account_id => '1111', :account_name => 'Blog 1')
+ # @profiles = [profile,profile]
+ # @account = Account.new(@profiles)
+ # end
+ #
+ # context "all" do
+ # should "use an optional user session" do
+ # session = Session.new
+ # Garb::Profile.expects(:all).with(session).returns(@profiles)
+ #
+ # accounts = Account.all(session)
+ # assert_equal 1, accounts.size
+ # assert_equal @profiles, accounts.first.profiles
+ # end
+ # end
+ #
+ # context "when creating a new account from an array of profiles" do
+ # should "take the account id from the first profile" do
+ # assert_equal @profiles.first.account_id, @account.id
+ # end
+ #
+ # should "take the account name from the first profile" do
+ # assert_equal @profiles.first.account_name, @account.name
+ # end
+ #
+ # should "store the array of profiles" do
+ # assert_equal @profiles, @account.profiles
+ # end
+ # end
+ # end
+ end
+end

0 comments on commit a1f99bd

Please sign in to comment.