Permalink
Browse files

Added method to return list of segments the authenticated user has ac…

…cess to
  • Loading branch information...
Chris Le
Chris Le committed Feb 19, 2011
1 parent bb93915 commit 2099e591af8051a45f80163430a49e125c21b6b6
Showing with 57 additions and 7 deletions.
  1. +2 −1 gattica.gemspec
  2. +37 −5 lib/gattica.rb
  3. +17 −0 lib/gattica/segment.rb
  4. +1 −1 lib/gattica/version.rb
View
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = %q{gattica}
s.version = "0.3.3.2"
s.version = "0.3.3.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Rob Cameron"]
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
"lib/gattica/data_set.rb",
"lib/gattica/exceptions.rb",
"lib/gattica/user.rb",
"lib/gattica/segment.rb",
"test/helper.rb",
"test/suite.rb",
"test/test_auth.rb",
View
@@ -20,6 +20,7 @@
require 'gattica/account'
require 'gattica/data_set'
require 'gattica/data_point'
require 'gattica/segment'
# Gattica is a Ruby library for talking to the Google Analytics API.
#
@@ -68,7 +69,9 @@ def initialize(options={})
@profile_id = @options[:profile_id] # if you don't include the profile_id now, you'll have to set it manually later via Gattica::Engine#profile_id=
@user_accounts = nil # filled in later if the user ever calls Gattica::Engine#accounts
@user_segments = nil
@headers = {}.merge(@options[:headers]) # headers used for any HTTP requests (Google requires a special 'Authorization' header which is set any time @token is set)
@default_account_feed = nil # We don't want to call /analytics/feeds/accounts/default more than once so do it once and keep in in here
# save an http connection for everyone to use
@http = Net::HTTP.new(SERVER, PORT)
@@ -96,7 +99,7 @@ def initialize(options={})
# instance and you can make regular calls from then on.
#
# ga = Gattica.new({:email => 'johndoe@google.com', :password => 'password'})
# ga.get_accounts
# ga.accounts
# # you parse through the accounts to find the profile_id you need
# ga.profile_id = 12345678
# # now you can perform a regular search, see Gattica::Engine#get
@@ -109,13 +112,35 @@ def initialize(options={})
def accounts
# if we haven't retrieved the user's accounts yet, get them now and save them
if @user_accounts.nil?
data = do_http_get('/analytics/feeds/accounts/default')
data = request_default_account_feed
xml = Hpricot(data)
@user_accounts = xml.search(:entry).collect { |entry| Account.new(entry) }
end
return @user_accounts
end
# Returns the list of segments available to the authenticated user.
#
# == Usage
# ga = Gattica.new({:email => 'johndoe@google.com', :password => 'password'})
# ga.segments # Look up segment id
# my_gaid = 'gaid::-5' # Non-paid Search Traffic
# ga.profile_id = 12345678 # Set our profile ID
#
# gs.get({ :start_date => '2008-01-01',
# :end_date => '2008-02-01',
# :dimensions => 'month',
# :metrics => 'views',
# :segment => my_gaid })
def segments
if @user_segments.nil?
data = request_default_account_feed
xml = Hpricot(data)
@user_segments = xml.search("dxp:segment").collect { |s| Segment.new(s) }
end
return @user_segments
end
# This is the method that performs the actual request to get data.
#
@@ -171,9 +196,16 @@ def token=(token)
set_http_headers
end
######################################################################
private
# Gets the default account feed from Google
def request_default_account_feed
if @default_account_feed.nil?
@default_account_feed = do_http_get('/analytics/feeds/accounts/default')
end
return @default_account_feed
end
# Does the work of making HTTP calls and then going through a suite of tests on the response to make
# sure it's valid and not an error
View
@@ -0,0 +1,17 @@
require 'rubygems'
require 'hpricot'
module Gattica
class Segment
include Convertible
attr_reader :id, :name, :definition
def initialize(xml)
@id = xml.attributes['id']
@name = xml.attributes['name']
@definition = xml.at("dxp:definition").inner_html
end
end
end
View
@@ -1,3 +1,3 @@
module Gattica
VERSION = '0.3.3.mathieuravaux'
VERSION = '0.3.3.chrisle'
end

0 comments on commit 2099e59

Please sign in to comment.