Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

switched to googlebase gem for authentication and making requests

  • Loading branch information...
commit 3b2b388a58710c1b17b383d2cf0fd50d69cef981 1 parent 05a55f0
@jnunemaker authored
View
6 History.txt
@@ -1,4 +1,2 @@
-== 0.0.1 2007-10-08
-
-* 1 major enhancement:
- * Initial release
+* 0.0.2 - switched to googlebase for google auth and requests
+* 0.0.1 - initial relase
View
3  lib/statwhore.rb
@@ -8,4 +8,7 @@
require 'hpricot'
require 'active_support'
+gem 'googlebase', '>= 0.2.0'
+require 'google/base'
+
require 'statwhore/google'
View
1  lib/statwhore/google/analytics.rb
@@ -1,4 +1,3 @@
-require 'statwhore/google/analytics/base'
require 'statwhore/google/analytics/account'
require 'statwhore/google/analytics/profile'
View
16 lib/statwhore/google/analytics/account.rb
@@ -1,15 +1,13 @@
module Statwhore
module Google
module Analytics
- class Account < Base
- class << self
- def find_all
- doc = parse(request('/analytics/home/'))
- (doc/'select[@name=account_list] 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
+ class Account < ::Google::Base
+ def self.find_all
+ doc = Hpricot::XML(get('https://www.google.com:443/analytics/home/'))
+ (doc/'select[@name=account_list] 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
View
94 lib/statwhore/google/analytics/base.rb
@@ -1,94 +0,0 @@
-module Statwhore
- module Google
- module Analytics
- class Base
- class << self
-
- def establish_connection(u, p, o={})
- @@logged_in, @@username, @@password = false, u, p
-
- @@connection_options = {
- :language => LANGUAGE_ENGLISH_US
- }.merge(o)
-
- params = {
- 'continue' => 'https://www.google.com/analytics/home/?et=reset&hl=' + connection_options[:language],
- 'service' => 'analytics',
- 'nui' => 'hidden',
- 'hl' => connection_options[:language],
- 'GA3T' => 'ouVrvynQwUs',
- 'Email' => @@username,
- 'Passwd' => @@password,
- 'PersistentCookie' => 'yes',
- 'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
- }
-
- http = Net::HTTP.new("www.google.com", 443)
- http.use_ssl = true
- response = nil
- http.start do |http|
- request = Net::HTTP::Post.new("/accounts/ServiceLoginBoxAuth")
- request.set_form_data(params)
- response = http.request(request)
- end
-
- doc = Hpricot.parse(response.body)
-
- # if Google Accounts is in the title tag then the login failed
- if (doc/:title).first.inner_html =~ /Google\sAccounts/
- raise InvalidCredentials
- else
- @@cookies = response.response['set-cookie'].split('; ')[0]
- @@logged_in = true
- end
- # hpricot will through a no method error if title tag not found and google doesn't currently have title tag on their login successful redirecting page
- rescue NoMethodError
- @@cookies = response.response['set-cookie'].split('; ')[0]
- @@logged_in = true
- end
-
- def connection_options
- @@connection_options
- end
-
- def cookies
- @@cookies
- end
-
- def connection_established?
- @@logged_in
- end
-
- def request(path)
- raise "You must establish a connection before making a request" unless connection_established?
-
- headers = {
- 'Cookie' => @@cookies,
- 'User-Agent' => USERAGENT,
- 'et' => 'reset',
- 'hl' => @@connection_options[:language]
- }
-
- response, body = nil
- http = Net::HTTP.new("www.google.com", 443)
- http.use_ssl = true
- http.start do |http|
- request = Net::HTTP::Get.new(path, headers)
- response = http.request(request)
- body = response.body
- end
- body
- end
-
- def parse(body)
- Hpricot.parse(body)
- end
-
- def parse_xml(xml)
- Hpricot.XML(xml)
- end
- end
- end
- end
- end
-end
View
24 lib/statwhore/google/analytics/profile.rb
@@ -1,22 +1,20 @@
module Statwhore
module Google
module Analytics
- class Profile < Base
- class << self
- def find_all(account_id)
- doc = parse(request("/analytics/home/admin?scid=#{account_id}"))
- (doc/'select[@name=profile_list] option').inject([]) do |profiles, option|
- profile_id = option['value'].to_i
- profiles << Profile.new(:account_id => account_id, :profile_id => profile_id, :name => option.inner_html) if profile_id > 0
- profiles
- end
- end
-
- def find(account_id, profile_id)
- find_all(account_id).select { |p| p.profile_id.to_s == profile_id.to_s }.first
+ class Profile < ::Google::Base
+ def self.find_all(account_id)
+ doc = Hpricot::XML(get("https://www.google.com:443/analytics/home/admin?scid=#{account_id}"))
+ (doc/'select[@name=profile_list] option').inject([]) do |profiles, option|
+ profile_id = option['value'].to_i
+ profiles << Profile.new(:account_id => account_id, :profile_id => profile_id, :name => option.inner_html) if profile_id > 0
+ profiles
end
end
+ def self.find(account_id, profile_id)
+ find_all(account_id).select { |p| p.profile_id.to_s == profile_id.to_s }.first
+ end
+
attr_accessor :account_id, :name, :profile_id
def initialize(attrs)
View
2  lib/statwhore/version.rb
@@ -2,7 +2,7 @@ module Statwhore #:nodoc:
module VERSION #:nodoc:
MAJOR = 0
MINOR = 0
- TINY = 1
+ TINY = 2
STRING = [MAJOR, MINOR, TINY].join('.')
end
View
2  test/test_helper.rb
@@ -10,4 +10,4 @@ def test(name, &block)
end
CONFIG = YAML::load(open("#{ENV['HOME']}/.statwhore"))
-Statwhore::Google::Analytics::Base.establish_connection(CONFIG[:username], CONFIG[:password])
+Google::Base.establish_connection(CONFIG[:username], CONFIG[:password])
View
13 test/test_statwhore_google_analytics_base.rb
@@ -1,13 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestStatewhoreGoogleAnalyticsBase < Test::Unit::TestCase
- test 'should be able to establish connection' do
- assert Statwhore::Google::Analytics::Base.connection_established?
- end
-
- %w(connection_options cookies request parse parse_xml).each do |m|
- test "should have #{m} method" do
- assert Statwhore::Google::Analytics::Base.methods.include?(m)
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.