Class: Google::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/google/connection.rb

Overview

This is a utility class that performs all of the communication with the google calendar api.

Instance Method Summary (collapse)

Constructor Details

- (Connection) initialize(params)

set the username, password, auth_url, app_name, and login.



12
13
14
15
16
17
18
19
# File 'lib/google/connection.rb', line 12

def initialize(params)
  @username = params[:username]
  @password = params[:password]
  @auth_url = params[:auth_url] || "https://www.google.com/accounts/ClientLogin"
  @app_name = params[:app_name] || "northworld.com-googlecalendar-integration"

  ()
end

Instance Method Details

- (Object) login

login to the google calendar and grab an auth token.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/google/connection.rb', line 23

def ()
  content = {
    'Email' => @username,
    'Passwd' => @password,
    'source' => @app_name,
    'accountType' => 'HOSTED_OR_GOOGLE',
    'service' => 'cl'}

  response = send(Addressable::URI.parse(@auth_url), :post_form, content)

  raise HTTPRequestFailed unless response.kind_of? Net::HTTPSuccess

  @token = response.body.split('=').last
  @headers = {
     'Authorization' => "GoogleLogin auth=#{@token}",
     'Content-Type'  => 'application/atom+xml'
   }
   @update_header = @headers.clone
   @update_header["If-Match"] = "*"
end

- (Object) send(uri, method, content = '', redirect_count = 10)

send a request to google.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/google/connection.rb', line 46

def send(uri, method, content = '', redirect_count = 10)
  raise HTTPTooManyRedirections if redirect_count == 0

  set_session_if_necessary(uri)

  http = (uri.scheme == 'https' ? Net::HTTPS.new(uri.host, uri.inferred_port) : Net::HTTP.new(uri.host, uri.inferred_port))
  response =  http.request(build_request(uri, method, content))

  # recurse if necessary.
  if response.kind_of? Net::HTTPRedirection
    response = send(Addressable::URI.parse(response['location']), method, content, redirect_count - 1)
  end

  check_for_errors(response)

  return response
end