Class: Gattica::Auth

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

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', :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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gattica/auth.rb', line 12

def initialize(http, user)
  options = OPTIONS.merge(user.to_h)
  options.extend HashExtensions
  
  response = http.post(SCRIPT_NAME, options.to_query, HEADERS)
  data = response.body ||= ''
  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(data)
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



9
10
11
# File 'lib/gattica/auth.rb', line 9

def tokens
  @tokens
end