robmckinnon / rugalytics

Rugalytics is a Ruby API for accessing your Google Analytics Data

This URL has Read+Write access

rugalytics / lib / rugalytics / account.rb
100644 42 lines (35 sloc) 1.147 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Rugalytics
  class Account < ::Google::Base
 
    class << self
      def find_all
        doc = Hpricot::XML get('https://www.google.com:443/analytics/settings/')
        (doc/'select[@id=account] option').inject([]) do |accounts, option|
          account_id = option['value'].to_i
          accounts << new(:account_id => account_id, :name => option.inner_html) if account_id > 0
          accounts
        end
      end
 
      def find account_id_or_name
        matchs = find_all.select{|a| a.name == account_id_or_name || a.account_id.to_s == account_id_or_name.to_s }
        matchs.empty? ? nil : matchs.first
      end
    end
 
    attr_accessor :name, :account_id
 
    def initialize(attrs)
      @name = attrs[:name]
      @account_id = attrs[:account_id]
    end
 
    def profiles(force=false)
      if force || @profiles.nil?
        @profiles = Profile.find_all(account_id)
      end
      @profiles
    end
 
    def find_profile profile_id_or_name
      profiles.detect { |p| p.profile_id.to_s == profile_id_or_name.to_s || p.name == profile_id_or_name }
    end
 
    def to_s
      "#{name} (#{account_id})"
    end
  end
end