Class: Garb::AuthenticationRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/garb/authentication_request.rb

Defined Under Namespace

Classes: AuthError

Constant Summary collapse

URL =
'https://www.google.com/accounts/ClientLogin'

Instance Method Summary collapse

Constructor Details

#initialize(email, password, opts = {}) ⇒ AuthenticationRequest

Returns a new instance of AuthenticationRequest.



7
8
9
10
11
# File 'lib/garb/authentication_request.rb', line 7

def initialize(email, password, opts={})
  @email = email
  @password = password
  @account_type = opts.fetch(:account_type, 'HOSTED_OR_GOOGLE')
end

Instance Method Details

#auth_token(opts = {}) ⇒ Object



47
48
49
50
# File 'lib/garb/authentication_request.rb', line 47

def auth_token(opts={})
  ssl_mode = opts[:secure] ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
  send_request(ssl_mode).body.match(/^Auth=(.*)$/)[1]
end

#build_requestObject



41
42
43
44
45
# File 'lib/garb/authentication_request.rb', line 41

def build_request
  post = Net::HTTP::Post.new(uri.path)
  post.set_form_data(parameters)
  post
end

#parametersObject



13
14
15
16
17
18
19
20
21
# File 'lib/garb/authentication_request.rb', line 13

def parameters
  {
    'Email'       => @email,
    'Passwd'      => @password,
    'accountType' => @account_type,
    'service'     => 'analytics',
    'source'      => 'vigetLabs-garb-001'
  }
end

#send_request(ssl_mode) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/garb/authentication_request.rb', line 27

def send_request(ssl_mode)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = ssl_mode

  if ssl_mode == OpenSSL::SSL::VERIFY_PEER
    http.ca_file = CA_CERT_FILE
  end

  http.request(build_request) do |response|
    raise AuthError unless response.is_a?(Net::HTTPOK)
  end
end

#uriObject



23
24
25
# File 'lib/garb/authentication_request.rb', line 23

def uri
  URI.parse(URL)
end