Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Tony Pitale tpitale authored
45 lib/garb/management/account.rb
... ... @@ -0,0 +1,45 @@
  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(session = Session)
  12 + # profiles = {}
  13 + #
  14 + # Profile.all(session).each do |profile|
  15 + # (profiles[profile.account_id] ||= []) << profile
  16 + # end
  17 + #
  18 + # profiles.map {|k,v| v}.map {|profiles| new(profiles)}
  19 + # end
  20 +
  21 + extend ManagementFeed
  22 +
  23 + def self.all(*)
  24 + super # builds request and parses response
  25 +
  26 + @parsed_response['entry'].map {|entry| new(entry)}
  27 + end
  28 +
  29 + def self.path
  30 + '/accounts'
  31 + end
  32 +
  33 + def path
  34 + [self.class.path, self.id].join('/')
  35 + end
  36 +
  37 + def initialize(entry)
  38 + @id = entry[]
  39 + end
  40 +
  41 + # def web_properties
  42 + # @web_properties ||= WebProperty.for_account(self) # will call path
  43 + # end
  44 + end
  45 +end
20 lib/garb/management/goal.rb
... ... @@ -0,0 +1,20 @@
  1 +module Garb
  2 + class Goal
  3 + attr_reader :name, :number, :value, :destination
  4 +
  5 + def initialize(attributes={})
  6 + return unless attributes.is_a?(Hash)
  7 +
  8 + @name = attributes['name']
  9 + @number = attributes['number'].to_i
  10 + @value = attributes['value'].to_f
  11 + @active = (attributes['active'] == 'true')
  12 +
  13 + @destination = Destination.new(attributes[Garb.to_ga('destination')])
  14 + end
  15 +
  16 + def active?
  17 + @active
  18 + end
  19 + end
  20 +end
26 lib/garb/management/management_feed.rb
... ... @@ -0,0 +1,26 @@
  1 +module Garb
  2 + module ManagementFeed
  3 + BASE_URL = "https://www.google.com/analytics/feeds/datasources/ga"
  4 +
  5 + def all(session = Session)
  6 + build_request(session)
  7 + parsed_response
  8 + end
  9 +
  10 + def url
  11 + BASE_URL + path
  12 + end
  13 +
  14 + def parsed_response
  15 + @parsed_response ||= Crack::XML.parse(response.body)
  16 + end
  17 +
  18 + def response
  19 + @response ||= request.send_request
  20 + end
  21 +
  22 + def request(session)
  23 + @request = DataRequest.new(session, url)
  24 + end
  25 + end
  26 +end
33 lib/garb/management/profile.rb
... ... @@ -0,0 +1,33 @@
  1 +module Garb
  2 + class Profile
  3 +
  4 + include ProfileReports
  5 +
  6 + attr_reader :session, :table_id, :title, :account_name, :account_id, :web_property_id, :goals
  7 +
  8 + # def initialize(entry, session)
  9 + # @session = session
  10 + # @title = entry['title']
  11 + # @table_id = entry['dxp:tableId']
  12 + # @goals = (entry[Garb.to_ga('goal')] || []).map {|g| Goal.new(g)}
  13 + #
  14 + # entry['dxp:property'].each do |p|
  15 + # instance_variable_set :"@#{Garb.from_ga(p['name'])}", p['value']
  16 + # end
  17 + # end
  18 + #
  19 + # def id
  20 + # Garb.from_ga(@table_id)
  21 + # end
  22 + #
  23 + # def self.all(session = Session)
  24 + # AccountFeedRequest.new(session).entries.map {|entry| new(entry, session)}
  25 + # end
  26 + #
  27 + # def self.first(id, session = Session)
  28 + # all(session).detect {|profile| profile.id == id || profile.web_property_id == id }
  29 + # end
  30 +
  31 + extend
  32 + end
  33 +end
5 lib/garb/management/web_property.rb
... ... @@ -0,0 +1,5 @@
  1 +module Garb
  2 + class WebProperty
  3 + extend ManagementFeed
  4 + end
  5 +end
59 test/unit/garb/management/account_test.rb
... ... @@ -0,0 +1,59 @@
  1 +require 'test_helper'
  2 +
  3 +module Garb
  4 + class AccountTest < MiniTest::Unit::TestCase
  5 + context "The Account class" do
  6 + should "have a path to the management feed" do
  7 + assert_equal ManagementFeed::BASE_URL+'/accounts', Account.url
  8 + end
  9 + end
  10 +
  11 + # context "The Account class" do
  12 + # should "have an array of accounts with all profiles" do
  13 + # p1 = stub(:account_id => '1111', :account_name => 'Blog 1')
  14 + # p2 = stub(:account_id => '1112', :account_name => 'Blog 2')
  15 + #
  16 + # Profile.stubs(:all).returns([p1,p2,p1,p2])
  17 + # Account.stubs(:new).returns('account1', 'account2')
  18 + #
  19 + # assert_equal ['account1','account2'], Account.all
  20 + # assert_received(Profile, :all)
  21 + # assert_received(Account, :new) {|e| e.with([p1,p1])}
  22 + # assert_received(Account, :new) {|e| e.with([p2,p2])}
  23 + # end
  24 + # end
  25 + #
  26 + # context "An instance of the Account class" do
  27 + # setup do
  28 + # profile = stub(:account_id => '1111', :account_name => 'Blog 1')
  29 + # @profiles = [profile,profile]
  30 + # @account = Account.new(@profiles)
  31 + # end
  32 + #
  33 + # context "all" do
  34 + # should "use an optional user session" do
  35 + # session = Session.new
  36 + # Garb::Profile.expects(:all).with(session).returns(@profiles)
  37 + #
  38 + # accounts = Account.all(session)
  39 + # assert_equal 1, accounts.size
  40 + # assert_equal @profiles, accounts.first.profiles
  41 + # end
  42 + # end
  43 + #
  44 + # context "when creating a new account from an array of profiles" do
  45 + # should "take the account id from the first profile" do
  46 + # assert_equal @profiles.first.account_id, @account.id
  47 + # end
  48 + #
  49 + # should "take the account name from the first profile" do
  50 + # assert_equal @profiles.first.account_name, @account.name
  51 + # end
  52 + #
  53 + # should "store the array of profiles" do
  54 + # assert_equal @profiles, @account.profiles
  55 + # end
  56 + # end
  57 + # end
  58 + end
  59 +end

0 comments on commit a1f99bd

Please sign in to comment.
Something went wrong with that request. Please try again.