Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

… parallel and deprecate the old usage
  • Loading branch information...
commit a1f99bd17c495456bb0bf7e4791ea7fcd50adbea 1 parent 50ecc79
@tpitale tpitale authored
View
45 lib/garb/management/account.rb
@@ -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
View
20 lib/garb/management/goal.rb
@@ -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
View
26 lib/garb/management/management_feed.rb
@@ -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
View
33 lib/garb/management/profile.rb
@@ -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
View
5 lib/garb/management/web_property.rb
@@ -0,0 +1,5 @@
+module Garb
+ class WebProperty
+ extend ManagementFeed
+ end
+end
View
59 test/unit/garb/management/account_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.