Permalink
Browse files

Cleaned up some kruft in User and Auth, moved details of HTTP call ou…

…t of Engine and into Auth
  • Loading branch information...
1 parent 4e9d6a3 commit 7efecc1ebcfaca27478edb5a1e31009f26119f3f @cannikin cannikin committed Apr 27, 2009
Showing with 14 additions and 19 deletions.
  1. +2 −0 History.txt
  2. +1 −1 examples/example.rb
  3. +4 −7 lib/gattica.rb
  4. +6 −9 lib/gattica/auth.rb
  5. +1 −2 lib/gattica/user.rb
View
@@ -1,3 +1,5 @@
+
+
== 0.2.0 / 2009-04-27
* Changed initialization format: pass a hash of options rather than individual email, password and profile_id
* Can initialize with a valid token and use that instead of requiring email/password each time
View
@@ -1,7 +1,7 @@
require '../lib/gattica'
# authenticate with the API via email/password
-ga = Gattica.new({:email => 'username@gmail.com', :password => 'password'})
+ga = Gattica.new({:email => 'activecom2@gmail.com', :password => 'Active123'})
# or, initialize via a pre-existing token (does not authenticate, but will throw an error on subsequent calls [like ga.accounts] if the token is invalid)
# ga = Gattica.new({:token => 'DQAAAJYAAACN-JMelka5I0Fs-T6lF53eUSfUooeHgcKc1iEdc0wkDS3w8GaXY7LjuUB_4vmzDB94HpScrULiweW_xQsU8yyUgdInDIX7ZnHm8_o0knf6FWSR90IoAZGsphpqteOjZ3O0NlNt603GgG7ylvGWRSeHl1ybD38nysMsKJR-dj0LYgIyPMvtgXLrqr_20oTTEExYbrDSg5_q84PkoLHUcODZ' })
View
@@ -34,8 +34,8 @@ def self.new(*args)
Engine.new(*args)
end
- # The real meat of Gattica, deals with talking to GA, returning and parsing results. You automatically
- # get an instance of this when you go Gattica.new()
+ # The real meat of Gattica, deals with talking to GA, returning and parsing results. You actually get
+ # an instance of this when you go Gattica.new()
class Engine
@@ -65,7 +65,7 @@ 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
- @headers = {} # headers used for any HTTP requests (Google requires a special 'Authorization' header)
+ @headers = {}.merge(@options[:headers]) # headers used for any HTTP requests (Google requires a special 'Authorization' header which is set any time @token is set)
# save an http connection for everyone to use
@http = Net::HTTP.new(SERVER, PORT)
@@ -75,16 +75,13 @@ def initialize(options={})
# authenticate
if @options[:email] && @options[:password] # email and password: authenticate and get a token from Google's ClientLogin
@user = User.new(@options[:email], @options[:password])
- @auth = Auth.new(@http, user, { :source => 'gattica-'+VERSION }, { 'User-Agent' => 'Ruby Net::HTTP' })
+ @auth = Auth.new(@http, user)
self.token = @auth.tokens[:auth]
elsif @options[:token] # use an existing token (this also sets the headers for any HTTP requests we make)
self.token = @options[:token]
else # no login or token, you can't do anything
raise GatticaError::NoLoginOrToken, 'You must provide an email and password, or authentication token'
end
-
- # the user can provide their own additional headers - merge them into the ones that Gattica requires
- @headers = @headers.merge(@options[:headers])
# TODO: check that the user has access to the specified profile and show an error here rather than wait for Google to respond with a message
end
View
@@ -10,18 +10,15 @@ class Auth
include Convertible
SCRIPT_NAME = '/accounts/ClientLogin'
- HEADERS = { 'Content-Type' => 'application/x-www-form-urlencoded' }
- OPTIONS = { :source => '', :service => 'analytics' }
-
+ HEADERS = { 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'Ruby Net::HTTP' } # Google asks that you be nice and provide a user-agent string
+ OPTIONS = { :source => 'gattica-'+VERSION, :service => 'analytics' } # Google asks that you provide the name of your app as a 'source' parameter in your POST
+
attr_reader :response, :data, :tokens, :token
# Prepare the user info along with options and header
- def initialize(http, user, options={}, headers={})
- data = OPTIONS.merge(options)
- data = data.merge(user.to_h)
- headers = HEADERS.merge(headers)
-
- @response, @data = http.post(SCRIPT_NAME, data.to_query, headers)
+ def initialize(http, user)
+ data = OPTIONS.merge(user.to_h)
+ @response, @data = http.post(SCRIPT_NAME, data.to_query, HEADERS)
@tokens = parse_tokens(@data)
end
View
@@ -6,10 +6,9 @@ class User
include Convertible
- SERVICE = 'analytics'
attr_accessor :email, :password
- def initialize(email,password,source='')
+ def initialize(email,password)
@email = email
@password = password
validate

0 comments on commit 7efecc1

Please sign in to comment.