Class: Gattica::Auth

Inherits:
Object
  • Object
show all
Includes:
Convertible
Defined in:
lib/gattica/auth.rb

Overview

Authenticates a user against the Google Client Login system

Constant Summary collapse

SCRIPT_NAME =
'/accounts/ClientLogin'
HEADERS =

Google asks that you be nice and provide a user-agent string

{ 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'Ruby Net::HTTP' }
OPTIONS =

Google asks that you provide the name of your app as a ‘source’ parameter in your POST

{ :source => 'gattica-'+VERSION, :service => 'analytics' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Convertible

#to_h, #to_query, #to_s, #to_xml

Constructor Details

#initialize(http, user) ⇒ Auth

Try to authenticate the user



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gattica/auth.rb', line 19

def initialize(http, user)
  options = OPTIONS.merge(user.to_h)
  
  response = http.post(SCRIPT_NAME, options.to_query, HEADERS)
  if response.code != '200'
    case response.code
    when '403'
      raise GatticaError::CouldNotAuthenticate, 'Your email and/or password is not recognized by the Google ClientLogin system (status code: 403)'
    else
      raise GatticaError::UnknownAnalyticsError, response.body + " (status code: #{response.code})"
    end
  end
  @tokens = parse_tokens(response.body)
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



16
17
18
# File 'lib/gattica/auth.rb', line 16

def tokens
  @tokens
end